-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
38 lines (34 loc) · 1.15 KB
/
main.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
package main
import (
"context"
"log"
"time"
"github.com/heureka/gorabbit"
"github.com/heureka/gorabbit/consumer"
amqp "github.com/rabbitmq/amqp091-go"
)
func main() {
cons, err := gorabbit.NewConsumer(
"amqp://localhost:5672",
"example-queue",
consumer.WithConsumerTag("example-consumer"), // set up custom consumer tag
consumer.WithArgs(amqp.Table{"example": "tag"}), // add additional consumer tags
consumer.WithAutoAck(), // automatically ACK all messages
consumer.WithExclusive(), // tell the server to ensure that this is the sole consumer from this queue
consumer.WithNoWait(), // tell the server to immediately begin deliveries
)
// see also examples/one and examples/batch for examples of prepared ProcessFunc.
err = cons.Start(context.Background(), consumer.ProcessFunc(func(ctx context.Context, deliveries <-chan amqp.Delivery) error {
for d := range deliveries {
log.Println(d.Body)
}
return nil
}))
if err != nil {
log.Panic(err)
}
time.Sleep(10 * time.Second) // consumer for 10 seconds
if err := cons.Stop(); err != nil {
log.Panic(err)
}
}