2025-02-14 18:08:09 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/Azure/go-amqp"
|
2025-02-17 21:04:13 +08:00
|
|
|
rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp"
|
2025-02-14 18:08:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func checkError(err error) {
|
|
|
|
|
if err != nil {
|
2025-02-17 21:04:13 +08:00
|
|
|
rmq.Error("Error", err)
|
2025-02-14 18:08:09 +08:00
|
|
|
// it should not happen for the example
|
|
|
|
|
// so panic just to make sure we catch it
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func main() {
|
|
|
|
|
|
2025-02-17 21:04:13 +08:00
|
|
|
rmq.Info("Define the publisher message targets")
|
2025-03-05 16:46:28 +08:00
|
|
|
env := rmq.NewEnvironment("amqp://guest:guest@localhost:5672/", nil)
|
2025-02-14 18:08:09 +08:00
|
|
|
amqpConnection, err := env.NewConnection(context.Background())
|
|
|
|
|
checkError(err)
|
|
|
|
|
queues := []string{"queue1", "queue2", "queue3"}
|
|
|
|
|
management := amqpConnection.Management()
|
|
|
|
|
for _, queue := range queues {
|
2025-02-17 21:04:13 +08:00
|
|
|
_, err = management.DeclareQueue(context.TODO(), &rmq.QuorumQueueSpecification{
|
2025-02-14 18:08:09 +08:00
|
|
|
Name: queue,
|
|
|
|
|
})
|
|
|
|
|
checkError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create a publisher without a target
|
2025-02-27 20:58:59 +08:00
|
|
|
publisher, err := amqpConnection.NewPublisher(context.TODO(), nil, nil)
|
2025-02-14 18:08:09 +08:00
|
|
|
checkError(err)
|
|
|
|
|
|
|
|
|
|
// publish messages to the stream
|
|
|
|
|
for i := 0; i < 12; i++ {
|
|
|
|
|
|
|
|
|
|
// with this helper function we create a message with a target
|
|
|
|
|
// that is the same to create a message with:
|
|
|
|
|
// msg := amqp.NewMessage([]byte("hello"))
|
2025-02-17 21:04:13 +08:00
|
|
|
// MessagePropertyToAddress(msg, &QueueAddress{Queue: qName})
|
2025-02-14 18:08:09 +08:00
|
|
|
// same like:
|
|
|
|
|
// msg := amqp.NewMessage([]byte("hello"))
|
|
|
|
|
// msg.Properties = &amqp.MessageProperties{}
|
|
|
|
|
// msg.Properties.To = &address
|
2025-02-17 21:04:13 +08:00
|
|
|
// NewMessageWithAddress and MessagePropertyToAddress helpers are provided to make the
|
2025-02-14 18:08:09 +08:00
|
|
|
// code more readable and easier to use
|
2025-02-17 21:04:13 +08:00
|
|
|
msg, err := rmq.NewMessageWithAddress([]byte("Hello World"),
|
|
|
|
|
&rmq.QueueAddress{Queue: queues[i%3]})
|
2025-02-14 18:08:09 +08:00
|
|
|
checkError(err)
|
|
|
|
|
publishResult, err := publisher.Publish(context.Background(), msg)
|
|
|
|
|
checkError(err)
|
|
|
|
|
switch publishResult.Outcome.(type) {
|
|
|
|
|
case *amqp.StateAccepted:
|
2025-02-17 21:04:13 +08:00
|
|
|
rmq.Info("[Publisher]", "Message accepted", publishResult.Message.Data[0])
|
2025-02-14 18:08:09 +08:00
|
|
|
default:
|
2025-02-17 21:04:13 +08:00
|
|
|
rmq.Warn("[Publisher]", "Message not accepted", publishResult.Message.Data[0])
|
2025-02-14 18:08:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check the UI, you should see 4 messages in each queue
|
|
|
|
|
|
|
|
|
|
// Close the publisher
|
|
|
|
|
err = publisher.Close(context.Background())
|
|
|
|
|
checkError(err)
|
|
|
|
|
err = env.CloseConnections(context.Background())
|
|
|
|
|
checkError(err)
|
|
|
|
|
}
|