-
Notifications
You must be signed in to change notification settings - Fork 151
/
SimplificationContextExample.cs
70 lines (57 loc) · 3.35 KB
/
SimplificationContextExample.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
// 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 NATS.Client;
using NATS.Client.JetStream;
namespace NATSExamples
{
class SimplificationContextExample
{
private static readonly string STREAM = "context-stream";
private static readonly string SUBJECT = "context-subject";
private static readonly string CONSUMER_NAME = "context-consumer";
public static string SERVER = "nats://localhost:4222";
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 a stream context from the connection
IStreamContext streamContext = c.GetStreamContext(STREAM);
Console.WriteLine("S1. " + streamContext.GetStreamInfo());
// get a stream context from the connection, supplying custom JetStreamOptions
streamContext = c.GetStreamContext(STREAM, JetStreamOptions.Builder().Build());
Console.WriteLine("S2. " + streamContext.GetStreamInfo());
// get a stream context from the JetStream context
streamContext = js.GetStreamContext(STREAM);
Console.WriteLine("S3. " + streamContext.GetStreamInfo());
// when you create a consumer from the stream context you get a ConsumerContext in return
IConsumerContext consumerContext = streamContext.CreateOrUpdateConsumer(ConsumerConfiguration.Builder().WithDurable(CONSUMER_NAME).Build());
Console.WriteLine("C1. " + consumerContext.GetCachedConsumerInfo());
// get a ConsumerContext from the connection for a pre-existing consumer
consumerContext = c.GetConsumerContext(STREAM, CONSUMER_NAME);
Console.WriteLine("C2. " + consumerContext.GetCachedConsumerInfo());
// get a ConsumerContext from the connection for a pre-existing consumer, supplying custom JetStreamOptions
consumerContext = c.GetConsumerContext(STREAM, CONSUMER_NAME, JetStreamOptions.Builder().Build());
Console.WriteLine("C3. " + consumerContext.GetCachedConsumerInfo());
// get a ConsumerContext from the stream context for a pre-existing consumer
consumerContext = streamContext.GetConsumerContext(CONSUMER_NAME);
Console.WriteLine("C4. " + consumerContext.GetCachedConsumerInfo());
}
}
}
}