forked from ssproessig/go-webservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamqp_api.go
41 lines (32 loc) · 908 Bytes
/
amqp_api.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 main
import (
"encoding/json"
"log"
"github.com/streadway/amqp"
)
const QueueName = "orm:todos"
func Connect2AMQPAndSetupQueue(amqpUri string, todoChanged chan Todo) {
connection, err := amqp.Dial(amqpUri)
if err != nil {
log.Fatalf("Failed to connect to %s: %s", amqpUri, err)
}
defer connection.Close()
log.Printf("Connected to AMQP: %s", amqpUri)
channel, err := connection.Channel()
if err != nil {
log.Fatalf("Failed to acquire channel: %s", err)
}
defer channel.Close()
_, err = channel.QueueDeclare(QueueName, false, false, false, false, nil, )
if err != nil {
log.Fatalf("Failed to declare queue '%s': %s", QueueName, err)
}
log.Printf("Using queue: %s", QueueName)
for {
todo := <-todoChanged
todoBytes, _ := json.Marshal(todo)
channel.Publish("", QueueName, false, false,
amqp.Publishing{ContentType: "text/json", Body: []byte(todoBytes)},
)
}
}