Skip to content

Commit dde7c17

Browse files
authored
Simplification Tuning (#926)
1 parent bd1e460 commit dde7c17

File tree

13 files changed

+50
-57
lines changed

13 files changed

+50
-57
lines changed

src/examples/java/io/nats/examples/jetstream/NatsJsUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@ public static int count408s(List<Message> messages) {
419419
return count;
420420
}
421421

422+
public static void createCleanMemStream(Connection nc, String stream, String... subs) throws IOException, JetStreamApiException {
423+
createCleanMemStream(nc.jetStreamManagement(), stream, subs);
424+
}
425+
422426
public static void createCleanMemStream(JetStreamManagement jsm, String stream, String... subs) throws IOException, JetStreamApiException {
423427
try {
424428
jsm.deleteStream(stream);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static void main(String[] args) {
5050
System.out.println("S3. " + streamContext.getStreamInfo());
5151

5252
// when you create a consumer from the stream context you get a ConsumerContext in return
53-
ConsumerContext consumerContext = streamContext.addConsumer(ConsumerConfiguration.builder().durable(CONSUMER_NAME).build());
53+
ConsumerContext consumerContext = streamContext.createOrUpdateConsumer(ConsumerConfiguration.builder().durable(CONSUMER_NAME).build());
5454
System.out.println("C1. " + consumerContext.getCachedConsumerInfo());
5555

5656
// get a ConsumerContext from the connection for a pre-existing consumer

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private static void simpleFetch(Connection nc, JetStream js, String label, int m
8181
ConsumerContext consumerContext;
8282
try {
8383
streamContext = nc.streamContext(STREAM);
84-
consumerContext = streamContext.addConsumer(ConsumerConfiguration.builder().durable(consumerName).build());
84+
consumerContext = streamContext.createOrUpdateConsumer(ConsumerConfiguration.builder().durable(consumerName).build());
8585
}
8686
catch (JetStreamApiException | IOException e) {
8787
// JetStreamApiException:
@@ -108,7 +108,8 @@ private static void simpleFetch(Connection nc, JetStream js, String label, int m
108108
// create the consumer then use it
109109
int receivedMessages = 0;
110110
long receivedBytes = 0;
111-
try (FetchConsumer consumer = consumerContext.fetch(fetchConsumeOptions)){
111+
try {
112+
FetchConsumer consumer = consumerContext.fetch(fetchConsumeOptions);
112113
Message msg = consumer.nextMessage();
113114
while (msg != null) {
114115
msg.ack();
@@ -136,9 +137,6 @@ private static void simpleFetch(Connection nc, JetStream js, String label, int m
136137
System.err.println("Exception should be handled properly, just exiting here.");
137138
System.exit(-1);
138139
}
139-
catch (Exception e) {
140-
// For FetchConsumer since it is AutoCloseable
141-
}
142140
long elapsed = System.currentTimeMillis() - start;
143141

144142
printSummary(receivedMessages, receivedBytes, elapsed);

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private static void simpleFetch(Connection nc, JetStream js, String label, int m
7575
ConsumerContext consumerContext;
7676
try {
7777
streamContext = nc.streamContext(STREAM);
78-
consumerContext = streamContext.addConsumer(ConsumerConfiguration.builder().durable(consumerName).build());
78+
consumerContext = streamContext.createOrUpdateConsumer(ConsumerConfiguration.builder().durable(consumerName).build());
7979
}
8080
catch (JetStreamApiException | IOException e) {
8181
// JetStreamApiException:
@@ -97,7 +97,8 @@ private static void simpleFetch(Connection nc, JetStream js, String label, int m
9797

9898
// create the consumer then use it
9999
int receivedMessages = 0;
100-
try (FetchConsumer consumer = consumerContext.fetch(fetchConsumeOptions)){
100+
try {
101+
FetchConsumer consumer = consumerContext.fetch(fetchConsumeOptions);
101102
Message msg = consumer.nextMessage();
102103
while (msg != null) {
103104
msg.ack();
@@ -121,9 +122,6 @@ private static void simpleFetch(Connection nc, JetStream js, String label, int m
121122
System.err.println("Exception should be handled properly, just exiting here.");
122123
System.exit(-1);
123124
}
124-
catch (Exception e) {
125-
// For FetchConsumer since it is AutoCloseable
126-
}
127125
long elapsed = System.currentTimeMillis() - start;
128126

129127
printSummary(receivedMessages, elapsed);

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void main(String[] args) {
4747
ConsumerContext consumerContext;
4848
try {
4949
streamContext = nc.streamContext(STREAM);
50-
consumerContext = streamContext.addConsumer(ConsumerConfiguration.builder().durable(CONSUMER_NAME).build());
50+
consumerContext = streamContext.createOrUpdateConsumer(ConsumerConfiguration.builder().durable(CONSUMER_NAME).build());
5151
}
5252
catch (JetStreamApiException | IOException e) {
5353
// JetStreamApiException:
@@ -61,7 +61,8 @@ public static void main(String[] args) {
6161
Thread consumeThread = new Thread(() -> {
6262
int count = 0;
6363
long start = System.nanoTime();
64-
try (IterableConsumer consumer = consumerContext.consume()){
64+
try {
65+
IterableConsumer consumer = consumerContext.consume();
6566
System.out.println("Starting main loop.");
6667
while (count < STOP_COUNT) {
6768
Message msg = consumer.nextMessage(1000);
@@ -95,9 +96,6 @@ public static void main(String[] args) {
9596
// developer interrupted this thread?
9697
return;
9798
}
98-
catch (Exception e) {
99-
// For IterableConsumer since it is AutoCloseable
100-
}
10199
report("Done", System.nanoTime() - start, count);
102100
});
103101
consumeThread.start();

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import io.nats.client.api.ConsumerConfiguration;
1818

1919
import java.io.IOException;
20-
import java.util.concurrent.*;
20+
import java.util.concurrent.CountDownLatch;
2121
import java.util.concurrent.atomic.AtomicInteger;
2222

2323
import static io.nats.examples.jetstream.simple.Utils.createOrReplaceStream;
@@ -52,7 +52,7 @@ public static void main(String[] args) {
5252
ConsumerContext consumerContext;
5353
try {
5454
streamContext = nc.streamContext(STREAM);
55-
streamContext.addConsumer(ConsumerConfiguration.builder().durable(CONSUMER_NAME).build());
55+
streamContext.createOrUpdateConsumer(ConsumerConfiguration.builder().durable(CONSUMER_NAME).build());
5656
consumerContext = streamContext.consumerContext(CONSUMER_NAME);
5757
}
5858
catch (JetStreamApiException | IOException e) {
@@ -78,17 +78,12 @@ public static void main(String[] args) {
7878
};
7979

8080
// create the consumer then use it
81-
try (MessageConsumer consumer = consumerContext.consume(handler)){
81+
try {
82+
MessageConsumer consumer = consumerContext.consume(handler);
8283
latch.await();
8384
// once the consumer is stopped, the client will drain messages
8485
System.out.println("Stop the consumer...");
85-
CompletableFuture<Boolean> stopFuture = consumer.stop(1000);
86-
try {
87-
stopFuture.get(1, TimeUnit.SECONDS);
88-
}
89-
catch (ExecutionException | TimeoutException e) {
90-
// from the future.get
91-
}
86+
consumer.stop(1000);
9287
}
9388
catch (JetStreamApiException | IOException e) {
9489
// JetStreamApiException:
@@ -98,9 +93,6 @@ public static void main(String[] args) {
9893
// likely a connection problem
9994
return;
10095
}
101-
catch (Exception e) {
102-
// For IterableConsumer since it is AutoCloseable
103-
}
10496

10597
report("Final", start, atomicCount.get());
10698
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static void main(String[] args) {
4242
ConsumerContext consumerContext;
4343
try {
4444
streamContext = nc.streamContext(STREAM);
45-
consumerContext = streamContext.addConsumer(ConsumerConfiguration.builder().durable(CONSUMER_NAME).build());
45+
consumerContext = streamContext.createOrUpdateConsumer(ConsumerConfiguration.builder().durable(CONSUMER_NAME).build());
4646
}
4747
catch (JetStreamApiException | IOException e) {
4848
// JetStreamApiException:

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
import io.nats.client.api.ConsumerInfo;
1717

1818
import java.io.IOException;
19-
import java.util.concurrent.CompletableFuture;
2019

2120
/**
2221
* The MessageConsumer interface is the core interface replacing
2322
* a subscription for a simplified consumer.
2423
* SIMPLIFICATION IS EXPERIMENTAL AND SUBJECT TO CHANGE
2524
*/
26-
public interface MessageConsumer extends AutoCloseable {
25+
public interface MessageConsumer {
2726
/**
2827
* Gets information about the consumer behind this subscription.
2928
* @return consumer information
@@ -35,12 +34,11 @@ public interface MessageConsumer extends AutoCloseable {
3534

3635
/**
3736
* Stop the MessageConsumer from asking for any more messages from the server.
38-
* Messages do not immediately stop
37+
* There still may be messages available and coming across the wire.
3938
* @param timeout The time to wait for the stop to succeed, pass 0 to wait
4039
* forever. Stop involves moving messages to and from the server
4140
* so a very short timeout is not recommended.
42-
* @return A future so you could wait for the stop to know when there are no more messages.
4341
* @throws InterruptedException if one is thrown, in order to propagate it up
4442
*/
45-
CompletableFuture<Boolean> stop(long timeout) throws InterruptedException;
43+
void stop(long timeout) throws InterruptedException;
4644
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ public interface StreamContext {
8484
ConsumerContext consumerContext(String consumerName) throws IOException, JetStreamApiException;
8585

8686
/**
87-
* Management function to creates a consumer on this stream.
87+
* Management function to create or update a consumer on this stream.
8888
* @param config the consumer configuration to use.
8989
* @return a ConsumerContext object
9090
* @throws IOException covers various communication issues with the NATS
9191
* server such as timeout or interruption
9292
* @throws JetStreamApiException the request had an error related to the data
9393
*/
94-
ConsumerContext addConsumer(ConsumerConfiguration config) throws IOException, JetStreamApiException;
94+
ConsumerContext createOrUpdateConsumer(ConsumerConfiguration config) throws IOException, JetStreamApiException;
9595

9696
/**
9797
* Management function to deletes a consumer.

src/main/java/io/nats/client/impl/NatsMessageConsumer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class NatsMessageConsumer extends NatsMessageConsumerBase implements TrackPendin
5454

5555
@Override
5656
public void track(int pendingMessages, long pendingBytes, boolean trackingBytes) {
57-
if (drainFuture == null &&
57+
if (!stopped &&
5858
(pmm.pendingMessages <= thresholdMessages
5959
|| (pmm.trackingBytes && pmm.pendingBytes <= thresholdBytes)))
6060
{

0 commit comments

Comments
 (0)