| 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 |
// Make sure the FULFILLMENT WorkQueue stream exists. |
| 30 |
stream, err := js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{ |
| 31 |
Name: "FULFILLMENT", |
| 32 |
Subjects: []string{"fulfill.>"}, |
| 33 |
Retention: jetstream.WorkQueuePolicy, |
| 34 |
}) |
| 35 |
if err != nil { |
| 36 |
panic(err) |
| 37 |
} |
| 38 |
|
| 39 |
// NATS-DOC-START |
| 40 |
// One unfiltered consumer covers every order in the queue. |
| 41 |
if _, err := stream.CreateConsumer(ctx, jetstream.ConsumerConfig{ |
| 42 |
Durable: "shippers", |
| 43 |
AckPolicy: jetstream.AckExplicitPolicy, |
| 44 |
}); err != nil { |
| 45 |
panic(err) |
| 46 |
} |
| 47 |
fmt.Println("created unfiltered consumer: shippers") |
| 48 |
|
| 49 |
// A WorkQueue stream delivers each message to exactly one consumer, so it |
| 50 |
// refuses a second consumer whose reach overlaps the first. Two unfiltered |
| 51 |
// consumers both cover fulfill.> , so this is rejected |
| 52 |
// (err 10099: multiple non-filtered consumers not allowed on workqueue stream). |
| 53 |
if _, err := stream.CreateConsumer(ctx, jetstream.ConsumerConfig{ |
| 54 |
Durable: "eu-shippers", |
| 55 |
AckPolicy: jetstream.AckExplicitPolicy, |
| 56 |
}); err != nil { |
| 57 |
fmt.Printf("second unfiltered consumer rejected: %v\n", err) |
| 58 |
} |
| 59 |
|
| 60 |
// Drop the catch-all consumer so we can split the queue by region instead. |
| 61 |
if err := stream.DeleteConsumer(ctx, "shippers"); err != nil { |
| 62 |
panic(err) |
| 63 |
} |
| 64 |
|
| 65 |
// Filtered consumers are allowed as long as their subjects don't overlap. |
| 66 |
// us-shippers takes fulfill.us, eu-shippers takes fulfill.eu, and each |
| 67 |
// order still goes to exactly one worker. |
| 68 |
if _, err := stream.CreateConsumer(ctx, jetstream.ConsumerConfig{ |
| 69 |
Durable: "us-shippers", |
| 70 |
FilterSubject: "fulfill.us", |
| 71 |
AckPolicy: jetstream.AckExplicitPolicy, |
| 72 |
}); err != nil { |
| 73 |
panic(err) |
| 74 |
} |
| 75 |
if _, err := stream.CreateConsumer(ctx, jetstream.ConsumerConfig{ |
| 76 |
Durable: "eu-shippers", |
| 77 |
FilterSubject: "fulfill.eu", |
| 78 |
AckPolicy: jetstream.AckExplicitPolicy, |
| 79 |
}); err != nil { |
| 80 |
panic(err) |
| 81 |
} |
| 82 |
fmt.Println("created filtered consumers: us-shippers (fulfill.us), eu-shippers (fulfill.eu)") |
| 83 |
// NATS-DOC-END |
| 84 |
} |