-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
75 lines (65 loc) · 1.46 KB
/
example_test.go
File metadata and controls
75 lines (65 loc) · 1.46 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
package bcamqp
import "log"
func Example() {
broker := New(BrokerOptions{
Encrypted: true,
Address: "localhost",
User: "guest",
Password: "guest",
AutoTimestamp: true,
})
err := broker.Connect()
if err != nil {
log.Fatalf("connect to broker: %v", err)
}
defer broker.Close()
err = broker.DeclareExchange(ExchangeOptions{
Name: "example-exchange",
Durable: false,
Type: Direct,
})
if err != nil {
log.Fatalf("declare exchange: %v", err)
}
err = broker.DeclareQueue(QueueOptions{
Name: "example-queue",
Durable: false,
Exclusive: false,
})
if err != nil {
log.Fatalf("declare queue: %v", err)
}
err = broker.DeclareBinding(BindingOptions{
Exchange: "example-exchange",
Queue: "example-queue",
RoutingKey: "#",
})
if err != nil {
log.Fatalf("declare binding: %v", err)
}
err = broker.Publish(Message{
Exchange: "example-exchange",
Body: []byte("test"),
ContentType: "text/plain",
})
if err != nil {
log.Fatalf("publish: %v", err)
}
cons, err := broker.Consume(ConsumerOptions{
Name: "bcamqp-example",
Queue: "example-queue",
AutoAck: false,
Exclusive: false,
})
if err != nil {
log.Fatalf("consume: %v", err)
}
defer cons.Close()
for msg := range cons.Messages() {
log.Printf(`%v message with routing key "%s": %s`, msg.Timestamp, msg.RoutingKey, msg.Body)
err = msg.Ack()
if err != nil {
log.Printf("ack message: %v", err)
}
}
}