Skip to content

Commit 463a167

Browse files
authored
Merge pull request #884 from nats-io/simplification-3
Simplification pre-experimental-release
2 parents 8bb1f66 + a8f758c commit 463a167

17 files changed

+317
-57
lines changed

src/examples/java/io/nats/examples/jetstream/simple/ConsumeManuallyCallNext.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1+
// Copyright 2020-2023 The NATS Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at:
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
114
package io.nats.examples.jetstream.simple;
215

316
import io.nats.client.*;
417
import io.nats.client.api.ConsumerConfiguration;
518

619
import java.time.Duration;
720

21+
import static io.nats.examples.jetstream.simple.Utils.Publisher;
22+
import static io.nats.examples.jetstream.simple.Utils.setupStream;
23+
824
/**
9-
* This example will demonstrate simplified manual consume
25+
* This example will demonstrate simplified manual consume.
26+
* SIMPLIFICATION IS EXPERIMENTAL AND SUBJECT TO CHANGE
1027
*/
1128
public class ConsumeManuallyCallNext {
1229
private static final String STREAM = "simple-stream";
@@ -25,7 +42,7 @@ public static void main(String[] args) {
2542
JetStreamManagement jsm = nc.jetStreamManagement();
2643
JetStream js = nc.jetStream();
2744

28-
Utils.setupStream(jsm, STREAM, SUBJECT);
45+
setupStream(jsm, STREAM, SUBJECT);
2946

3047
String name = "simple-consumer-" + NUID.nextGlobal();
3148

src/examples/java/io/nats/examples/jetstream/simple/ConsumeWithHandler.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2020-2023 The NATS Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at:
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
114
package io.nats.examples.jetstream.simple;
215

316
import io.nats.client.*;
@@ -7,8 +20,12 @@
720
import java.util.concurrent.CountDownLatch;
821
import java.util.concurrent.atomic.AtomicInteger;
922

23+
import static io.nats.examples.jetstream.simple.Utils.Publisher;
24+
import static io.nats.examples.jetstream.simple.Utils.setupStream;
25+
1026
/**
11-
* This example will demonstrate simplified fetch
27+
* This example will demonstrate simplified consume with a handler
28+
* SIMPLIFICATION IS EXPERIMENTAL AND SUBJECT TO CHANGE
1229
*/
1330
public class ConsumeWithHandler {
1431
private static final String STREAM = "simple-stream";
@@ -27,7 +44,7 @@ public static void main(String[] args) {
2744
JetStreamManagement jsm = nc.jetStreamManagement();
2845
JetStream js = nc.jetStream();
2946

30-
Utils.setupStream(jsm, STREAM, SUBJECT);
47+
setupStream(jsm, STREAM, SUBJECT);
3148

3249
String name = "simple-consumer-" + NUID.nextGlobal();
3350

src/examples/java/io/nats/examples/jetstream/simple/FetchExample.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2020-2023 The NATS Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at:
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
114
package io.nats.examples.jetstream.simple;
215

316
import io.nats.client.*;
@@ -7,6 +20,7 @@
720

821
/**
922
* This example will demonstrate simplified fetch
23+
* SIMPLIFICATION IS EXPERIMENTAL AND SUBJECT TO CHANGE
1024
*/
1125
public class FetchExample {
1226
private static final String STREAM = "simple-stream";

src/examples/java/io/nats/examples/jetstream/simple/Publisher.java

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Copyright 2020-2023 The NATS Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at:
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package io.nats.examples.jetstream.simple;
15+
16+
import io.nats.client.*;
17+
import io.nats.client.api.ConsumerConfiguration;
18+
19+
import java.util.concurrent.ThreadLocalRandom;
20+
import java.util.concurrent.atomic.AtomicInteger;
21+
import java.util.concurrent.atomic.AtomicLong;
22+
23+
import static io.nats.examples.jetstream.simple.Utils.Publisher;
24+
import static io.nats.examples.jetstream.simple.Utils.setupStream;
25+
26+
/**
27+
* This example will demonstrate all 3 simplified consumes running at the same time.
28+
* It just runs forever until you manually stop it.
29+
* SIMPLIFICATION IS EXPERIMENTAL AND SUBJECT TO CHANGE
30+
*/
31+
public class ThreeDifferentConsumers {
32+
private static final String STREAM = "simple-stream";
33+
private static final String SUBJECT = "simple-subject";
34+
private static final int REPORT_EVERY = 100;
35+
private static final int JITTER = 30;
36+
37+
// change this is you need to...
38+
public static String SERVER = "nats://localhost:4222";
39+
40+
public static void main(String[] args) {
41+
Options options = Options.builder().server(SERVER).build();
42+
try (Connection nc = Nats.connect(options)) {
43+
44+
JetStreamManagement jsm = nc.jetStreamManagement();
45+
JetStream js = nc.jetStream();
46+
47+
setupStream(jsm, STREAM, SUBJECT);
48+
49+
String name1 = "next-" + NUID.nextGlobal();
50+
String name2 = "handle-" + NUID.nextGlobal();
51+
String name3 = "fetch-" + NUID.nextGlobal();
52+
53+
jsm.addOrUpdateConsumer(STREAM, ConsumerConfiguration.builder().durable(name1).build());
54+
jsm.addOrUpdateConsumer(STREAM, ConsumerConfiguration.builder().durable(name2).build());
55+
jsm.addOrUpdateConsumer(STREAM, ConsumerConfiguration.builder().durable(name3).build());
56+
57+
// Consumer[Context]
58+
ConsumerContext ctx1 = js.getConsumerContext(STREAM, name1);
59+
ConsumerContext ctx2 = js.getConsumerContext(STREAM, name2);
60+
ConsumerContext ctx3 = js.getConsumerContext(STREAM, name3);
61+
62+
// create the consumer then use it
63+
ManualConsumer con1 = ctx1.consume();
64+
65+
Thread con1Thread = new Thread(() -> {
66+
long mark = System.currentTimeMillis();
67+
int count = 0;
68+
long report = randomReportInterval();
69+
try {
70+
while (true) {
71+
Message msg = con1.nextMessage(1000);
72+
if (msg != null) {
73+
msg.ack();
74+
++count;
75+
if (System.currentTimeMillis() - mark > report) {
76+
System.out.println("Manual " + count + " messages.");
77+
mark = System.currentTimeMillis();
78+
report = randomReportInterval();
79+
}
80+
}
81+
}
82+
}
83+
catch (InterruptedException e) {
84+
throw new RuntimeException(e);
85+
}
86+
});
87+
con1Thread.start();
88+
89+
Publisher publisher = new Publisher(js, SUBJECT, JITTER);
90+
Thread pubThread = new Thread(publisher);
91+
pubThread.start();
92+
93+
Thread.sleep(1000); // just makes the consumers be reading different messages
94+
AtomicInteger atomicCount = new AtomicInteger();
95+
AtomicLong atomicMark = new AtomicLong(System.currentTimeMillis());
96+
AtomicLong atomicReport = new AtomicLong(randomReportInterval());
97+
98+
MessageHandler handler = msg -> {
99+
msg.ack();
100+
int count = atomicCount.incrementAndGet();
101+
if (System.currentTimeMillis() - atomicMark.get() > atomicReport.get()) {
102+
System.out.println("Handled " + count + " messages.");
103+
atomicMark.set(System.currentTimeMillis());
104+
atomicReport.set(randomReportInterval());
105+
}
106+
};
107+
SimpleConsumer con2 = ctx2.consume(handler);
108+
109+
Thread.sleep(1000); // just makes the consumers be reading different messages
110+
Thread con2Thread = new Thread(() -> {
111+
int count = 0;
112+
long mark = System.currentTimeMillis();
113+
long report = randomReportInterval();
114+
try {
115+
while (true) {
116+
FetchConsumer fc = ctx3.fetch(REPORT_EVERY);
117+
Message msg = fc.nextMessage();
118+
while (msg != null) {
119+
msg.ack();
120+
++count;
121+
if (System.currentTimeMillis() - mark > report) {
122+
System.out.println("Fetched " + count + " messages.");
123+
mark = System.currentTimeMillis();
124+
report = randomReportInterval();
125+
}
126+
msg = fc.nextMessage();
127+
}
128+
}
129+
}
130+
catch (Exception e) {
131+
throw new RuntimeException(e);
132+
}
133+
});
134+
con2Thread.start();
135+
136+
con2Thread.join(); // never ends so program runs until stopped.
137+
}
138+
catch (Exception e) {
139+
e.printStackTrace();
140+
}
141+
}
142+
143+
private static long randomReportInterval() {
144+
return 1000 + ThreadLocalRandom.current().nextLong(1000);
145+
}
146+
}

src/examples/java/io/nats/examples/jetstream/simple/Utils.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2020-2023 The NATS Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at:
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
114
package io.nats.examples.jetstream.simple;
215

316
import io.nats.client.Connection;
@@ -8,6 +21,8 @@
821
import io.nats.client.api.StorageType;
922

1023
import java.io.IOException;
24+
import java.util.concurrent.ThreadLocalRandom;
25+
import java.util.concurrent.atomic.AtomicBoolean;
1126

1227
import static io.nats.examples.jetstream.NatsJsUtils.createOrReplaceStream;
1328

@@ -63,4 +78,34 @@ public static void setupConsumer(JetStreamManagement jsm, String stream, String
6378
.build();
6479
jsm.addOrUpdateConsumer(stream, cc);
6580
}
81+
82+
public static class Publisher implements Runnable {
83+
private final JetStream js;
84+
private final String subject;
85+
private final int jitter;
86+
private final AtomicBoolean keepGoing = new AtomicBoolean(true);
87+
private int pubNo;
88+
89+
public Publisher(JetStream js, String subject, int jitter) {
90+
this.js = js;
91+
this.subject = subject;
92+
this.jitter = jitter;
93+
}
94+
95+
public void stop() {
96+
keepGoing.set(false);
97+
}
98+
99+
@Override
100+
public void run() {
101+
try {
102+
while (keepGoing.get()) {
103+
Thread.sleep(ThreadLocalRandom.current().nextLong(jitter));
104+
js.publish(subject, ("simple-message-" + (++pubNo)).getBytes());
105+
}
106+
} catch (Exception e) {
107+
throw new RuntimeException(e);
108+
}
109+
}
110+
}
66111
}

src/main/java/io/nats/client/BaseConsumeOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
/**
1919
* Base Consume Options are provided to customize the way the
2020
* consume and fetch operate. It is the base class for FetchConsumeOptions
21+
* SIMPLIFICATION IS EXPERIMENTAL AND SUBJECT TO CHANGE
2122
*/
2223
public class BaseConsumeOptions {
2324
public static final int DEFAULT_MESSAGE_COUNT = 100;

src/main/java/io/nats/client/ConsumeOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
/**
1717
* Consume Options are provided to customize the consume operation.
18+
* SIMPLIFICATION IS EXPERIMENTAL AND SUBJECT TO CHANGE
1819
*/
1920
public class ConsumeOptions extends BaseConsumeOptions {
2021
public static ConsumeOptions DEFAULT_CONSUME_OPTIONS = ConsumeOptions.builder().build();

src/main/java/io/nats/client/ConsumerContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.io.IOException;
1919

2020
/**
21-
* TODO
21+
* SIMPLIFICATION IS EXPERIMENTAL AND SUBJECT TO CHANGE
2222
*/
2323
public interface ConsumerContext {
2424
String getName();

src/main/java/io/nats/client/FetchConsumeOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
/**
1717
* Fetch Consume Options are provided to customize the fetch operation.
18+
* SIMPLIFICATION IS EXPERIMENTAL AND SUBJECT TO CHANGE
1819
*/
1920
public class FetchConsumeOptions extends BaseConsumeOptions {
2021
private FetchConsumeOptions(Builder b) {

0 commit comments

Comments
 (0)