-
Notifications
You must be signed in to change notification settings - Fork 2
/
consumer.go
42 lines (34 loc) · 872 Bytes
/
consumer.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
package rabbit
import (
"github.com/streadway/amqp"
)
func createConsumer(channel *amqp.Channel, subscriber *Subscriber) error {
channel.Qos(subscriber.PrefetchCount, 0, false)
messages, err := channel.Consume(
subscriber.Queue, // queue
"", // consumer
false, // auto ack
false, // exclusive
false, // no local
false, // no wait
nil, // args
)
if err != nil {
return err
}
handler := Handlers[subscriber.RoutingKey]
go consumeMessages(messages, handler, subscriber.ManualAck)
return nil
}
func consumeMessages(messages <-chan amqp.Delivery, handler func(amqp.Delivery) bool, manualAck bool) {
for message := range messages {
result := handler(message)
if !manualAck {
if result {
message.Ack(false)
} else {
message.Nack(false, true)
}
}
}
}