2024-09-10 17:26:46 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
mq "github.com/rabbitmq/rabbitmq-amqp-go-client/rabbitmq_amqp"
|
|
|
|
|
"os"
|
2024-09-11 20:42:05 +08:00
|
|
|
"time"
|
2024-09-10 17:26:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
fmt.Printf("Getting started with AMQP Go AMQP 1.0 Client\n")
|
|
|
|
|
chStatusChanged := make(chan *mq.StatusChanged, 1)
|
|
|
|
|
|
|
|
|
|
go func(ch chan *mq.StatusChanged) {
|
|
|
|
|
for statusChanged := range ch {
|
|
|
|
|
fmt.Printf("Status changed from %d to %d\n", statusChanged.From, statusChanged.To)
|
|
|
|
|
}
|
|
|
|
|
}(chStatusChanged)
|
|
|
|
|
|
|
|
|
|
amqpConnection := mq.NewAmqpConnection()
|
|
|
|
|
amqpConnection.NotifyStatusChange(chStatusChanged)
|
|
|
|
|
err := amqpConnection.Open(context.Background(), mq.NewConnectionSettings())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Printf("AMQP Connection opened.\n")
|
|
|
|
|
management := amqpConnection.Management()
|
|
|
|
|
queueSpec := management.Queue("getting_started_queue").
|
|
|
|
|
QueueType(mq.QueueType{Type: mq.Quorum}).
|
2024-09-11 20:42:05 +08:00
|
|
|
MaxLengthBytes(mq.CapacityGB(1))
|
|
|
|
|
exchangeSpec := management.Exchange("getting_started_exchange").
|
|
|
|
|
ExchangeType(mq.ExchangeType{Type: mq.Topic})
|
|
|
|
|
|
2024-09-10 17:26:46 +08:00
|
|
|
queueInfo, err := queueSpec.Declare(context.Background())
|
|
|
|
|
if err != nil {
|
2024-09-11 20:42:05 +08:00
|
|
|
fmt.Printf("Error declaring queue %s\n", err)
|
2024-09-10 17:26:46 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("Queue %s created.\n", queueInfo.GetName())
|
2024-09-11 20:42:05 +08:00
|
|
|
|
|
|
|
|
exchangeInfo, err := exchangeSpec.Declare(context.Background())
|
2024-09-10 17:26:46 +08:00
|
|
|
if err != nil {
|
2024-09-11 20:42:05 +08:00
|
|
|
fmt.Printf("Error declaring exchange %s\n", err)
|
2024-09-10 17:26:46 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-09-11 20:42:05 +08:00
|
|
|
fmt.Printf("Exchange %s created.\n", exchangeInfo.GetName())
|
2024-09-10 17:26:46 +08:00
|
|
|
|
2024-09-18 00:59:33 +08:00
|
|
|
bindingSpec := management.Binding().SourceExchange(exchangeSpec).DestinationQueue(queueSpec).Key("routing-key")
|
2024-09-11 20:42:05 +08:00
|
|
|
|
|
|
|
|
err = bindingSpec.Bind(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error binding %s\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Printf("Binding between %s and %s created.\n", exchangeInfo.GetName(), queueInfo.GetName())
|
|
|
|
|
|
|
|
|
|
fmt.Println("Press any key to cleanup and exit")
|
2024-09-10 17:26:46 +08:00
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
|
_, _ = reader.ReadString('\n')
|
|
|
|
|
|
2024-09-11 20:42:05 +08:00
|
|
|
err = bindingSpec.Unbind(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error unbinding %s\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Printf("Binding between %s and %s deleted.\n", exchangeInfo.GetName(), queueInfo.GetName())
|
|
|
|
|
|
|
|
|
|
err = exchangeSpec.Delete(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error deleting exchange %s\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = queueSpec.Delete(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("Queue %s deleted.\n", queueInfo.GetName())
|
|
|
|
|
|
2024-09-10 17:26:46 +08:00
|
|
|
err = amqpConnection.Close(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("AMQP Connection closed.\n")
|
2024-09-11 20:42:05 +08:00
|
|
|
// Wait for the status change to be printed
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2024-09-10 17:26:46 +08:00
|
|
|
close(chStatusChanged)
|
|
|
|
|
}
|