forked from streadway/amqp
/
examples_test.go
276 lines (230 loc) · 7.73 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package amqp_test
import (
"github.com/streadway/amqp"
"log"
"runtime"
"time"
)
func ExampleChannel_Confirm_bridge() {
// This example acts as a bridge, shoveling all messages sent from the source
// exchange "log" to destination exchange "log".
// Confirming publishes can help from overproduction and ensure every message
// is delivered.
// Setup the source of the store and forward
source, err := amqp.Dial("amqp://source/")
if err != nil {
log.Fatalf("connection.open source: %s", err)
}
defer source.Close()
chs, err := source.Channel()
if err != nil {
log.Fatalf("channel.open source: %s", err)
}
if err := chs.ExchangeDeclare("log", "topic", true, false, false, false, nil); err != nil {
log.Fatalf("exchange.declare destination: %s", err)
}
if _, err := chs.QueueDeclare("remote-tee", true, true, false, false, nil); err != nil {
log.Fatalf("queue.declare source: %s", err)
}
if err := chs.QueueBind("remote-tee", "#", "logs", false, nil); err != nil {
log.Fatalf("queue.bind source: %s", err)
}
shovel, err := chs.Consume("remote-tee", "shovel", false, false, false, false, nil)
if err != nil {
log.Fatalf("basic.consume source: %s", err)
}
// Setup the destination of the store and forward
destination, err := amqp.Dial("amqp://destination/")
if err != nil {
log.Fatalf("connection.open destination: %s", err)
}
defer destination.Close()
chd, err := destination.Channel()
if err != nil {
log.Fatalf("channel.open destination: %s", err)
}
if err := chd.ExchangeDeclare("log", "topic", true, false, false, false, nil); err != nil {
log.Fatalf("exchange.declare destination: %s", err)
}
pubAcks, pubNacks := chd.NotifyConfirm(make(chan uint64), make(chan uint64))
if err := chd.Confirm(false); err != nil {
log.Fatalf("confirm.select destination: %s", err)
}
// Now pump the messages, one by one, a smarter implementation
// would batch the deliveries and use multiple ack/nacks
for {
msg, ok := <-shovel
if !ok {
log.Fatalf("source channel closed, see the reconnect example for handling this")
}
err = chd.Publish("logs", msg.RoutingKey, false, false, amqp.Publishing{
// Copy all the properties
ContentType: msg.ContentType,
ContentEncoding: msg.ContentEncoding,
DeliveryMode: msg.DeliveryMode,
Priority: msg.Priority,
CorrelationId: msg.CorrelationId,
ReplyTo: msg.ReplyTo,
Expiration: msg.Expiration,
MessageId: msg.MessageId,
Timestamp: msg.Timestamp,
Type: msg.Type,
UserId: msg.UserId,
AppId: msg.AppId,
// Custom headers
Headers: msg.Headers,
// And the body
Body: msg.Body,
})
if err != nil {
msg.Nack(false, false)
log.Fatalf("basic.publish destination: %s", msg)
}
// only ack the source delivery when the destination acks the publishing
// here you could check for delivery order by keeping a local state of
// expected delivery tags
select {
case <-pubAcks:
msg.Ack(false)
case <-pubNacks:
msg.Nack(false, false)
}
}
}
func ExampleChannel_Consume() {
// Connects opens an AMQP connection from the credentials in the URL.
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatalf("connection.open: %s", err)
}
defer conn.Close()
c, err := conn.Channel()
if err != nil {
log.Fatalf("channel.open: %s", err)
}
// We declare our topology on both the publisher and consumer to ensure they
// are the same. This is part of AMQP being a programmable messaging model.
//
// See the Channel.Publish example for the complimentary declare.
err = c.ExchangeDeclare("logs", "topic", true, false, false, false, nil)
if err != nil {
log.Fatal("exchange.declare: %s", err)
}
// Establish our queue topologies that we are responsible for
type bind struct {
queue string
key string
}
bindings := []bind{
bind{"page", "alert"},
bind{"email", "info"},
bind{"firehose", "#"},
}
for _, b := range bindings {
_, err = c.QueueDeclare(b.queue, true, false, false, false, nil)
if err != nil {
log.Fatal("queue.declare: %s", err)
}
err = c.QueueBind(b.queue, b.key, "logs", false, nil)
if err != nil {
log.Fatal("queue.bind: %s", err)
}
}
// Set our quality of service. Since we're sharing 3 consumers on the same
// channel, we want at least 3 messages in flight.
err = c.Qos(3, 0, false)
if err != nil {
log.Fatal("basic.qos: %s", err)
}
// Establish our consumers that have different responsibilities. Our first
// two queues do not ack the messages on the server, so require to be acked
// on the client.
pages, err := c.Consume("page", "pager", false, false, false, false, nil)
if err != nil {
log.Fatal("basic.consume: %s", err)
}
go func() {
for log := range pages {
// ... this consumer is responsible for sending pages per log
log.Ack(false)
}
}()
// Notice how the concern for which messages arrive here are in the AMQP
// topology and not in the queue. We let the server pick a consumer tag this
// time.
emails, err := c.Consume("email", "", false, false, false, false, nil)
if err != nil {
log.Fatal("basic.consume: %s", err)
}
go func() {
for log := range emails {
// ... this consumer is responsible for sending emails per log
log.Ack(false)
}
}()
// This consumer requests that every message is acknowledged as soon as it's
// delivered.
firehose, err := c.Consume("firehose", "", true, false, false, false, nil)
if err != nil {
log.Fatal("basic.consume: %s", err)
}
// To show how to process the items in parallel, we'll use a work pool.
for i := 0; i < runtime.NumCPU(); i++ {
go func(work <-chan amqp.Delivery) {
for _ = range work {
// ... this consumer pulls from the firehose and doesn't need to acknowledge
}
}(firehose)
}
// Wait until you're ready to finish, could be a signal handler here.
time.Sleep(10 * time.Second)
// Cancelling a consumer by name will finish the range and gracefully end the
// goroutine
err = c.Cancel("pager", false)
if err != nil {
log.Fatal("basic.cancel: %s", err)
}
// deferred closing the Connection will also finish the consumer's ranges of
// their delivery chans. If you need every delivery to be processed, make
// sure to wait for all consumers goroutines to finish before exiting your
// process.
}
func ExampleChannel_Publish() {
// Connects opens an AMQP connection from the credentials in the URL.
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatalf("connection.open: %s", err)
}
// This waits for a server acknowledgment which means the sockets will have
// flushed all outbound publishings prior to returning. It's important to
// block on Close to not lose any publishings.
defer conn.Close()
c, err := conn.Channel()
if err != nil {
log.Fatalf("channel.open: %s", err)
}
// We declare our topology on both the publisher and consumer to ensure they
// are the same. This is part of AMQP being a programmable messaging model.
//
// See the Channel.Consume example for the complimentary declare.
err = c.ExchangeDeclare("logs", "topic", true, false, false, false, nil)
if err != nil {
log.Fatal("exchange.declare: %s", err)
}
// Prepare this message to be persistent. Your publishing requirements may
// be different.
msg := amqp.Publishing{
DeliveryMode: amqp.Persistent,
Timestamp: time.Now(),
ContentType: "text/plain",
Body: []byte("Go Go AMQP!"),
}
// This is not a mandatory delivery, so it will be dropped if there are no
// queues bound to the logs exchange.
err = c.Publish("logs", "info", false, false, msg)
if err != nil {
// Since publish is asynchronous this can happen if the network connection
// is reset or if the server has run out of resources.
log.Fatal("basic.publish: %s", err)
}
}