@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
"github.com/Azure/go-amqp"
7
8
"github.com/rabbitmq/rabbitmq-amqp-go-client/rabbitmq_amqp"
@@ -67,48 +68,97 @@ func main() {
67
68
return
68
69
}
69
70
70
- addr , err := rabbitmq_amqp .ExchangeAddress (& exchangeName , & routingKey )
71
-
72
- publisher , err := amqpConnection .Publisher (context .Background (), addr , "getting-started-publisher" )
71
+ // Create a consumer to receive messages from the queue
72
+ // you need to build the address of the queue, but you can use the helper function
73
+ addrQueue , _ := rabbitmq_amqp .QueueAddress (& queueName )
74
+ consumer , err := amqpConnection .Consumer (context .Background (), addrQueue , "getting-started-consumer" )
73
75
if err != nil {
74
- rabbitmq_amqp .Error ("Error creating publisher " , err )
76
+ rabbitmq_amqp .Error ("Error creating consumer " , err )
75
77
return
76
78
}
77
79
78
- // Publish a message to the exchange
79
- publishResult , err := publisher .Publish (context .Background (), amqp .NewMessage ([]byte ("Hello, World!" )))
80
+ consumerContext , cancel := context .WithCancel (context .Background ())
81
+
82
+ // Consume messages from the queue
83
+ go func (ctx context.Context ) {
84
+ for {
85
+ deliveryContext , err := consumer .Receive (ctx )
86
+ if errors .Is (err , context .Canceled ) {
87
+ // The consumer was closed correctly
88
+ rabbitmq_amqp .Info ("[Consumer]" , "consumer closed. Context" , err )
89
+ return
90
+ }
91
+ if err != nil {
92
+ // An error occurred receiving the message
93
+ rabbitmq_amqp .Error ("[Consumer]" , "Error receiving message" , err )
94
+ return
95
+ }
96
+
97
+ rabbitmq_amqp .Info ("[Consumer]" , "Received message" ,
98
+ fmt .Sprintf ("%s" , deliveryContext .Message ().Data ))
99
+
100
+ err = deliveryContext .Accept (context .Background ())
101
+ if err != nil {
102
+ rabbitmq_amqp .Error ("Error accepting message" , err )
103
+ return
104
+ }
105
+ }
106
+ }(consumerContext )
107
+
108
+ addr , _ := rabbitmq_amqp .ExchangeAddress (& exchangeName , & routingKey )
109
+ publisher , err := amqpConnection .Publisher (context .Background (), addr , "getting-started-publisher" )
80
110
if err != nil {
81
- rabbitmq_amqp .Error ("Error publishing message " , err )
111
+ rabbitmq_amqp .Error ("Error creating publisher " , err )
82
112
return
83
113
}
84
- switch publishResult .Outcome {
85
- case & amqp.StateAccepted {}:
86
- rabbitmq_amqp .Info ("Message accepted" )
87
- case & amqp.StateReleased {}:
88
- rabbitmq_amqp .Warn ("Message was not routed" )
89
- case & amqp.StateRejected {}:
90
- rabbitmq_amqp .Warn ("Message rejected" )
91
- stateType := publishResult .Outcome .(* amqp.StateRejected )
92
- if stateType .Error != nil {
93
- rabbitmq_amqp .Warn ("Message rejected with error: %v" , stateType .Error )
114
+
115
+ for i := 0 ; i < 10 ; i ++ {
116
+
117
+ // Publish a message to the exchange
118
+ publishResult , err := publisher .Publish (context .Background (), amqp .NewMessage ([]byte ("Hello, World!" + fmt .Sprintf ("%d" , i ))))
119
+ if err != nil {
120
+ rabbitmq_amqp .Error ("Error publishing message" , err )
121
+ return
122
+ }
123
+ switch publishResult .Outcome .(type ) {
124
+ case * amqp.StateAccepted :
125
+ rabbitmq_amqp .Info ("[Publisher]" , "Message accepted" , publishResult .Message .Data [0 ])
126
+ break
127
+ case * amqp.StateReleased :
128
+ rabbitmq_amqp .Warn ("[Publisher]" , "Message was not routed" , publishResult .Message .Data [0 ])
129
+ break
130
+ case * amqp.StateRejected :
131
+ rabbitmq_amqp .Warn ("[Publisher]" , "Message rejected" , publishResult .Message .Data [0 ])
132
+ stateType := publishResult .Outcome .(* amqp.StateRejected )
133
+ if stateType .Error != nil {
134
+ rabbitmq_amqp .Warn ("[Publisher]" , "Message rejected with error: %v" , stateType .Error )
135
+ }
136
+ break
137
+ default :
138
+ // these status are not supported. Leave it for AMQP 1.0 compatibility
139
+ // see: https://www.rabbitmq.com/docs/next/amqp#outcomes
140
+ rabbitmq_amqp .Warn ("Message state: %v" , publishResult .Outcome )
94
141
}
95
- default :
96
- // these status are not supported
97
- rabbitmq_amqp .Warn ("Message state: %v" , publishResult .Outcome )
98
142
}
99
143
100
144
println ("press any key to close the connection" )
101
145
102
146
var input string
103
147
_ , _ = fmt .Scanln (& input )
104
148
149
+ cancel ()
150
+ //Close the consumer
151
+ err = consumer .Close (context .Background ())
152
+ if err != nil {
153
+ rabbitmq_amqp .Error ("[Consumer]" , err )
154
+ }
105
155
// Close the publisher
106
156
err = publisher .Close (context .Background ())
107
157
if err != nil {
108
158
return
109
159
}
110
- // Unbind the queue from the exchange
111
160
161
+ // Unbind the queue from the exchange
112
162
err = management .Unbind (context .TODO (), bindingPath )
113
163
114
164
if err != nil {
@@ -143,8 +193,7 @@ func main() {
143
193
}
144
194
145
195
fmt .Printf ("AMQP Connection closed.\n " )
146
- // Wait for the status change to be printed
147
- time .Sleep (500 * time .Millisecond )
148
-
149
- close (stateChangeds )
196
+ // not necessary. It waits for the status change to be printed
197
+ time .Sleep (100 * time .Millisecond )
198
+ close (stateChanged )
150
199
}
0 commit comments