-
Notifications
You must be signed in to change notification settings - Fork 46
/
doc_test.go
109 lines (90 loc) · 2.27 KB
/
doc_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
package cony_test
import (
"log"
"os"
"time"
"github.com/assembla/cony"
"github.com/streadway/amqp"
)
func Example() {
client := cony.NewClient(cony.URL(os.Getenv("AMQP_URL")), cony.Backoff(cony.DefaultBackoff))
q := &cony.Queue{
Name: "", // autogenerated queue name
AutoDelete: true,
}
exchange := cony.Exchange{
Name: "amq.topic",
Durable: true,
}
b := cony.Binding{
Queue: q,
Exchange: exchange,
Key: "something.#",
}
// wrap all declarations and save into slice
declarations := []cony.Declaration{
cony.DeclareQueue(q),
cony.DeclareExchange(exchange),
cony.DeclareBinding(b),
}
// declare consumer
consumer := cony.NewConsumer(q,
cony.Qos(10),
cony.AutoTag(),
cony.AutoAck(),
)
// declare publisher
publisher := cony.NewPublisher(exchange.Name,
"ololo.key",
cony.PublishingTemplate(amqp.Publishing{
ContentType: "application/json",
AppId: "app1",
}), // template amqp.Publising
)
// let client know about declarations
client.Declare(declarations)
// let client know about consumers/publishers
client.Consume(consumer)
client.Publish(publisher)
clientErrs := client.Errors()
deliveries := consumer.Deliveries()
consumerErrs := consumer.Errors()
// connect, reconnect, or exit loop
// run network operations such as:
// queue, exchange, bidning, consumers declarations
for client.Loop() {
select {
case msg := <-deliveries:
log.Println(msg)
msg.Ack(false)
publisher.Write([]byte("ololo reply"))
case err := <-consumerErrs:
log.Println("CONSUMER ERROR: ", err)
case err := <-clientErrs:
log.Println("CLIENT ERROR: ", err)
client.Close()
}
}
}
func ExampleURL() {
cony.NewClient(cony.URL("amqp://guest:guest@localhost/"))
}
func ExampleErrorsChan() {
errors := make(chan error, 100) // define custom buffer size
cony.NewClient(cony.ErrorsChan(errors))
}
func ExampleBlockingChan() {
blockings := make(chan amqp.Blocking, 100) // define custom buffer size
cony.NewClient(cony.BlockingChan(blockings))
}
func ExampleClient_Loop() {
client := cony.NewClient(cony.URL("amqp://guest:guest@localhost/"))
for client.Loop() {
select {
case err := <-client.Errors():
log.Println("CLIENT ERROR: ", err)
client.Close()
}
time.Sleep(1 * time.Second) // naive backoff
}
}