/
consumer.go
44 lines (37 loc) · 941 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
43
44
package main
import (
"github.com/streadway/amqp"
"code.google.com/p/goprotobuf/proto"
"github.com/Sophiacom/RabbitMQGoDemo/protobuf"
)
func Consume(connection *amqp.Connection, queue string, outChannel chan *protobuf.Notification) {
// create a channel on this connection
channel, err := connection.Channel()
if err != nil {
panic(err)
}
defer channel.Close()
// start consuming data
consumerTag := queue + "-consumer"
deliveries, err := channel.Consume(
queue, // name
consumerTag, // consumerTag
false, // noAck
false, // exclusive
false, // noLocal
false, // noWait
nil, // arguments
)
if err != nil {
panic(err)
}
defer channel.Cancel(consumerTag, false)
for delivery := range deliveries {
notif := &protobuf.Notification{}
proto.Unmarshal(delivery.Body, notif)
outChannel <- notif
if err := delivery.Ack(false); err != nil {
panic(err)
}
}
}