This is an automated email from the ASF dual-hosted git repository.

apupier pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit fd39bb7c1bcfd9dded9ba7b544412e81cf82d0f6
Author: AurĂ©lien Pupier <[email protected]>
AuthorDate: Wed Nov 25 14:29:57 2026 +0100

    CAMEL-26407: camel-mongodb - address review: persist only for a thread left 
behind, guard the stop-time persist, volatile lastVal
---
 .../component/mongodb/MongoAbstractConsumerThread.java  | 17 ++++++++++-------
 .../component/mongodb/MongoDbTailTrackingManager.java   |  2 +-
 .../camel/component/mongodb/MongoDbTailingThread.java   | 10 +++++++---
 .../component/mongodb/MongoDbConsumerStopTest.java      | 14 ++++++++++----
 4 files changed, 28 insertions(+), 15 deletions(-)

diff --git 
a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoAbstractConsumerThread.java
 
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoAbstractConsumerThread.java
index 68a2e4555bff..e1b0fe52d59e 100644
--- 
a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoAbstractConsumerThread.java
+++ 
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoAbstractConsumerThread.java
@@ -182,8 +182,9 @@ abstract class MongoAbstractConsumerThread implements 
Runnable {
         if (cursor != null) {
             cursor.close();
         }
-        awaitStopped();
-        afterStopped();
+        if (awaitStopped()) {
+            afterStopped();
+        }
 
         if (log.isInfoEnabled()) {
             log.info("Stopped MongoDB Tailable Cursor consumer, bound to 
collection: {}",
@@ -192,8 +193,9 @@ abstract class MongoAbstractConsumerThread implements 
Runnable {
     }
 
     /**
-     * Called by {@link #stop()} once the thread has ended, or has been left 
behind by {@link #awaitStopped()}. Runs on
-     * the thread that stops the consumer, so that the work is done before 
{@code stop()} returns.
+     * Called by {@link #stop()} when the consumer thread was left behind by 
{@link #awaitStopped()}, that is, it did
+     * not end while the thread that stops the consumer waited for it. Runs on 
that thread, before {@code stop()}
+     * returns.
      */
     protected void afterStopped() {
     }
@@ -208,9 +210,9 @@ abstract class MongoAbstractConsumerThread implements 
Runnable {
      * interrupted and left behind. An exchange that the thread routes is 
waited for as before, as the graceful shutdown
      * bounds the wait for an exchange in flight with its own timeout.
      */
-    private void awaitStopped() throws InterruptedException {
+    private boolean awaitStopped() throws InterruptedException {
         if (stoppedLatch.getCount() == 0) {
-            return;
+            return false;
         }
         log.info("Going to wait for stopping");
         // at least a little, so that the loop does not spin while the thread 
routes an exchange
@@ -226,8 +228,9 @@ abstract class MongoAbstractConsumerThread implements 
Runnable {
                 if (thread != null) {
                     thread.interrupt();
                 }
-                return;
+                return true;
             }
         }
+        return false;
     }
 }
diff --git 
a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbTailTrackingManager.java
 
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbTailTrackingManager.java
index 074f7602b56d..8e962599bb79 100644
--- 
a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbTailTrackingManager.java
+++ 
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbTailTrackingManager.java
@@ -33,7 +33,7 @@ public class MongoDbTailTrackingManager {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(MongoDbTailTrackingManager.class);
 
-    public Object lastVal;
+    public volatile Object lastVal;
 
     private final MongoClient connection;
     private final MongoDbTailTrackingConfig config;
diff --git 
a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbTailingThread.java
 
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbTailingThread.java
index 322edaad5445..1b5fa2171728 100644
--- 
a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbTailingThread.java
+++ 
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbTailingThread.java
@@ -149,12 +149,16 @@ class MongoDbTailingThread extends 
MongoAbstractConsumerThread {
     }
 
     /**
-     * Persists the tail position once the thread has ended, so that a test or 
caller reading the tracking document
-     * right after {@code stopRoute} sees the last routed record.
+     * Persists the tail position from the thread that stops the consumer, as 
a thread left behind may only persist much
+     * later. A failure is logged, so that the stop completes.
      */
     @Override
     protected void afterStopped() {
-        tailTracking.persistToStore();
+        try {
+            tailTracking.persistToStore();
+        } catch (Exception e) {
+            log.warn("Failed to persist the tail position while stopping the 
consumer", e);
+        }
     }
 
     Exchange createMongoDbExchange(Document dbObj) {
diff --git 
a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConsumerStopTest.java
 
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConsumerStopTest.java
index f695d7fe216d..549c2ded90fd 100644
--- 
a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConsumerStopTest.java
+++ 
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConsumerStopTest.java
@@ -49,6 +49,7 @@ import org.bson.conversions.Bson;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
 
 import static org.awaitility.Awaitility.await;
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -178,11 +179,16 @@ class MongoDbConsumerStopTest {
         result.assertIsSatisfied();
         assertTrue(reading.await(10, TimeUnit.SECONDS), "the consumer thread 
reads from the cursor");
 
-        stopRouteInAnotherThread(new CountDownLatch(1), new 
AtomicReference<>());
+        CountDownLatch stopped = new CountDownLatch(1);
+        AtomicReference<Exception> failure = new AtomicReference<>();
+        stopRouteInAnotherThread(stopped, failure);
+        assertTrue(stopped.await(10, TimeUnit.SECONDS), "the route stops while 
the consumer thread is blocked");
+        assertNull(failure.get());
         // the position is persisted by the stop itself: the consumer thread 
is blocked and never gets to its own persist
-        await().atMost(10, TimeUnit.SECONDS)
-                .until(() -> 
context.getRouteController().getRouteStatus("mongo") == ServiceStatus.Stopped);
-        verify(tracking).updateOne(any(Bson.class), any(Bson.class));
+        ArgumentCaptor<Bson> update = ArgumentCaptor.forClass(Bson.class);
+        verify(tracking).updateOne(any(Bson.class), update.capture());
+        assertEquals(new Document("$set", new 
Document(MongoDbTailTrackingConfig.DEFAULT_FIELD, 1)).toBsonDocument(),
+                update.getValue().toBsonDocument());
     }
 
     @Test

Reply via email to