2024-09-10 17:26:46 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2024-11-21 17:34:08 +08:00
|
|
|
"github.com/Azure/go-amqp"
|
2024-11-15 15:37:28 +08:00
|
|
|
"github.com/rabbitmq/rabbitmq-amqp-go-client/rabbitmq_amqp"
|
2024-09-11 20:42:05 +08:00
|
|
|
"time"
|
2024-09-10 17:26:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2024-11-21 17:34:08 +08:00
|
|
|
|
|
|
|
|
exchangeName := "getting-started-exchange"
|
|
|
|
|
queueName := "getting-started-queue"
|
|
|
|
|
routingKey := "routing-key"
|
|
|
|
|
|
2024-09-10 17:26:46 +08:00
|
|
|
fmt.Printf("Getting started with AMQP Go AMQP 1.0 Client\n")
|
|
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
/// Create a channel to receive status change notifications
|
|
|
|
|
chStatusChanged := make(chan *rabbitmq_amqp.StatusChanged, 1)
|
2024-11-15 15:37:28 +08:00
|
|
|
go func(ch chan *rabbitmq_amqp.StatusChanged) {
|
2024-09-10 17:26:46 +08:00
|
|
|
for statusChanged := range ch {
|
2024-11-15 15:37:28 +08:00
|
|
|
fmt.Printf("%s\n", statusChanged)
|
2024-09-10 17:26:46 +08:00
|
|
|
}
|
|
|
|
|
}(chStatusChanged)
|
|
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
// Open a connection to the AMQP 1.0 server
|
|
|
|
|
amqpConnection, err := rabbitmq_amqp.Dial(context.Background(), "amqp://", nil)
|
2024-09-10 17:26:46 +08:00
|
|
|
if err != nil {
|
2024-11-15 15:37:28 +08:00
|
|
|
fmt.Printf("Error opening connection: %v\n", err)
|
2024-09-10 17:26:46 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-11-21 17:34:08 +08:00
|
|
|
// Register the channel to receive status change notifications
|
|
|
|
|
amqpConnection.NotifyStatusChange(chStatusChanged)
|
|
|
|
|
|
2024-09-10 17:26:46 +08:00
|
|
|
fmt.Printf("AMQP Connection opened.\n")
|
2024-11-21 17:34:08 +08:00
|
|
|
// Create the management interface for the connection
|
|
|
|
|
// so we can declare exchanges, queues, and bindings
|
2024-09-10 17:26:46 +08:00
|
|
|
management := amqpConnection.Management()
|
2024-11-15 15:37:28 +08:00
|
|
|
exchangeInfo, err := management.DeclareExchange(context.TODO(), &rabbitmq_amqp.ExchangeSpecification{
|
2024-11-21 17:34:08 +08:00
|
|
|
Name: exchangeName,
|
2024-11-15 15:37:28 +08:00
|
|
|
})
|
2024-09-10 17:26:46 +08:00
|
|
|
if err != nil {
|
2024-11-15 15:37:28 +08:00
|
|
|
fmt.Printf("Error declaring exchange: %v\n", err)
|
2024-09-10 17:26:46 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-09-11 20:42:05 +08:00
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
// Declare a Quorum queue
|
2024-11-15 15:37:28 +08:00
|
|
|
queueInfo, err := management.DeclareQueue(context.TODO(), &rabbitmq_amqp.QueueSpecification{
|
2024-11-21 17:34:08 +08:00
|
|
|
Name: queueName,
|
2024-11-15 15:37:28 +08:00
|
|
|
QueueType: rabbitmq_amqp.QueueType{Type: rabbitmq_amqp.Quorum},
|
|
|
|
|
})
|
|
|
|
|
|
2024-09-10 17:26:46 +08:00
|
|
|
if err != nil {
|
2024-11-15 15:37:28 +08:00
|
|
|
fmt.Printf("Error declaring queue: %v\n", err)
|
2024-09-10 17:26:46 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
// Bind the queue to the exchange
|
2024-11-15 15:37:28 +08:00
|
|
|
bindingPath, err := management.Bind(context.TODO(), &rabbitmq_amqp.BindingSpecification{
|
2024-11-21 17:34:08 +08:00
|
|
|
SourceExchange: exchangeName,
|
|
|
|
|
DestinationQueue: queueName,
|
|
|
|
|
BindingKey: routingKey,
|
2024-11-15 15:37:28 +08:00
|
|
|
})
|
2024-09-11 20:42:05 +08:00
|
|
|
|
|
|
|
|
if err != nil {
|
2024-11-15 15:37:28 +08:00
|
|
|
fmt.Printf("Error binding: %v\n", err)
|
2024-09-11 20:42:05 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
addr, err := rabbitmq_amqp.ExchangeAddress(&exchangeName, &routingKey)
|
|
|
|
|
|
|
|
|
|
publisher, err := amqpConnection.Publisher(context.Background(), addr, "getting-started-publisher")
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error creating publisher: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Publish a message to the exchange
|
|
|
|
|
err = publisher.Publish(context.Background(), amqp.NewMessage([]byte("Hello, World!")))
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error publishing message: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println("press any key to close the connection")
|
|
|
|
|
|
|
|
|
|
var input string
|
|
|
|
|
_, _ = fmt.Scanln(&input)
|
|
|
|
|
|
|
|
|
|
// Close the publisher
|
|
|
|
|
err = publisher.Close(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Unbind the queue from the exchange
|
|
|
|
|
|
2024-11-15 15:37:28 +08:00
|
|
|
err = management.Unbind(context.TODO(), bindingPath)
|
2024-09-10 17:26:46 +08:00
|
|
|
|
2024-09-11 20:42:05 +08:00
|
|
|
if err != nil {
|
2024-11-15 15:37:28 +08:00
|
|
|
fmt.Printf("Error unbinding: %v\n", err)
|
2024-09-11 20:42:05 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 15:37:28 +08:00
|
|
|
err = management.DeleteExchange(context.TODO(), exchangeInfo.Name())
|
2024-09-11 20:42:05 +08:00
|
|
|
if err != nil {
|
2024-11-15 15:37:28 +08:00
|
|
|
fmt.Printf("Error deleting exchange: %v\n", err)
|
2024-09-11 20:42:05 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
// Purge the queue
|
|
|
|
|
purged, err := management.PurgeQueue(context.TODO(), queueInfo.Name())
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error purging queue: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("Purged %d messages from the queue.\n", purged)
|
|
|
|
|
|
2024-11-15 15:37:28 +08:00
|
|
|
err = management.DeleteQueue(context.TODO(), queueInfo.Name())
|
2024-09-11 20:42:05 +08:00
|
|
|
if err != nil {
|
2024-11-15 15:37:28 +08:00
|
|
|
fmt.Printf("Error deleting queue: %v\n", err)
|
2024-09-11 20:42:05 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 17:26:46 +08:00
|
|
|
err = amqpConnection.Close(context.Background())
|
|
|
|
|
if err != nil {
|
2024-11-15 15:37:28 +08:00
|
|
|
fmt.Printf("Error closing connection: %v\n", err)
|
2024-09-10 17:26:46 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-11-15 15:37:28 +08:00
|
|
|
|
2024-09-10 17:26:46 +08:00
|
|
|
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-11-15 15:37:28 +08:00
|
|
|
|
2024-09-10 17:26:46 +08:00
|
|
|
close(chStatusChanged)
|
|
|
|
|
}
|