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

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

commit 4c73ceb9ec36fbd51d4c70835d0e069830c2e4f6
Author: croway <[email protected]>
AuthorDate: Wed Nov 25 18:25:14 2026 +0100

    CAMEL-27009: camel-spring-rabbitmq - a stop that fails with an error in the 
background is logged and destroys the listener container
    
    The stop of a listener container that nobody waits for anymore caught a
    RuntimeException only: an Error (an AssertionError, a NoClassDefFoundError)
    ended in the abandoned CompletableFuture, without the WARN and without
    destroying the listener container. It now catches the Error too, logs it
    and destroys the listener container; a consumer that still waits for the
    stop gets the Error unchanged, as from a stop on its own thread. The stop
    in doStop also destroys the listener container when its stop fails with
    an Error.
    
    RabbitMQConsumerSuspendResumeIT names its exchanges and queues per run,
    so that another run on the same broker, or a message that a failed run
    left in a queue, does not change the counts it checks.
---
 .../springrabbit/SpringRabbitMQConsumer.java       | 19 ++++---
 .../SpringRabbitMQConsumerStopTest.java            | 62 +++++++++++++++++++++-
 .../RabbitMQConsumerSuspendResumeIT.java           | 14 +++--
 3 files changed, 82 insertions(+), 13 deletions(-)

diff --git 
a/components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumer.java
 
