This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 5fe48cb0893d CAMEL-26915: camel-nats - each start of the consumer has
its own run, so the task of a stopped start does not take over the next one
(#29998)
5fe48cb0893d is described below
commit 5fe48cb0893d8f109a7f821d077f2678fe7886c9
Author: allthingssecurity <[email protected]>
AuthorDate: Sun Nov 15 14:14:43 2026 +0530
CAMEL-26915: camel-nats - each start of the consumer has its own run, so
the task of a stopped start does not take over the next one (#29998)
NatsConsumer subscribes on a task that checked isRunAllowed(), the state of
the consumer rather than of one start. When the same consumer was stopped and
started again (as route policies such as ThrottlingInflightRoutePolicy do)
while the task of the first start was still subscribing, the old task kept its
subscription and overwrote the dispatcher and subscription of the new start, so
one of them was never ended and a shared connection could deliver each message
twice.
Each start now creates a NatsConsumer.Run that holds what its task makes; a
task whose run is no longer current ends what it made itself, doStop clears the
current run first, and NatsJetStreamPushConsumerKeeper follows the run of its
start. New NatsConsumerRestartTest covers a pull subscription and a JetStream
push dispatcher of a stopped start.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../apache/camel/component/nats/NatsConsumer.java | 138 +++++++-----
.../nats/NatsJetStreamPushConsumerKeeper.java | 17 +-
.../component/nats/NatsConsumerReplyTest.java | 3 +-
.../component/nats/NatsConsumerRestartTest.java | 237 +++++++++++++++++++++
4 files changed, 337 insertions(+), 58 deletions(-)
diff --git
a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java
b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java
index a346e298b56a..68ace2e4475e 100644
---
a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java
+++
b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java
@@ -64,15 +64,10 @@ public class NatsConsumer extends DefaultConsumer {
private final Processor processor;
private ExecutorService executor;
private Connection connection;
- // set by the consuming task and read when the consumer stops
- private volatile Dispatcher dispatcher;
- // the message handler of the dispatcher, which keeps the messages in
flight of that dispatcher (one per start)
- private volatile NatsConsumingTask.CamelNatsMessageHandler
dispatcherHandler;
private boolean active;
- // set by the consuming task, and again by the push consumer keeper, and
read when the consumer stops
- private volatile JetStreamSubscription jetStreamSubscription;
- // set by the consuming task and read when the consumer stops
- private volatile NatsJetStreamPushConsumerKeeper pushConsumerKeeper;
+ // the run of the current start, null while the consumer is stopped: its
consuming task sets what it made in it, and
+ // the stop of that start ends it
+ private volatile Run currentRun;
public NatsConsumer(NatsEndpoint endpoint, Processor processor) {
super(endpoint, processor);
@@ -107,7 +102,9 @@ public class NatsConsumer extends DefaultConsumer {
LOG.warn(
"exchangePattern=InOut has no effect on JetStream
messages: replies are for core NATS only, as the reply subject of a JetStream
message is its ack subject");
}
- this.executor.submit(new NatsConsumingTask(this.connection, config));
+ Run run = new Run(this.executor);
+ this.currentRun = run;
+ this.executor.submit(new NatsConsumingTask(this.connection, config,
run));
}
@Override
@@ -116,10 +113,14 @@ public class NatsConsumer extends DefaultConsumer {
// an interrupt during the flush is kept until the end: jnats drops a
message to the server (an UNSUB) that is
// queued on an interrupted thread
boolean interrupted = false;
-
- if (this.pushConsumerKeeper != null) {
- this.pushConsumerKeeper.stop();
- this.pushConsumerKeeper = null;
+ // the task of this run sees from now on that its run ended: what it
sets in the run after this is either seen
+ // below, or ended by the task itself
+ final Run run = this.currentRun;
+ this.currentRun = null;
+
+ final NatsJetStreamPushConsumerKeeper keeper = run != null ?
run.pushConsumerKeeper : null;
+ if (keeper != null) {
+ keeper.stop();
}
if (configuration.isFlushConnection() &&
ObjectHelper.isNotEmpty(this.connection)) {
@@ -139,7 +140,7 @@ public class NatsConsumer extends DefaultConsumer {
}
}
- final Dispatcher stoppedDispatcher = this.dispatcher;
+ final Dispatcher stoppedDispatcher = run != null ? run.dispatcher :
null;
if (stoppedDispatcher != null) {
try {
stoppedDispatcher.unsubscribe(configuration.getTopic());
@@ -149,7 +150,7 @@ public class NatsConsumer extends DefaultConsumer {
// a JetStream push subscription belongs to the dispatcher (it
cannot unsubscribe directly) and is on its
// deliver subject, not on the topic: end it through the
dispatcher, which matters when the connection is
// shared and stays open. This does not interrupt the dispatcher
thread, which may still run an exchange
- JetStreamSubscription pushSubscription =
this.jetStreamSubscription;
+ JetStreamSubscription pushSubscription = run.jetStreamSubscription;
if (pushSubscription != null && pushSubscription.isActive()) {
try {
stoppedDispatcher.unsubscribe(pushSubscription);
@@ -162,7 +163,7 @@ public class NatsConsumer extends DefaultConsumer {
// in flight
final Connection stoppedConnection = this.connection;
Runnable close = () -> closeDispatcher(stoppedConnection,
stoppedDispatcher);
- NatsConsumingTask.CamelNatsMessageHandler handler =
this.dispatcherHandler;
+ NatsConsumingTask.CamelNatsMessageHandler handler =
run.dispatcherHandler;
if (handler != null) {
// the handler of this dispatcher only: a message of an
earlier run that is still in flight (after a
// forced stop) closes the dispatcher of its own run
@@ -170,11 +171,9 @@ public class NatsConsumer extends DefaultConsumer {
} else {
close.run();
}
- this.dispatcher = null;
- this.dispatcherHandler = null;
}
- final JetStreamSubscription stoppedSubscription =
this.jetStreamSubscription;
+ final JetStreamSubscription stoppedSubscription = run != null ?
run.jetStreamSubscription : null;
if (ObjectHelper.isNotEmpty(stoppedSubscription) &&
stoppedSubscription.isActive()) {
try {
stoppedSubscription.unsubscribe();
@@ -247,10 +246,44 @@ public class NatsConsumer extends DefaultConsumer {
this.active = active;
}
+ /**
+ * One start of the consumer, and what its consuming task made for it,
which the stop of that start ends.
+ * <p>
+ * The same consumer can be stopped and started again (a route policy does
so, such as the throttling policies, as
+ * this consumer is not suspendable) while the task of the first start is
still in its subscribe: the task then sees
+ * that its run is no longer the current one, ends what it made itself,
and does not touch the run of the new start.
+ * The task writes each field before it checks {@link #isCurrent()}, and
the stop clears the current run before it
+ * reads the fields (all volatile): so either the task sees the stop, or
the stop sees what the task made (or both,
+ * and both end it).
+ */
+ final class Run {
+ // the executor of this start, which the push consumer keeper of this
run uses
+ private final ExecutorService executor;
+ volatile Dispatcher dispatcher;
+ // the message handler of the dispatcher, which keeps the messages in
flight of that dispatcher
+ volatile NatsConsumingTask.CamelNatsMessageHandler dispatcherHandler;
+ // set by the consuming task, and again by the push consumer keeper or
the pull subscribe again
+ volatile JetStreamSubscription jetStreamSubscription;
+ volatile NatsJetStreamPushConsumerKeeper pushConsumerKeeper;
+
+ Run(ExecutorService executor) {
+ this.executor = executor;
+ }
+
+ /**
+ * Whether this run is the one of the current start and the consumer
runs: false once the consumer stops, and
+ * stays false after it is started again.
+ */
+ boolean isCurrent() {
+ return currentRun == this && isRunAllowed();
+ }
+ }
+
class NatsConsumingTask implements Runnable {
private final Connection connection;
private final NatsConfiguration configuration;
+ private final Run run;
// stream sequences of the JetStream messages fetched by the pull loop
and not acknowledged yet, and the highest
// fetched: where a new ephemeral consumer must start when the server
deleted the one this consumer used
private final NavigableSet<Long> unacknowledged = new
ConcurrentSkipListSet<>();
@@ -264,9 +297,10 @@ public class NatsConsumer extends DefaultConsumer {
// whether the WARN that the durable consumer is gone was logged, so
that it is logged once until it is back
private boolean durableGoneReported;
- NatsConsumingTask(Connection connection, NatsConfiguration
configuration) {
+ NatsConsumingTask(Connection connection, NatsConfiguration
configuration, Run run) {
this.connection = connection;
this.configuration = configuration;
+ this.run = run;
}
@Override
@@ -287,9 +321,9 @@ public class NatsConsumer extends DefaultConsumer {
setupStandardNatsConsumer(topic, queueName, maxMessages);
}
} catch (final Exception e) {
- if (!NatsConsumer.this.isRunAllowed()) {
- // the consumer stopped meanwhile (its stop closes the
dispatcher that a JetStream push subscribe
- // uses, which then fails): nothing to report
+ if (!run.isCurrent()) {
+ // the consumer stopped meanwhile, and may have started
again (its stop closes the dispatcher that a
+ // JetStream push subscribe uses, which then fails):
nothing to report
LOG.debug("Not consuming from topic {} as the consumer is
stopping",
getEndpoint().getConfiguration().getTopic(), e);
} else {
@@ -361,20 +395,20 @@ public class NatsConsumer extends DefaultConsumer {
JetStreamSubscription pullSubscription =
this.connection.jetStream().subscribe(
NatsConsumer.this.getEndpoint().getConfiguration().getTopic(),
pullOptions);
- NatsConsumer.this.jetStreamSubscription = pullSubscription;
- if (!NatsConsumer.this.isRunAllowed()) {
- // stopped while subscribing: doStop may have run before
the subscription was set, and then did
- // not end it
+ run.jetStreamSubscription = pullSubscription;
+ if (!run.isCurrent()) {
+ // stopped while subscribing (and maybe started again):
doStop may have run before the subscription
+ // was set, and then did not end it
unsubscribeStopped(pullSubscription);
return;
}
NatsConsumer.this.setActive(true);
- runPullFetchLoop(NatsConsumer.this.jetStreamSubscription,
messageHandler, cc);
+ runPullFetchLoop(pullSubscription, messageHandler, cc);
} else {
Dispatcher pushDispatcher =
this.connection.createDispatcher(messageHandler);
- NatsConsumer.this.dispatcher = pushDispatcher;
- NatsConsumer.this.dispatcherHandler = messageHandler;
+ run.dispatcher = pushDispatcher;
+ run.dispatcherHandler = messageHandler;
if (stoppedBeforeSubscribe(pushDispatcher)) {
return;
}
@@ -394,23 +428,24 @@ public class NatsConsumer extends DefaultConsumer {
// more than its inactive threshold, server restart); a
durable consumer is not created again
NatsJetStreamPushConsumerKeeper keeper = new
NatsJetStreamPushConsumerKeeper(
NatsConsumer.this, this.connection, streamName,
configuration.getTopic(), queueName,
- pushDispatcher, messageHandler, cc,
NatsConsumer.this.executor,
- sub -> NatsConsumer.this.jetStreamSubscription = sub);
- NatsConsumer.this.jetStreamSubscription =
this.connection.jetStream().subscribe(
+ pushDispatcher, messageHandler, cc, run.executor,
run::isCurrent,
+ sub -> run.jetStreamSubscription = sub);
+ JetStreamSubscription pushSubscription =
this.connection.jetStream().subscribe(
NatsConsumer.this.getEndpoint().getConfiguration().getTopic(),
queueName,
pushDispatcher,
keeper,
autoAck,
pushOptions);
- NatsConsumer.this.pushConsumerKeeper = keeper;
- keeper.start(NatsConsumer.this.jetStreamSubscription,
+ run.jetStreamSubscription = pushSubscription;
+ run.pushConsumerKeeper = keeper;
+ keeper.start(pushSubscription,
streamInfo != null ? streamInfo.getCreateTime() : null,
streamInfo != null && streamInfo.getStreamState() !=
null
?
streamInfo.getStreamState().getLastSequence() : -1);
- if (!NatsConsumer.this.isRunAllowed()) {
- // stopped while subscribing: doStop did not see the
keeper (it saw the dispatcher, and ends the
- // subscription through it)
+ if (!run.isCurrent()) {
+ // stopped while subscribing: doStop did not see the
keeper (it saw the dispatcher, as this run was
+ // current after the dispatcher was set, and ends the
subscription through it)
keeper.stop();
return;
}
@@ -443,7 +478,7 @@ public class NatsConsumer extends DefaultConsumer {
JetStreamSubscription sub = subscription;
int batchSize = configuration.getPullBatchSize();
Duration fetchTimeout =
Duration.ofMillis(configuration.getPullFetchTimeout());
- while (NatsConsumer.this.isRunAllowed() &&
!Thread.currentThread().isInterrupted()) {
+ while (run.isCurrent() && !Thread.currentThread().isInterrupted())
{
long fetchStarted = System.nanoTime();
try {
List<Message> messages = sub.fetch(batchSize,
fetchTimeout);
@@ -464,7 +499,7 @@ public class NatsConsumer extends DefaultConsumer {
}
durableGoneReported = false;
for (Message msg : messages) {
- if (!NatsConsumer.this.isRunAllowed()) {
+ if (!run.isCurrent()) {
break;
}
try {
@@ -627,8 +662,8 @@ public class NatsConsumer extends DefaultConsumer {
JetStreamSubscription sub =
this.connection.jetStream().subscribe(configuration.getTopic(),
PullSubscribeOptions.builder().configuration(again).build());
unacknowledged.clear();
- NatsConsumer.this.jetStreamSubscription = sub;
- if (!NatsConsumer.this.isRunAllowed()) {
+ run.jetStreamSubscription = sub;
+ if (!run.isCurrent()) {
// stopping: doStop may have unsubscribed the old subscription
before this one was assigned, and the
// stop interrupts this thread
unsubscribeStopped(sub);
@@ -658,15 +693,16 @@ public class NatsConsumer extends DefaultConsumer {
}
/**
- * Whether the consumer stopped before this run subscribed with the
dispatcher it just set; if so, closes that
- * dispatcher, which has no subscription yet. The dispatcher field is
volatile and written before the run state
- * is read here, and a stop sets the state before doStop reads the
field: so either this run sees the stop, or
- * doStop sees the dispatcher and closes it (on the stopping thread,
and a subscribe on the closed dispatcher
- * fails). Closing it here, before any subscribe, matters: doStop
interrupts this thread, and jnats drops the
- * UNSUB of a close on an interrupted thread, so the server would keep
a subscription that a later close ended.
+ * Whether the run ended (the consumer stopped, and may have started
again) before it subscribed with the
+ * dispatcher it just set; if so, closes that dispatcher, which has no
subscription yet. The dispatcher field of
+ * the run is volatile and written before the run is checked here, and
a stop clears the current run before
+ * doStop reads the field: so either this run sees the stop, or doStop
sees the dispatcher and closes it (on the
+ * stopping thread, and a subscribe on the closed dispatcher fails).
Closing it here, before any subscribe,
+ * matters: doStop interrupts this thread, and jnats drops the UNSUB
of a close on an interrupted thread, so the
+ * server would keep a subscription that a later close ended.
*/
private boolean stoppedBeforeSubscribe(Dispatcher runDispatcher) {
- if (NatsConsumer.this.isRunAllowed()) {
+ if (run.isCurrent()) {
return false;
}
LOG.debug("Not subscribing to topic {} as the consumer is
stopping", configuration.getTopic());
@@ -677,8 +713,8 @@ public class NatsConsumer extends DefaultConsumer {
private void setupStandardNatsConsumer(String topic, String queueName,
Integer maxMessages) {
CamelNatsMessageHandler messageHandler = new
CamelNatsMessageHandler();
Dispatcher standardDispatcher =
connection.createDispatcher(messageHandler);
- NatsConsumer.this.dispatcher = standardDispatcher;
- NatsConsumer.this.dispatcherHandler = messageHandler;
+ run.dispatcher = standardDispatcher;
+ run.dispatcherHandler = messageHandler;
if (stoppedBeforeSubscribe(standardDispatcher)) {
return;
}
diff --git
a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsJetStreamPushConsumerKeeper.java
b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsJetStreamPushConsumerKeeper.java
index 25bd9a1eb984..ac7a0d362961 100644
---
a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsJetStreamPushConsumerKeeper.java
+++
b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsJetStreamPushConsumerKeeper.java
@@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import io.nats.client.Connection;
@@ -92,6 +93,9 @@ final class NatsJetStreamPushConsumerKeeper implements
MessageHandler, Connectio
private final MessageHandler handler;
private final ConsumerConfiguration configuration;
private final ExecutorService executor;
+ // whether the run of the consumer that made this keeper is still the
current one (the consumer may have been
+ // stopped and started again, with a keeper of its own)
+ private final BooleanSupplier running;
private final Consumer<JetStreamSubscription> onSubscribed;
private final Lock lock = new ReentrantLock();
// whether a check is submitted and has not started yet, so that
reconnects do not queue several checks
@@ -116,7 +120,7 @@ final class NatsJetStreamPushConsumerKeeper implements
MessageHandler, Connectio
NatsJetStreamPushConsumerKeeper(NatsConsumer consumer, Connection
connection, String streamName, String topic,
String queueName, Dispatcher dispatcher,
MessageHandler handler,
ConsumerConfiguration configuration,
ExecutorService executor,
- Consumer<JetStreamSubscription>
onSubscribed) {
+ BooleanSupplier running,
Consumer<JetStreamSubscription> onSubscribed) {
this.consumer = consumer;
this.connection = connection;
this.streamName = streamName;
@@ -126,6 +130,7 @@ final class NatsJetStreamPushConsumerKeeper implements
MessageHandler, Connectio
this.handler = handler;
this.configuration = configuration;
this.executor = executor;
+ this.running = running;
this.onSubscribed = onSubscribed;
this.trackUnacknowledged = configuration.getAckPolicy() !=
AckPolicy.None;
this.maxDeliver = configuration.getMaxDeliver();
@@ -164,7 +169,7 @@ final class NatsJetStreamPushConsumerKeeper implements
MessageHandler, Connectio
@Override
public void connectionEvent(Connection conn, Events type) {
// jnats has subscribed the deliver subject again: is the consumer
still there?
- if (type == Events.RESUBSCRIBED && consumer.isRunAllowed() &&
checkQueued.compareAndSet(false, true)) {
+ if (type == Events.RESUBSCRIBED && running.getAsBoolean() &&
checkQueued.compareAndSet(false, true)) {
try {
executor.submit(this::checkConsumer);
} catch (Exception e) {
@@ -190,7 +195,7 @@ final class NatsJetStreamPushConsumerKeeper implements
MessageHandler, Connectio
checkQueued.set(false);
long delay = CHECK_RETRY_DELAY_MILLIS;
boolean reported = false;
- for (int attempt = 1; consumer.isRunAllowed(); attempt++) {
+ for (int attempt = 1; running.getAsBoolean(); attempt++) {
Boolean gone = consumerGone();
if (Boolean.FALSE.equals(gone)) {
if (attempt > CHECK_ATTEMPTS) {
@@ -265,14 +270,14 @@ final class NatsJetStreamPushConsumerKeeper implements
MessageHandler, Connectio
configuration.getDurable(), topic, goneReason);
return true;
}
- if (!consumer.isRunAllowed()) {
+ if (!running.getAsBoolean()) {
return true;
}
try {
subscribeAgain();
return true;
} catch (Exception e) {
- if (!consumer.isRunAllowed()) {
+ if (!running.getAsBoolean()) {
// the consumer stopped meanwhile (its stop can close the
dispatcher): nothing to report
LOG.debug("Not subscribing again as the consumer of topic {}
is stopping", topic, e);
return true;
@@ -365,7 +370,7 @@ final class NatsJetStreamPushConsumerKeeper implements
MessageHandler, Connectio
} catch (Exception e) {
LOG.debug("Error unsubscribing the push subscription of the
deleted consumer", e);
}
- if (!consumer.isRunAllowed()) {
+ if (!running.getAsBoolean()) {
// the consumer was stopped while subscribing: its stop may have
ended the previous subscription only
try {
dispatcher.unsubscribe(subscription);
diff --git
a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerReplyTest.java
b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerReplyTest.java
index cb8b6e56f8ec..0246b9800dc2 100644
---
a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerReplyTest.java
+++
b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerReplyTest.java
@@ -115,7 +115,8 @@ public class NatsConsumerReplyTest extends CamelTestSupport
{
handledErrors.add(exception);
}
});
- consumer.new NatsConsumingTask(connection,
endpoint.getConfiguration()).new CamelNatsMessageHandler()
+ consumer.new NatsConsumingTask(
+ connection, endpoint.getConfiguration(), consumer.new
Run(null)).new CamelNatsMessageHandler()
.onMessage(message);
}
diff --git
a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerRestartTest.java
b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerRestartTest.java
new file mode 100644
index 000000000000..9e336585112a
--- /dev/null
+++
b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerRestartTest.java
@@ -0,0 +1,237 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.camel.component.nats;
+
+import java.time.Duration;
+import java.time.ZonedDateTime;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import io.nats.client.Connection;
+import io.nats.client.JetStream;
+import io.nats.client.JetStreamManagement;
+import io.nats.client.JetStreamSubscription;
+import io.nats.client.PullSubscribeOptions;
+import io.nats.client.api.ServerInfo;
+import io.nats.client.api.StreamInfo;
+import io.nats.client.support.DateTimeUtils;
+import io.nats.client.support.JsonParser;
+import org.apache.camel.Consumer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.support.service.ServiceHelper;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static
org.apache.camel.component.nats.NatsConsumerPullConsumerDeletedTest.FakeServer.proxy;
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A NATS consumer that is stopped and started again while the consuming task
of the first start is still in its
+ * subscribe: when that subscribe returns, the task of the first start must
end what it made and leave the second start
+ * alone, and the next stop must end what the second start made.
+ * <p>
+ * A route policy stops the consumer of its route and starts the same consumer
again (the throttling policies suspend or
+ * stop it and resume or start it, and the NATS consumer is not suspendable).
A route that is stopped and started again
+ * (or suspended and resumed) gets a new consumer instead.
+ */
+class NatsConsumerRestartTest extends CamelTestSupport {
+
+ NatsConsumerRestartTest() {
+ testConfiguration().withUseRouteBuilder(false);
+ }
+
+ @Test
+ void pullSubscriptionOfTheStoppedStartIsEndedAndTheNewOneIsKept() throws
Exception {
+ FakePullSubscription first = new FakePullSubscription();
+ FakePullSubscription second = new FakePullSubscription();
+ CountDownLatch firstSubscribing = new CountDownLatch(1);
+ CountDownLatch firstSubscribeDone = new CountDownLatch(1);
+ context.getRegistry().bind("conn", pullConnection(first, second,
firstSubscribing, firstSubscribeDone));
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
from("nats:orders?connection=#conn&jetstreamEnabled=true&jetstreamName=ORDERS&flushConnection=false")
+ .routeId("orders").to("mock:result");
+ }
+ });
+ context.start();
+ assertTrue(firstSubscribing.await(10, TimeUnit.SECONDS));
+
+ // a quick stop and start of the consumer, as a route policy does,
while the subscribe of the first start is in
+ // flight
+ restartConsumer();
+ await().atMost(10, TimeUnit.SECONDS).until(() -> second.fetches.get()
> 0);
+
+ // the subscribe of the first start returns now: the consumer runs
again, but not for that start
+ firstSubscribeDone.countDown();
+
+ await().atMost(10, TimeUnit.SECONDS).untilAsserted(() ->
assertEquals(1, first.unsubscribes.get(),
+ "the subscription made for the stopped start must be ended"));
+ assertFalse(first.unsubscribedOnInterruptedThread.get(), "the
unsubscribe must not run on an interrupted thread");
+ assertEquals(0, first.fetches.get(), "the task of the stopped start
must not fetch");
+ assertTrue(second.active.get(), "the subscription of the new start
must be kept");
+
+ context.getRouteController().stopRoute("orders");
+
+ assertEquals(1, second.unsubscribes.get(), "the stop must end the
subscription of the new start");
+ assertEquals(1, first.unsubscribes.get());
+ }
+
+ @Test
+ void dispatcherOfTheStoppedStartIsClosedAndTheNewOneIsKept() throws
Exception {
+ NatsConsumerJetStreamPushStopTest.FakeServer server = new
NatsConsumerJetStreamPushStopTest.FakeServer();
+ CountDownLatch firstCreating = new CountDownLatch(1);
+ CountDownLatch firstCreateDone = new CountDownLatch(1);
+ AtomicInteger creates = new AtomicInteger();
+ server.afterCreateDispatcher = () -> {
+ if (creates.incrementAndGet() == 1) {
+ // the task of the first start gets its dispatcher only after
the stop and the next start
+ firstCreating.countDown();
+ awaitUninterruptibly(firstCreateDone);
+ }
+ };
+ context.getRegistry().bind("conn", server.connection());
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
from("nats:orders?connection=#conn&jetstreamEnabled=true&jetstreamName=ORDERS&pullSubscription=false")
+ .routeId("orders").to("mock:result");
+ }
+ });
+ context.start();
+ assertTrue(firstCreating.await(10, TimeUnit.SECONDS));
+
+ // a quick stop and start of the consumer, as a route policy does,
while the first start is creating its
+ // dispatcher
+ restartConsumer();
+ await().atMost(10, TimeUnit.SECONDS).until(() ->
server.activeSubscriptions() == 1);
+
+ // the task of the first start gets its dispatcher now: the consumer
runs again, but not for that start
+ firstCreateDone.countDown();
+
+ await().atMost(10, TimeUnit.SECONDS).untilAsserted(() ->
assertEquals(1, server.closedDispatchers(),
+ "the dispatcher made for the stopped start must be closed"));
+ assertEquals(1, server.activeSubscriptions(), "the subscription of the
new start must be kept");
+
+ // the new start still consumes
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+ server.publish("a");
+ mock.assertIsSatisfied();
+
+ context.getRouteController().stopRoute("orders");
+
+ // the stop ends the subscription of the new start and closes its
dispatcher
+ assertEquals(0, server.activeSubscriptions());
+ await().atMost(10, TimeUnit.SECONDS).untilAsserted(() ->
assertEquals(2, server.closedDispatchers(),
+ "the stop must close the dispatcher of the new start"));
+ }
+
+ private void restartConsumer() throws Exception {
+ Consumer consumer = context.getRoute("orders").getConsumer();
+ ServiceHelper.stopService(consumer);
+ ServiceHelper.startService(consumer);
+ }
+
+ private static Connection pullConnection(
+ FakePullSubscription first, FakePullSubscription second,
CountDownLatch firstSubscribing,
+ CountDownLatch firstSubscribeDone) {
+ StreamInfo streamInfo = new StreamInfo(
+
JsonParser.parseUnchecked("{\"config\":{\"name\":\"ORDERS\",\"subjects\":[\"orders\"]},\"created\":\""
+ +
DateTimeUtils.toRfc3339(ZonedDateTime.now()) + "\",\"state\":{}}"));
+ JetStreamManagement jsm = proxy(JetStreamManagement.class, (name,
args) -> switch (name) {
+ case "getStreamInfo", "addStream" -> streamInfo;
+ default -> null;
+ });
+ AtomicInteger subscribes = new AtomicInteger();
+ JetStream js = proxy(JetStream.class, (name, args) -> {
+ if ("subscribe".equals(name) && args.length == 2 && args[1]
instanceof PullSubscribeOptions) {
+ if (subscribes.incrementAndGet() > 1) {
+ return second.subscription;
+ }
+ firstSubscribing.countDown();
+ // the subscribe completes only after the stop and the next
start (the interrupt of the stop does not
+ // end it)
+ awaitUninterruptibly(firstSubscribeDone);
+ return first.subscription;
+ }
+ throw new UnsupportedOperationException(name);
+ });
+ ServerInfo info = new ServerInfo("INFO
{\"server_id\":\"fake\",\"jetstream\":true}");
+ return proxy(Connection.class, (name, args) -> switch (name) {
+ case "getServerInfo" -> info;
+ case "jetStreamManagement" -> jsm;
+ case "jetStream" -> js;
+ case "getStatus" -> Connection.Status.CONNECTED;
+ default -> null;
+ });
+ }
+
+ /**
+ * Waits for the latch and keeps the interrupt of the thread for
afterwards, as a subscribe that the interrupt does
+ * not end.
+ */
+ private static void awaitUninterruptibly(CountDownLatch latch) {
+ boolean interrupted = false;
+ while (true) {
+ try {
+ assertTrue(latch.await(20, TimeUnit.SECONDS));
+ break;
+ } catch (InterruptedException e) {
+ interrupted = true;
+ }
+ }
+ if (interrupted) {
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ private static final class FakePullSubscription {
+ final AtomicBoolean active = new AtomicBoolean(true);
+ final AtomicInteger unsubscribes = new AtomicInteger();
+ final AtomicInteger fetches = new AtomicInteger();
+ final AtomicBoolean unsubscribedOnInterruptedThread = new
AtomicBoolean();
+ final JetStreamSubscription subscription =
proxy(JetStreamSubscription.class, (name, args) -> switch (name) {
+ case "isActive" -> active.get();
+ case "unsubscribe" -> {
+ // jnats drops the UNSUB that it queues on an interrupted
thread
+ unsubscribedOnInterruptedThread.compareAndSet(false,
Thread.currentThread().isInterrupted());
+ unsubscribes.incrementAndGet();
+ active.set(false);
+ yield null;
+ }
+ case "fetch" -> {
+ fetches.incrementAndGet();
+ // an idle consumer: the fetch ends empty at its timeout (an
interrupt ends it early, and is kept)
+ try {
+ new CountDownLatch(1).await(((Duration)
args[1]).toMillis(), TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ yield List.of();
+ }
+ default -> null;
+ });
+ }
+}