This repository was archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
159 lines (137 loc) · 3.22 KB
/
client.go
File metadata and controls
159 lines (137 loc) · 3.22 KB
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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/streadway/amqp"
)
func FailOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}
func ConsumeForever(
server string, queue string, existing_queue bool, autoack bool, recover bool) {
if server == "" {
server = "amqp://guest:guest@localhost:5672/"
}
conn, err := amqp.Dial(server)
FailOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
FailOnError(err, "Failed to open a channel")
defer ch.Close()
if queue == "" {
FailOnError(nil, "Queue name is empty")
}
ch.Recover(recover)
q := amqp.Queue{}
if existing_queue {
q, err = ch.QueueDeclarePassive(
queue, // name
true, // durable
false, // delete when unused (auto_delete)
false, // exclusive
false, // no-wait
nil, // arguments
)
FailOnError(err, "Queue does not exist")
} else {
q, err = ch.QueueDeclare(
queue, // name
true, // durable
false, // delete when unused (auto_delete)
false, // exclusive
false, // no-wait
nil, // arguments
)
FailOnError(err, "Failed to create the queue")
}
msgs, err := ch.Consume(
q.Name, // queue
"acker", // consumer
autoack, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
FailOnError(err, "Failed to register a consumer")
forever := make(chan bool)
total := 0
go func() {
for msg := range msgs {
total++
log.Printf("Received message: #%d, Content: %s", total, msg.Body)
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGABRT)
go func() {
for sig := range c {
log.Printf(sig.String())
log.Printf("Total consumed messages: %d", total)
os.Exit(-1)
}
}()
log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
<-forever
}
func Produce(server string, queue string, existing_queue bool, body string, count int) {
if server == "" {
server = "amqp://guest:guest@localhost:5672/"
}
conn, err := amqp.Dial(server)
FailOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()
if queue == "" {
FailOnError(nil, "Queue name is empty")
}
ch, err := conn.Channel()
FailOnError(err, "Failed to open a channel")
defer ch.Close()
q := amqp.Queue{}
if existing_queue {
q, err = ch.QueueDeclarePassive(
queue, // name
true, // durable
false, // delete when unused (auto_delete)
false, // exclusive
false, // no-wait
nil, // arguments
)
FailOnError(err, "Queue does not exist")
} else {
q, err = ch.QueueDeclare(
queue, // name
true, // durable
false, // delete when unused (auto_delete)
false, // exclusive
false, // no-wait
nil, // arguments
)
FailOnError(err, "Failed to declare a queue")
}
if count == 0 {
count = 1
}
total := 0
for i := 0; i < count; i++ {
err = ch.Publish(
q.Name, // exchange
q.Name, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(body),
})
log.Printf(" [x] Sent %s", body)
if err == nil {
total += 1
}
FailOnError(err, "Failed to publish a message")
}
log.Printf(" [x] Published %d messages", total)
}