Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
95 lines (84 loc) · 1.82 KB
/
Copy pathexample_test.go
File metadata and controls
95 lines (84 loc) · 1.82 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
83
84
85
86
87
88
89
90
91
92
93
94
95
package amqpx_test
import (
"context"
"log"
"log/slog"
"os"
"os/signal"
amqpx "github.com/nemith/amqpx"
amqp "github.com/rabbitmq/amqp091-go"
)
func ExampleProducer() {
ctx := context.Background()
client, err := amqpx.NewClient("amqp://localhost:5672")
if err != nil {
slog.Error("failed to create client", "err", err)
}
defer client.Close()
producer, err := client.NewProducer(ctx,
amqpx.WithExchange("my-exchange"),
amqpx.WithDeclaration(
&amqpx.ExchangeDeclaration{
Name: "my-exchange",
Kind: "direct",
},
),
)
if err != nil {
slog.Error("failed to create producer", "err", err)
return
}
defer producer.Close()
msg := amqp.Publishing{
Body: []byte("Hello, World!"),
}
if err := producer.Publish(context.Background(), "key", msg); err != nil {
slog.Error("failed to publish msg", "err", err)
return
}
}
func ExampleConsumer() {
ctx := context.Background()
client, err := amqpx.NewClient("amqp://localhost:5672")
if err != nil {
slog.Error("failed to create client", "err", err)
}
defer client.Close()
consumer, err := client.NewConsumer(ctx,
amqpx.WithQueue("my-queue"),
amqpx.WithAutoAck(true),
amqpx.WithDeclaration(
&amqpx.ExchangeDeclaration{
Name: "my-exchange",
Kind: "direct",
},
&amqpx.QueueDeclaration{Name: "my-queue"},
&amqpx.QueueBinding{
Queue: "my-queue",
Exchange: "my-exchange",
Key: "key",
},
),
)
if err != nil {
slog.Error("failed to create consumer", "err", err)
return
}
defer consumer.Close()
msgs, err := consumer.Consume(ctx)
if err != nil {
slog.Error("failed to consume msgs", "err", err)
return
}
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
Loop:
for {
select {
case msg := <-msgs:
log.Printf("received msg: %s", msg.Body)
case <-stop:
break Loop
}
}
}