|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp" |
| 7 | +) |
| 8 | + |
| 9 | +func main() { |
| 10 | + exchangeName := "getting-started-go-exchange" |
| 11 | + queueName := "getting-started-go-queue" |
| 12 | + routingKey := "routing-key" |
| 13 | + |
| 14 | + env := rmq.NewEnvironment([]string{"amqp://guest:guest@localhost:5672"}, nil) |
| 15 | + |
| 16 | + // Open a connection to the AMQP 1.0 server ( RabbitMQ >= 4.0) |
| 17 | + amqpConnection, err := env.NewConnection(context.Background()) |
| 18 | + if err != nil { |
| 19 | + rmq.Error("Error opening connection", err) |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + // Create the management interface for the connection |
| 24 | + // so we can declare exchanges, queues, and bindings |
| 25 | + management := amqpConnection.Management() |
| 26 | + // TopicExchangeSpecification but can be also DirectExchangeSpecification/FanOutExchangeSpecification |
| 27 | + _, err = management.DeclareExchange(context.Background(), &rmq.TopicExchangeSpecification{ |
| 28 | + Name: exchangeName, |
| 29 | + }) |
| 30 | + |
| 31 | + if err != nil { |
| 32 | + rmq.Error("Error declaring exchange", err) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + // Declare a Quorum queue |
| 37 | + // QuorumQueueSpecification but can be also ClassicQueueSpecification, |
| 38 | + // AutoGeneratedQueueSpecification, and StreamQueueSpecification |
| 39 | + _, err = management.DeclareQueue(context.Background(), &rmq.QuorumQueueSpecification{ |
| 40 | + Name: queueName, |
| 41 | + }) |
| 42 | + |
| 43 | + if err != nil { |
| 44 | + rmq.Error("Error declaring queue", err) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + // Bind the queue to the exchange |
| 49 | + bindingPath, err := management.Bind(context.Background(), &rmq.ExchangeToQueueBindingSpecification{ |
| 50 | + SourceExchange: exchangeName, |
| 51 | + DestinationQueue: queueName, |
| 52 | + BindingKey: routingKey, |
| 53 | + }) |
| 54 | + |
| 55 | + if err != nil { |
| 56 | + rmq.Error("Error binding", err) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + publisher, err := amqpConnection.NewPublisher(context.Background(), &rmq.ExchangeAddress{ |
| 61 | + Exchange: exchangeName, |
| 62 | + Key: routingKey, |
| 63 | + }, "getting-started-publisher") |
| 64 | + if err != nil { |
| 65 | + rmq.Error("Error creating publisher", err) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + for i := 0; i < 10; i++ { |
| 70 | + publishResult, err := publisher.Publish(context.Background(), |
| 71 | + rmq.NewMessage([]byte(fmt.Sprint("Hello AMQP 1.0 - id:", i)))) |
| 72 | + if err != nil { |
| 73 | + rmq.Error("Error publishing message", err) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + switch publishResult.Outcome.(type) { |
| 78 | + // publish result |
| 79 | + case *rmq.StateAccepted: |
| 80 | + rmq.Info("Message accepted", "message", publishResult.Message.GetData()) |
| 81 | + default: |
| 82 | + rmq.Error("Message not accepted", "outcome", publishResult.Outcome) |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + consumer, err := amqpConnection.NewConsumer(context.Background(), queueName, nil) |
| 88 | + |
| 89 | + if err != nil { |
| 90 | + rmq.Error("Error creating consumer", err) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + for i := 0; i < 10; i++ { |
| 95 | + deliveryContext, err := consumer.Receive(context.Background()) |
| 96 | + if err != nil { |
| 97 | + rmq.Error("Error receiving message", err) |
| 98 | + return |
| 99 | + } |
| 100 | + rmq.Info("Received message", "message", deliveryContext.Message().GetData()) |
| 101 | + // Accept the message. Message will be removed from the queue |
| 102 | + err = deliveryContext.Accept(context.Background()) |
| 103 | + if err != nil { |
| 104 | + rmq.Error("Error accepting message", err) |
| 105 | + return |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // Close the publisher |
| 110 | + _ = publisher.Close(context.Background()) |
| 111 | + |
| 112 | + // Close the consumer |
| 113 | + _ = consumer.Close(context.Background()) |
| 114 | + |
| 115 | + // Delete binding |
| 116 | + _ = management.Unbind(context.Background(), bindingPath) |
| 117 | + |
| 118 | + // Delete the queue |
| 119 | + _ = management.DeleteQueue(context.Background(), queueName) |
| 120 | + |
| 121 | + // Delete the exchange |
| 122 | + _ = management.DeleteExchange(context.Background(), exchangeName) |
| 123 | + |
| 124 | + // Close the connection |
| 125 | + _ = amqpConnection.Close(context.Background()) |
| 126 | + |
| 127 | + // close the connection with env |
| 128 | + _ = env.CloseConnections(context.Background()) |
| 129 | +} |
0 commit comments