-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathconsumer_example_test.go
170 lines (140 loc) · 3.55 KB
/
consumer_example_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
package rabbitroutine_test
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/furdarius/rabbitroutine"
amqp "github.com/rabbitmq/amqp091-go"
)
// Consumer implement rabbitroutine.Consumer interface.
type Consumer struct {
ExchangeName string
QueueName string
}
// Declare implement rabbitroutine.Consumer.(Declare) interface method.
func (c *Consumer) Declare(ctx context.Context, ch *amqp.Channel) error {
err := ch.ExchangeDeclare(
c.ExchangeName, // name
"direct", // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
if err != nil {
log.Printf("failed to declare exchange %v: %v", c.ExchangeName, err)
return err
}
_, err = ch.QueueDeclare(
c.QueueName, // name
true, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
log.Printf("failed to declare queue %v: %v", c.QueueName, err)
return err
}
err = ch.QueueBind(
c.QueueName, // queue name
c.QueueName, // routing key
c.ExchangeName, // exchange
false, // no-wait
nil, // arguments
)
if err != nil {
log.Printf("failed to bind queue %v: %v", c.QueueName, err)
return err
}
return nil
}
// Consume implement rabbitroutine.Consumer.(Consume) interface method.
func (c *Consumer) Consume(ctx context.Context, ch *amqp.Channel) error {
defer log.Println("consume method finished")
err := ch.Qos(
1, // prefetch count
0, // prefetch size
false, // global
)
if err != nil {
log.Printf("failed to set qos: %v", err)
return err
}
msgs, err := ch.Consume(
c.QueueName, // queue
"myconsumer", // consumer name
false, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
log.Printf("failed to consume %v: %v", c.QueueName, err)
return err
}
for {
select {
case msg, ok := <-msgs:
if !ok {
return amqp.ErrClosed
}
content := string(msg.Body)
fmt.Println("New message:", content)
err := msg.Ack(false)
if err != nil {
log.Printf("failed to Ack message: %v", err)
}
case <-ctx.Done():
return ctx.Err()
}
}
}
// This example demonstrates consuming messages from RabbitMQ queue.
func ExampleConsumer() {
ctx := context.Background()
url := "amqp://guest:[email protected]:5672/"
conn := rabbitroutine.NewConnector(rabbitroutine.Config{
// Max reconnect attempts
ReconnectAttempts: 20,
// How long wait between reconnect
Wait: 2 * time.Second,
})
conn.AddRetriedListener(func(r rabbitroutine.Retried) {
log.Printf("try to connect to RabbitMQ: attempt=%d, error=\"%v\"",
r.ReconnectAttempt, r.Error)
})
conn.AddDialedListener(func(_ rabbitroutine.Dialed) {
log.Printf("RabbitMQ connection successfully established")
})
conn.AddAMQPNotifiedListener(func(n rabbitroutine.AMQPNotified) {
log.Printf("RabbitMQ error received: %v", n.Error)
})
consumer := &Consumer{
ExchangeName: "myexch",
QueueName: "myqueue",
}
go func() {
err := conn.Dial(ctx, url)
if err != nil {
log.Println("failed to establish RabbitMQ connection:", err)
}
}()
go func() {
err := conn.StartMultipleConsumers(ctx, consumer, 5)
if err != nil {
log.Println("failed to start consumer:", err)
}
}()
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, os.Interrupt, syscall.SIGTERM)
// Wait for OS termination signal
<-sigc
}