forked from nats-io/nats.net.v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IterableConsumerExample.cs
126 lines (113 loc) · 5.31 KB
/
IterableConsumerExample.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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 IterableConsumerExample
{
private static readonly string STREAM = "iterable-stream";
private static readonly string SUBJECT = "iterable-subject";
private static readonly string CONSUMER_NAME = "iterable-consumer";
private static readonly string MESSAGE_TEXT = "iterable";
private static readonly int STOP_COUNT = 500;
private static readonly int REPORT_EVERY = 50;
private static readonly int JITTER = 20;
public static 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);
// 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;
}
Thread consumeThread = new Thread(() =>
{
try
{
int count = 0;
Stopwatch sw = Stopwatch.StartNew();
using (IIterableConsumer consumer = consumerContext.Iterate())
{
Msg msg;
Console.WriteLine("Starting main loop.");
while (count < STOP_COUNT)
{
msg = consumer.NextMessage(1000);
msg.Ack();
if (++count % REPORT_EVERY == 0)
{
report("Main Loop Running", sw.ElapsedMilliseconds, count);
}
}
report("Main Loop Stopped", sw.ElapsedMilliseconds, count);
// The consumer has at least 1 pull request active. When stop is called,
// no more pull requests will be made, but messages already requested
// will still come across the wire to the client.
consumer.Stop();
Console.WriteLine("Starting post-stop loop.");
msg = consumer.NextMessage(1000);
while (msg != null)
{
msg.Ack();
if (++count % REPORT_EVERY == 0)
{
report("Post-stop loop running", sw.ElapsedMilliseconds, count);
}
msg = consumer.NextMessage(1000);
}
}
report("Post-stop loop stopped", sw.ElapsedMilliseconds, count);
}
catch (NATSJetStreamStatusException)
{
// Either the consumer was deleted in the middle
// of the pull or there is a new status from the
// server that this client is not aware of
}
});
consumeThread.Start();
Publisher publisher = new Publisher(js, SUBJECT, MESSAGE_TEXT, JITTER);
Thread pubThread = new Thread(publisher.run);
pubThread.Start();
consumeThread.Join();
publisher.StopPublishing();
pubThread.Join();
}
}
private static void report(string label, long elapsed, int count) {
Console.WriteLine($"{label}: Received {count} messages in {elapsed} ms.");
}
}
}