b/components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumer.java
index 57e07fdaecff..e585868eecf3 100644
--- 
a/components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumer.java
+++ 
b/components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumer.java
@@ -335,19 +335,22 @@ public class SpringRabbitMQConsumer extends 
DefaultConsumer implements Suspendab
 
     /**
      * The stop that runs on a thread of the component. When nobody waits for 
it anymore, it destroys the listener
-     * container and logs the outcome by itself.
+     * container and logs the outcome by itself, also when the stop fails with 
an error.
      */
     private void doStopListenerContainer(
             AbstractMessageListenerContainer container, boolean destroy, 
AtomicBoolean waiting) {
-        RuntimeException failure = null;
+        Throwable failure = null;
         try {
             stopAndDestroy(container, destroy);
-        } catch (RuntimeException e) {
+        } catch (RuntimeException | Error e) {
             failure = e;
         }
         if (waiting.compareAndSet(true, false)) {
-            if (failure != null) {
-                throw failure;
+            // the consumer waits for the stop: it gets the failure unchanged, 
as from a stop on its own thread
+            if (failure instanceof RuntimeException e) {
+                throw e;
+            } else if (failure instanceof Error e) {
+                throw e;
             }
             return;
         }
@@ -361,7 +364,7 @@ public class SpringRabbitMQConsumer extends DefaultConsumer 
implements Suspendab
         if (!destroy) {
             try {
                 container.destroy();
-            } catch (RuntimeException e) {
+            } catch (RuntimeException | Error e) {
                 LOG.warn("Error destroying the listener container of {} 
(queues: {}) in the background. This exception"
                          + " is ignored.",
                         getEndpoint(), queues, e);
@@ -375,11 +378,11 @@ public class SpringRabbitMQConsumer extends 
DefaultConsumer implements Suspendab
     private static void stopAndDestroy(AbstractMessageListenerContainer 
container, boolean destroy) {
         try {
             container.stop();
-        } catch (RuntimeException e) {
+        } catch (RuntimeException | Error e) {
             if (destroy) {
                 try {
                     container.destroy();
-                } catch (RuntimeException destroyFailure) {
+                } catch (RuntimeException | Error destroyFailure) {
                     e.addSuppressed(destroyFailure);
                 }
             }
diff --git 
a/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumerStopTest.java
 
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumerStopTest.java
index 288f35ec0234..261f0246d422 100644
--- 
a/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumerStopTest.java
+++ 
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumerStopTest.java
@@ -18,9 +18,9 @@ package org.apache.camel.component.springrabbit;
 
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionException;
@@ -64,6 +64,8 @@ import static org.awaitility.Awaitility.await;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyBoolean;
@@ -95,7 +97,10 @@ class SpringRabbitMQConsumerStopTest {
     private final String name = "orders-" + UUID.randomUUID();
 
     private final List<AbstractMessageListenerContainer> containers = new 
CopyOnWriteArrayList<>();
-    private final Set<AbstractMessageListenerContainer> destroyed = 
ConcurrentHashMap.newKeySet();
+    // each destroy of a listener container
+    private final List<AbstractMessageListenerContainer> destroyed = new 
CopyOnWriteArrayList<>();
+    // an error that the next stop of a listener container throws, once it 
stopped
+    private final AtomicReference<Error> stopError = new AtomicReference<>();
     // the channels on which a consumer subscribed, in that order
     private final List<FakeChannel> subscribed = new CopyOnWriteArrayList<>();
     private final List<ConnectionListener> connectionListeners = new 
CopyOnWriteArrayList<>();
@@ -230,6 +235,40 @@ class SpringRabbitMQConsumerStopTest {
                 .until(() -> destroyed.contains(containers.get(0)));
     }
 
+    @ParameterizedTest
+    @ValueSource(strings = { "DMLC", "SMLC" })
+    void 
errorOfAStopInTheBackgroundIsLoggedAndTheListenerContainerDestroyed(String 
type) throws Exception {
+        startRoute(type);
+        subscribed(0).stopAnswering();
+        AssertionError error = new AssertionError("the stop failed");
+        stopError.set(error);
+        suspendThatDoesNotWait();
+        AbstractMessageListenerContainer container = containers.get(0);
+
+        // the broker answers: the stop, which nobody waits for anymore, fails 
with an error
+        answer.countDown();
+
+        await("the error is logged").atMost(10, TimeUnit.SECONDS).until(() -> 
events.stream()
+                .anyMatch(e -> e.getLevel() == Level.WARN && e.getThrown() == 
error
+                        && 
e.getMessage().getFormattedMessage().contains(name)));
+        await("the listener container is destroyed").atMost(10, 
TimeUnit.SECONDS)
+                .until(() -> destroyed.contains(container));
+        assertEquals(1, Collections.frequency(destroyed, container), 
"destroyed once: " + destroyed);
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = { "DMLC", "SMLC" })
+    void errorOfAStopInTimeReachesTheConsumerUnchanged(String type) throws 
Exception {
+        startRoute(type);
+        subscribed(0);
+        AssertionError error = new AssertionError("the stop failed");
+        stopError.set(error);
+        SpringRabbitMQConsumer consumer = (SpringRabbitMQConsumer) 
context.getRoute("orders").getConsumer();
+
+        // as when the consumer stopped the listener container on its own 
thread
+        assertSame(error, assertThrows(AssertionError.class, 
consumer::suspend));
+    }
+
     @ParameterizedTest
     @ValueSource(strings = { "DMLC", "SMLC" })
     void 
resumeAfterASuspendThatDidNotWaitConsumesWithANewListenerContainer(String type) 
throws Exception {
@@ -408,6 +447,12 @@ class SpringRabbitMQConsumerStopTest {
         AbstractMessageListenerContainer container;
         if 
(SpringRabbitMQConstants.SIMPLE_MESSAGE_LISTENER_CONTAINER.equals(endpoint.getMessageListenerContainerType()))
 {
             CamelSimpleMessageListenerContainer simple = new 
CamelSimpleMessageListenerContainer(endpoint) {
+                @Override
+                public void stop() {
+                    super.stop();
+                    failStop();
+                }
+
                 @Override
                 public void destroy() {
                     destroyed.add(this);
@@ -419,6 +464,12 @@ class SpringRabbitMQConsumerStopTest {
             container = simple;
         } else {
             container = new CamelDirectMessageListenerContainer(endpoint) {
+                @Override
+                public void stop() {
+                    super.stop();
+                    failStop();
+                }
+
                 @Override
                 public void destroy() {
                     destroyed.add(this);
@@ -436,6 +487,13 @@ class SpringRabbitMQConsumerStopTest {
         return container;
     }
 
+    private void failStop() {
+        Error error = stopError.getAndSet(null);
+        if (error != null) {
+            throw error;
+        }
+    }
+
     private ConnectionFactory connectionFactory() {
         ConnectionFactory cf = mock(ConnectionFactory.class);
         Connection connection = mock(Connection.class);
diff --git 
a/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/integration/RabbitMQConsumerSuspendResumeIT.java
 
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/integration/RabbitMQConsumerSuspendResumeIT.java
index 19ffbe21325f..4e527dd62d73 100644
--- 
a/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/integration/RabbitMQConsumerSuspendResumeIT.java
+++ 
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/integration/RabbitMQConsumerSuspendResumeIT.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.springrabbit.integration;
 
 import java.util.Properties;
+import java.util.UUID;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.RoutesBuilder;
@@ -38,6 +39,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
  */
 public class RabbitMQConsumerSuspendResumeIT extends RabbitMQITSupport {
 
+    // the exchanges and queues of this test run only, so that another run on 
the same broker, or a message that a
+    // failed run left in a queue, does not change the counts
+    private final String run = UUID.randomUUID().toString();
     // the test checks the queue with its own connection factory
     private CachingConnectionFactory testConnectionFactory;
 
@@ -51,7 +55,7 @@ public class RabbitMQConsumerSuspendResumeIT extends 
RabbitMQITSupport {
     @ParameterizedTest
     @ValueSource(strings = { "DMLC", "SMLC" })
     public void testSuspendAndResume(String type) throws Exception {
-        String queue = uniqueName(type + "-queue");
+        String queue = name(type + "-queue");
         testConnectionFactory = (CachingConnectionFactory) 
createConnectionFactory(false);
         RabbitAdmin admin = new RabbitAdmin(testConnectionFactory);
         MockEndpoint mock = getMockEndpoint("mock:" + type);
@@ -76,6 +80,10 @@ public class RabbitMQConsumerSuspendResumeIT extends 
RabbitMQITSupport {
         MockEndpoint.assertIsSatisfied(context, 30, TimeUnit.SECONDS);
     }
 
+    private String name(String baseName) {
+        return uniqueName(baseName) + "-" + run;
+    }
+
     private static int count(RabbitAdmin admin, String queue, Object property) 
{
         Properties properties = admin.getQueueProperties(queue);
         return properties == null ? -1 : ((Number) 
properties.get(property)).intValue();
@@ -88,9 +96,9 @@ public class RabbitMQConsumerSuspendResumeIT extends 
RabbitMQITSupport {
             public void configure() {
                 for (String type : new String[] { "DMLC", "SMLC" }) {
                     from("direct:" + type)
-                            .to("spring-rabbitmq:" + uniqueName(type) + 
"?routingKey=new");
+                            .to("spring-rabbitmq:" + name(type) + 
"?routingKey=new");
 
-                    from("spring-rabbitmq:" + uniqueName(type) + "?queues=" + 
uniqueName(type + "-queue")
+                    from("spring-rabbitmq:" + name(type) + "?queues=" + 
name(type + "-queue")
                          + "&routingKey=new&messageListenerContainerType=" + 
type)
                             .routeId(type)
                             .to("mock:" + type);

Reply via email to