|
| 1 | +// RabbitMQ AMQP 1.0 Go Client: https://github.com/rabbitmq/rabbitmq-amqp-go-client |
| 2 | +// RabbitMQ AMQP 1.0 documentation: https://www.rabbitmq.com/docs/amqp |
| 3 | +// This example mirrors getting_started but declares a JMS queue via JmsQueueSpecification |
| 4 | +// (RabbitMQ queue type "jms"). JMS queues are available on Tanzu RabbitMQ 4.x; see: |
| 5 | +// https://techdocs.broadcom.com/us/en/vmware-tanzu/data-solutions/tanzu-rabbitmq-oci/4-2/tanzu-rabbitmq-oci-image/site-overview.html |
| 6 | +// example path: https://github.com/rabbitmq/rabbitmq-amqp-go-client/tree/main/docs/examples/jms_queue/main.go |
| 7 | + |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "errors" |
| 13 | + "fmt" |
| 14 | + "time" |
| 15 | + |
| 16 | + rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp" |
| 17 | +) |
| 18 | + |
| 19 | +func main() { |
| 20 | + exchangeName := "jms-queue-go-exchange" |
| 21 | + queueName := "jms-queue-go-queue" |
| 22 | + routingKey := "routing-key" |
| 23 | + |
| 24 | + rmq.Info("JMS queue example with AMQP Go AMQP 1.0 Client") |
| 25 | + |
| 26 | + stateChanged := make(chan *rmq.StateChanged, 1) |
| 27 | + go func(ch chan *rmq.StateChanged) { |
| 28 | + for statusChanged := range ch { |
| 29 | + rmq.Info("[connection]", "Status changed", statusChanged) |
| 30 | + } |
| 31 | + }(stateChanged) |
| 32 | + |
| 33 | + env := rmq.NewEnvironment("amqp://guest:guest@localhost:5672/", nil) |
| 34 | + |
| 35 | + amqpConnection, err := env.NewConnection(context.Background()) |
| 36 | + if err != nil { |
| 37 | + rmq.Error("Error opening connection", err) |
| 38 | + return |
| 39 | + } |
| 40 | + amqpConnection.NotifyStatusChange(stateChanged) |
| 41 | + |
| 42 | + rmq.Info("AMQP connection opened") |
| 43 | + management := amqpConnection.Management() |
| 44 | + exchangeInfo, err := management.DeclareExchange(context.TODO(), &rmq.TopicExchangeSpecification{ |
| 45 | + Name: exchangeName, |
| 46 | + }) |
| 47 | + if err != nil { |
| 48 | + rmq.Error("Error declaring exchange", err) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + // Declare a JMS queue (x-queue-type=jms). Optional queue arguments go in Arguments. |
| 53 | + queueInfo, err := management.DeclareQueue(context.TODO(), &rmq.JmsQueueSpecification{ |
| 54 | + Name: queueName, |
| 55 | + }) |
| 56 | + |
| 57 | + if err != nil { |
| 58 | + rmq.Error("Error declaring queue", err) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + bindingPath, err := management.Bind(context.TODO(), &rmq.ExchangeToQueueBindingSpecification{ |
| 63 | + SourceExchange: exchangeName, |
| 64 | + DestinationQueue: queueName, |
| 65 | + BindingKey: routingKey, |
| 66 | + }) |
| 67 | + |
| 68 | + if err != nil { |
| 69 | + rmq.Error("Error binding", err) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + consumer, err := amqpConnection.NewConsumer(context.Background(), queueName, nil) |
| 74 | + if err != nil { |
| 75 | + rmq.Error("Error creating consumer", err) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + consumerContext, cancel := context.WithCancel(context.Background()) |
| 80 | + |
| 81 | + go func(ctx context.Context) { |
| 82 | + for { |
| 83 | + deliveryContext, err := consumer.Receive(ctx) |
| 84 | + if errors.Is(err, context.Canceled) { |
| 85 | + rmq.Info("[Consumer] Consumer closed", "context", err) |
| 86 | + return |
| 87 | + } |
| 88 | + if err != nil { |
| 89 | + rmq.Error("[Consumer] Error receiving message", "error", err) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + rmq.Info("[Consumer] Received message", "message", |
| 94 | + fmt.Sprintf("%s", deliveryContext.Message().Data)) |
| 95 | + |
| 96 | + err = deliveryContext.Accept(context.Background()) |
| 97 | + if err != nil { |
| 98 | + rmq.Error("[Consumer] Error accepting message", "error", err) |
| 99 | + return |
| 100 | + } |
| 101 | + } |
| 102 | + }(consumerContext) |
| 103 | + |
| 104 | + publisher, err := amqpConnection.NewPublisher(context.Background(), &rmq.ExchangeAddress{ |
| 105 | + Exchange: exchangeName, |
| 106 | + Key: routingKey, |
| 107 | + }, nil) |
| 108 | + if err != nil { |
| 109 | + rmq.Error("Error creating publisher", err) |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + for i := 0; i < 100; i++ { |
| 114 | + publishResult, err := publisher.Publish(context.Background(), rmq.NewMessage([]byte("Hello, World!"+fmt.Sprintf("%d", i)))) |
| 115 | + if err != nil { |
| 116 | + rmq.Error("Error publishing message", "error", err) |
| 117 | + time.Sleep(1 * time.Second) |
| 118 | + continue |
| 119 | + } |
| 120 | + switch publishResult.Outcome.(type) { |
| 121 | + case *rmq.StateAccepted: |
| 122 | + rmq.Info("[Publisher]", "Message accepted", publishResult.Message.Data[0]) |
| 123 | + case *rmq.StateReleased: |
| 124 | + rmq.Warn("[Publisher]", "Message was not routed", publishResult.Message.Data[0]) |
| 125 | + case *rmq.StateRejected: |
| 126 | + rmq.Warn("[Publisher]", "Message rejected", publishResult.Message.Data[0]) |
| 127 | + stateType := publishResult.Outcome.(*rmq.StateRejected) |
| 128 | + if stateType.Error != nil { |
| 129 | + rmq.Warn("[Publisher]", "Message rejected with error: %v", stateType.Error) |
| 130 | + } |
| 131 | + default: |
| 132 | + rmq.Warn("Message state: %v", publishResult.Outcome) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + println("press any key to close the connection") |
| 137 | + |
| 138 | + var input string |
| 139 | + _, _ = fmt.Scanln(&input) |
| 140 | + |
| 141 | + cancel() |
| 142 | + err = consumer.Close(context.Background()) |
| 143 | + if err != nil { |
| 144 | + rmq.Error("[Consumer]", err) |
| 145 | + return |
| 146 | + } |
| 147 | + err = publisher.Close(context.Background()) |
| 148 | + if err != nil { |
| 149 | + rmq.Error("[Publisher]", err) |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + err = management.Unbind(context.TODO(), bindingPath) |
| 154 | + |
| 155 | + if err != nil { |
| 156 | + rmq.Error("Error unbinding", "error", err) |
| 157 | + return |
| 158 | + } |
| 159 | + |
| 160 | + err = management.DeleteExchange(context.TODO(), exchangeInfo.Name()) |
| 161 | + if err != nil { |
| 162 | + rmq.Error("Error deleting exchange", "error", err) |
| 163 | + return |
| 164 | + } |
| 165 | + |
| 166 | + purged, err := management.PurgeQueue(context.TODO(), queueInfo.Name()) |
| 167 | + if err != nil { |
| 168 | + rmq.Error("Error purging queue", "error", err) |
| 169 | + return |
| 170 | + } |
| 171 | + rmq.Info("Purged messages from the queue", "count", purged) |
| 172 | + |
| 173 | + err = management.DeleteQueue(context.TODO(), queueInfo.Name()) |
| 174 | + if err != nil { |
| 175 | + rmq.Error("Error deleting queue", "error", err) |
| 176 | + return |
| 177 | + } |
| 178 | + |
| 179 | + err = env.CloseConnections(context.Background()) |
| 180 | + if err != nil { |
| 181 | + rmq.Error("Error closing connection", "error", err) |
| 182 | + return |
| 183 | + } |
| 184 | + |
| 185 | + rmq.Info("AMQP connection closed") |
| 186 | + time.Sleep(100 * time.Millisecond) |
| 187 | + close(stateChanged) |
| 188 | +} |
0 commit comments