-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathproducer.go
More file actions
82 lines (71 loc) · 1.74 KB
/
Copy pathproducer.go
File metadata and controls
82 lines (71 loc) · 1.74 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
package main
import (
"flag"
"fmt"
"github.com/assembla/cony"
amqp "github.com/rabbitmq/amqp091-go"
"time"
)
var url = flag.String("url", "amqp://guest:guest@localhost/", "amqp url")
var body = flag.String("body", "Hello world!", "what should be sent")
func showUsageAndStatus() {
fmt.Printf("Producer is running\n\n")
fmt.Println("Flags:")
flag.PrintDefaults()
fmt.Printf("\n\n")
fmt.Println("Publishing:")
fmt.Printf("Body: %q\n", *body)
fmt.Printf("\n\n")
}
func main() {
flag.Parse()
showUsageAndStatus()
// Construct new client with the flag url
// and default backoff policy
cli := cony.NewClient(
cony.URL(*url),
cony.Backoff(cony.DefaultBackoff),
)
// Declare the exchange we'll be using
exc := cony.Exchange{
Name: "myExc",
Kind: "fanout",
AutoDelete: true,
}
cli.Declare([]cony.Declaration{
cony.DeclareExchange(exc),
})
// Declare and register a publisher
// with the cony client
pbl := cony.NewPublisher(exc.Name, "pubSub")
cli.Publish(pbl)
// Launch a go routine and publish a message.
// "Publish" is a blocking method this is why it
// needs to be called in its own go routine.
//
go func() {
ticker := time.NewTicker(time.Second)
for {
select {
case <-ticker.C:
fmt.Printf("Client publishing\n")
err := pbl.Publish(amqp.Publishing{
Body: []byte(*body),
})
if err != nil {
fmt.Printf("Client publish error: %v\n", err)
}
}
}
}()
// Client loop sends out declarations(exchanges, queues, bindings
// etc) to the AMQP server. It also handles reconnecting.
for cli.Loop() {
select {
case err := <-cli.Errors():
fmt.Printf("Client error: %v\n", err)
case blocked := <-cli.Blocking():
fmt.Printf("Client is blocked %v\n", blocked)
}
}
}