Debian Codegraph @

examples/docs/learn-jetstream-acknowledgment-nakWithDelay/main.go

raw
1 package main
2
3 import (
4 "context"
5 "fmt"
6 "time"
7
8 "github.com/nats-io/nats.go"
9 "github.com/nats-io/nats.go/jetstream"
10 )
11
12 func main() {
13 // Connect to the NATS server.
14 nc, err := nats.Connect("nats://localhost:4222")
15 if err != nil {
16 panic(err)
17 }
18 defer nc.Close()
19
20 // Create a JetStream context (the stream manager).
21 js, err := jetstream.New(nc)
22 if err != nil {
23 panic(err)
24 }
25
26 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
27 defer cancel()
28
29 // NATS-DOC-START
30 // Bind to the durable "shipping" consumer created earlier.
31 cons, err := js.Consumer(ctx, "ORDERS", "shipping")
32 if err != nil {
33 panic(err)
34 }
35
36 // Fetch a single message from the consumer.
37 msg, err := cons.Next()
38 if err != nil {
39 panic(err)
40 }
41 fmt.Printf("Received on %s: %s\n", msg.Subject(), string(msg.Data()))
42
43 // Negatively acknowledge with a delay. The server holds the message and
44 // redelivers it after the delay instead of right away, which is useful when
45 // a downstream dependency needs time to recover.
46 if err := msg.NakWithDelay(10 * time.Second); err != nil {
47 panic(err)
48 }
49 fmt.Println("Message NAK'd, redelivery delayed by 10s")
50 // NATS-DOC-END
51 }