-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (41 loc) · 1.04 KB
/
main.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
package main
import (
"flag"
"log"
"github.com/streadway/amqp"
)
func main() {
useAmqpAddress := flag.String("amqp", "amqp://guest:guest@localhost:5677/", "address to amqp broker")
flag.Parse()
consumer, err := NewRandomConsumer(*useAmqpAddress)
if err != nil {
log.Fatal(err)
}
consumer.Consume()
}
type RandomConsumer struct {
cc *amqp.Channel
}
func NewRandomConsumer(amqpAddress string) (*RandomConsumer, error) {
conn, err := amqp.Dial(amqpAddress)
if err != nil {
return nil, err
}
ch, err := conn.Channel()
if err != nil {
return nil, err
}
return &RandomConsumer{
cc: ch,
}, nil
}
func (c *RandomConsumer) Consume() {
_ = c.cc.ExchangeDeclare("rnd", amqp.ExchangeTopic, true, false, false, false, nil)
queue, _ := c.cc.QueueDeclare("outbox", true, false, false, false, nil)
_ = c.cc.QueueBind(queue.Name, "outbox-subject", "rnd", false, nil)
ch, _ := c.cc.Consume("outbox", "", false, false, false, false, nil)
for next := range ch {
log.Printf("next arrived message: %s", next.Body)
_ = next.Ack(false)
}
}