-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathMessageConsumerExample.cs
98 lines (85 loc) · 3.82 KB
/
MessageConsumerExample.cs
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
96
97
98
// Copyright 2023 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Diagnostics;
using System.Threading;
using NATS.Client;
using NATS.Client.JetStream;
namespace NATSExamples
{
internal static class MessageConsumerExample
{
private static readonly string STREAM = "consume-stream";
private static readonly string SUBJECT = "consume-subject";
private static readonly string CONSUMER_NAME = "consume-consumer";
private static readonly string MESSAGE_TEXT = "consume";
private static readonly int STOP_COUNT = 500;
private static readonly int REPORT_EVERY = 100;
private static readonly string SERVER = "nats://localhost:4222";
public static void Main(string[] args)
{
Options opts = ConnectionFactory.GetDefaultOptions(SERVER);
using (IConnection c = new ConnectionFactory().CreateConnection(opts))
{
IJetStreamManagement jsm = c.CreateJetStreamManagementContext();
IJetStream js = c.CreateJetStreamContext();
// set's up the stream and publish data
JsUtils.CreateOrReplaceStream(jsm, STREAM, SUBJECT);
JsUtils.Publish(js, SUBJECT, MESSAGE_TEXT, 2500, false);
// get stream context, create consumer and get the consumer context
IStreamContext streamContext;
IConsumerContext consumerContext;
try
{
streamContext = c.GetStreamContext(STREAM);
consumerContext = streamContext.CreateOrUpdateConsumer(ConsumerConfiguration.Builder().WithDurable(CONSUMER_NAME).Build());
}
catch (Exception)
{
// possible exceptions
// - a connection problem
// - the stream or consumer did not exist
return;
}
CountdownEvent latch = new CountdownEvent(1);
int count = 0;
Stopwatch sw = Stopwatch.StartNew();
EventHandler<MsgHandlerEventArgs> handler = (s, e) => {
e.Message.Ack();
if (++count % REPORT_EVERY == 0) {
report("Handler", sw.ElapsedMilliseconds, count);
}
if (count == STOP_COUNT) {
latch.Signal();
}
};
using (IMessageConsumer consumer = consumerContext.Consume(handler))
{
latch.Wait();
// once the consumer is stopped, the client will drain messages
Console.WriteLine("Stop the consumer...");
consumer.Stop();
// wait until the consumer is finished
while (!consumer.Finished) {
Thread.Sleep(10);
}
}
report("Final", sw.ElapsedMilliseconds, count);
Console.WriteLine();
}
}
private static void report(string label, long elapsed, int count) {
Console.WriteLine($"{label}: Received {count} messages in {elapsed} ms.");
}
}
}