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 4cd44045ecbf CAMEL-27588: camel-jms - a temporary reply queue refresh 
no longer clears the queue a consumer has just published (#30911)
4cd44045ecbf is described below

commit 4cd44045ecbf089576a838e1377db3c78d6ad300
Author: allthingssecurity <[email protected]>
AuthorDate: Fri Nov 27 12:42:35 2026 +0530

    CAMEL-27588: camel-jms - a temporary reply queue refresh no longer clears 
the queue a consumer has just published (#30911)
    
    scheduleRefresh bumped the refresh generation and then set replyTo to null,
    without the resolver's lock. A consumer of the reply listener container that
    resolved the new generation in between published its new queue, and the late
    null write overwrote it. No refresh was pending any more, so nothing 
published
    the queue again, and every InOut request failed with "Failed to resolve
    replyTo destination" until the next connection failure.
    
    scheduleRefresh now clears replyTo before it bumps the generation: a 
consumer
    can only publish the new generation after it has read the bumped value, so 
its
    publish wins. The exception listener path stays lock-free.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../jms/reply/TemporaryQueueReplyManager.java      | 13 +++++-
 .../TemporaryQueueReplyManagerRefreshTest.java     | 53 ++++++++++++++++++++++
 2 files changed, 64 insertions(+), 2 deletions(-)

diff --git 
a/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/TemporaryQueueReplyManager.java
 
b/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/TemporaryQueueReplyManager.java
index 9d4301131107..ef27b16d208c 100644
--- 
a/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/TemporaryQueueReplyManager.java
+++ 
b/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/TemporaryQueueReplyManager.java
@@ -375,12 +375,18 @@ public class TemporaryQueueReplyManager extends 
ReplyManagerSupport {
         // the destination, it would deadlock trying to acquire 
BaseService.lock.
         private final Lock destinationLock = new ReentrantLock();
         private volatile TemporaryQueue queue;
-        private final AtomicLong refreshGeneration = new AtomicLong();
+        private final AtomicLong refreshGeneration;
         private volatile long publishedGeneration;
         private final TemporaryQueueResolver custom;
 
         public TemporaryReplyQueueDestinationResolver(TemporaryQueueResolver 
custom) {
+            this(custom, new AtomicLong());
+        }
+
+        // package-private for testing: a test can pass a refresh generation 
it controls
+        TemporaryReplyQueueDestinationResolver(TemporaryQueueResolver custom, 
AtomicLong refreshGeneration) {
             this.custom = custom;
+            this.refreshGeneration = refreshGeneration;
         }
 
         boolean isRefreshPending() {
@@ -442,8 +448,11 @@ public class TemporaryQueueReplyManager extends 
ReplyManagerSupport {
         }
 
         public void scheduleRefresh() {
-            refreshGeneration.incrementAndGet();
+            // clear replyTo before the new generation is visible: a consumer 
that resolves this generation publishes
+            // its queue after this write, so the late null cannot overwrite 
the new queue (no refresh would be pending
+            // any more, and replyTo would stay null until the next connection 
failure)
             replyTo = null;
+            refreshGeneration.incrementAndGet();
             triggerReplyDestinationRecovery();
         }
 
diff --git 
a/components/camel-jms/src/test/java/org/apache/camel/component/jms/reply/TemporaryQueueReplyManagerRefreshTest.java
 
b/components/camel-jms/src/test/java/org/apache/camel/component/jms/reply/TemporaryQueueReplyManagerRefreshTest.java
index 5daff22434d3..64b9fe2b5be7 100644
--- 
a/components/camel-jms/src/test/java/org/apache/camel/component/jms/reply/TemporaryQueueReplyManagerRefreshTest.java
+++ 
b/components/camel-jms/src/test/java/org/apache/camel/component/jms/reply/TemporaryQueueReplyManagerRefreshTest.java
@@ -16,10 +16,12 @@
  */
 package org.apache.camel.component.jms.reply;
 
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
 
 import jakarta.jms.Connection;
 import jakarta.jms.Destination;
@@ -47,7 +49,9 @@ import static 
org.apache.camel.component.jms.JmsComponent.jmsComponentAutoAcknow
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.awaitility.Awaitility.await;
+import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
 
 class TemporaryQueueReplyManagerRefreshTest {
@@ -245,6 +249,55 @@ class TemporaryQueueReplyManagerRefreshTest {
         }
     }
 
+    @Test
+    void 
shouldKeepReplyToWhenConsumerResolvesTheNewGenerationDuringTheRefresh() throws 
Exception {
+        CountingTemporaryQueueResolver resolver = new 
CountingTemporaryQueueResolver();
+        replyManager = new 
TemporaryQueueReplyManager(contextExtension.getContext(), resolver);
+        replyManager.setEndpoint(createEndpoint());
+
+        // the exception listener thread pauses right after it has taken the 
new refresh generation
+        CountDownLatch generationTaken = new CountDownLatch(1);
+        CountDownLatch resume = new CountDownLatch(1);
+        AtomicLong refreshGeneration = spy(new AtomicLong());
+        doAnswer(invocation -> {
+            Object answer = invocation.callRealMethod();
+            generationTaken.countDown();
+            assertThat(resume.await(20, TimeUnit.SECONDS)).isTrue();
+            return answer;
+        }).when(refreshGeneration).incrementAndGet();
+        TemporaryQueueReplyManager.TemporaryReplyQueueDestinationResolver 
destinationResolver
+                = replyManager.new 
TemporaryReplyQueueDestinationResolver(resolver, refreshGeneration);
+
+        try (Connection connection = 
ConnectionFactoryHelper.createConnectionFactory(service).createConnection()) {
+            connection.start();
+            Session session = connection.createSession(false, 
Session.AUTO_ACKNOWLEDGE);
+
+            TemporaryQueue firstQueue
+                    = (TemporaryQueue) 
destinationResolver.resolveDestinationName(session, "temporary", false);
+            assertThat(replyManager.replyTo).isEqualTo(firstQueue);
+
+            // the JMS provider reports a connection failure to the exception 
listener
+            Thread exceptionListener = new 
Thread(destinationResolver::scheduleRefresh, "exception-listener");
+            exceptionListener.start();
+            assertThat(generationTaken.await(20, TimeUnit.SECONDS)).isTrue();
+            assertThat(destinationResolver.isRefreshPending()).isTrue();
+
+            // meanwhile a consumer of the listener container recovers and 
resolves the new generation
+            TemporaryQueue secondQueue
+                    = (TemporaryQueue) 
destinationResolver.resolveDestinationName(session, "temporary", false);
+            assertThat(secondQueue).isNotNull().isNotEqualTo(firstQueue);
+            assertThat(destinationResolver.isRefreshPending()).isFalse();
+
+            resume.countDown();
+            exceptionListener.join(20000);
+            assertThat(exceptionListener.isAlive()).isFalse();
+
+            // no refresh is pending, so nothing would publish the queue 
again: replyTo must still be the new queue
+            assertThat(destinationResolver.isRefreshPending()).isFalse();
+            assertThat(replyManager.replyTo).isEqualTo(secondQueue);
+        }
+    }
+
     private JmsEndpoint createEndpoint() {
         JmsEndpoint endpoint = mock(JmsEndpoint.class);
         
when(endpoint.getCamelContext()).thenReturn(contextExtension.getContext());

Reply via email to