-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrabbitmq.go
41 lines (35 loc) · 850 Bytes
/
rabbitmq.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
package rabbitmq
import (
"log"
amqp "github.com/rabbitmq/amqp091-go"
)
func FailOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}
// Connect to RabbitMQ and return a connection
func Connect(url string) *amqp.Connection {
conn, err := amqp.Dial(url)
FailOnError(err, "Failed to connect to RabbitMQ")
return conn
}
// Create a channel from the connection
func CreateChannel(conn *amqp.Connection) *amqp.Channel {
ch, err := conn.Channel()
FailOnError(err, "Failed to open a channel")
return ch
}
// Declare a queue
func DeclareQueue(ch *amqp.Channel, queueName string) amqp.Queue {
q, err := ch.QueueDeclare(
queueName,
false, // Durable
false, // Delete when unused
false, // Exclusive
false, // No-wait
nil, // Arguments
)
FailOnError(err, "Failed to declare a queue")
return q
}