-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrabbit.go
176 lines (153 loc) · 3.87 KB
/
rabbit.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
package main
import (
"github.com/streadway/amqp"
"log"
)
var (
amqpURI = "amqp://guest:guest@localhost:5672/"
exchangeType = "fanout"
key = "test-key"
)
type RabbitRunner struct{}
func (rr *RabbitRunner) Name() string { return "RabbitMQ" }
func (rr *RabbitRunner) setupQueues(names []string) {}
func (rr *RabbitRunner) Produce(name, body string, messages int) {
exchange := name
connection, err := amqp.Dial(amqpURI)
if err != nil {
log.Printf("Dial: %s", err)
}
defer connection.Close()
channel, err := connection.Channel()
if err != nil {
log.Printf("Channel: %s", err)
}
if err := channel.ExchangeDeclare(
exchange, // name
exchangeType, // type
true, // durable
false, // auto-deleted
false, // internal
false, // noWait
nil, // arguments
); err != nil {
log.Printf("Exchange Declare: %s", err)
}
// We have to declare a queue to receive BEFORE we can publish anything
// because AMQP didn't get enough attention from their dad as a teenager.
queue, err := channel.QueueDeclare(
name, // name of the queue
true, // durable
false, // delete when usused
false, // exclusive
false, // noWait
nil, // arguments
)
if err != nil {
log.Printf("Queue Declare: %s", err)
}
if err = channel.QueueBind(
queue.Name, // name of the queue
key, // bindingKey
exchange, // sourceExchange
false, // noWait
nil, // arguments
); err != nil {
log.Printf("Queue Bind: %s", err)
}
// Reliable publisher confirms require confirm.select support from the
// connection.
if err := channel.Confirm(false); err != nil {
log.Printf("Channel could not be put into confirm mode: %s", err)
}
ack, nack := channel.NotifyConfirm(make(chan uint64, 1), make(chan uint64, 1))
for i := 0; i < messages; i++ {
if err = channel.Publish(
exchange, // publish to an exchange
key, // routing to 0 or more queues
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(body),
DeliveryMode: amqp.Transient, // 1=non-persistent, 2=persistent
// a bunch of application/implementation-specific fields
},
); err != nil {
log.Printf("Exchange Publish: %s", err)
}
}
// yes, by default, rabbit does not care whether you published or not. check here
for i := 0; i < messages; i++ {
select {
case <-ack:
case <-nack:
}
}
}
func (rr *RabbitRunner) Consume(name string, messages int) {
exchange := name
conn, err := amqp.Dial(amqpURI)
if err != nil {
log.Println("Dial: %s", err)
}
defer conn.Close()
channel, err := conn.Channel()
if err != nil {
log.Println("Channel: %s", err)
}
defer channel.Close()
if err = channel.ExchangeDeclare(
exchange, // name of the exchange
exchangeType, // type
true, // durable
false, // delete when complete
false, // internal
false, // noWait
nil, // arguments
); err != nil {
log.Println("Exchange Declare: %s", err)
}
channel.Qos(messages, 0, false)
queue, err := channel.QueueDeclare(
name, // name of the queue
true, // durable
false, // delete when usused
false, // exclusive
false, // noWait
nil, // arguments
)
if err != nil {
log.Printf("Queue Declare: %s", err)
}
if err = channel.QueueBind(
queue.Name, // name of the queue
key, // bindingKey
exchange, // sourceExchange
false, // noWait
nil, // arguments
); err != nil {
log.Printf("Queue Bind: %s", err)
}
deliveries, err := channel.Consume(
queue.Name, // name
"", // consumerTag,
false, // noAck
false, // exclusive
false, // noLocal
false, // noWait
nil, // arguments
)
if err != nil {
log.Printf("Queue Consume: %s", err)
}
i := 0
// why?
for d := range deliveries {
d.Ack(true)
i++
if i >= messages {
break
}
}
}