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 4816c01647e0 CAMEL-27425: camel-jms, camel-sjms - a failed request 
says that no reply is sent, and the caller waits (#30648)
4816c01647e0 is described below

commit 4816c01647e02506e6fc22be99f42e2555de3447
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Nov 23 10:03:13 2026 +0100

    CAMEL-27425: camel-jms, camel-sjms - a failed request says that no reply is 
sent, and the caller waits (#30648)
    
    When the exchange of a JMS message that expects a reply (it has a 
JMSReplyTo)
    failed, the consumer sent no reply and rethrew the failure, which its 
listener
    container logs. The caller timed out later, and nothing linked the two. The
    rethrown exception now says that no reply is sent, that the caller waits 
until
    its requestTimeout, and how to make the caller fail at once: reply with a 
message that says the
    request failed, and check it in the caller (not transferException, which 
sends
    the exception as a serialized Java object). Its cause is the exception of 
the
    exchange, and its message starts with it, as before.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../component/jms/EndpointMessageListener.java     |  19 +++
 .../jms/JmsInOutFailedRequestMessageTest.java      | 117 +++++++++++++++++++
 .../sjms/consumer/EndpointMessageListener.java     |  18 +++
 .../InOutConsumerFailedRequestMessageTest.java     | 130 +++++++++++++++++++++
 4 files changed, 284 insertions(+)

diff --git 
a/components/camel-jms/src/main/java/org/apache/camel/component/jms/EndpointMessageListener.java
 
b/components/camel-jms/src/main/java/org/apache/camel/component/jms/EndpointMessageListener.java
index 9297aa4a64d4..e243a16d0d0d 100644
--- 
a/components/camel-jms/src/main/java/org/apache/camel/component/jms/EndpointMessageListener.java
+++ 
b/components/camel-jms/src/main/java/org/apache/camel/component/jms/EndpointMessageListener.java
@@ -176,6 +176,20 @@ public class EndpointMessageListener implements 
SessionAwareMessageListener {
         LOG.trace("onMessage END");
     }
 
+    /**
+     * The failure of a message that expects a reply, as the listener 
container logs it. The exchange failed, so no
+     * reply is sent: the caller waits until its requestTimeout, and its 
ExchangeTimedOutException cannot say why.
+     */
+    static String noReplyMessage(Exception cause, Object replyDestination) {
+        String failure = cause.toString();
+        if (failure.endsWith(".")) {
+            failure = failure.substring(0, failure.length() - 1);
+        }
+        return failure + ". No reply is sent (JMSReplyTo " + replyDestination 
+ "), as the exchange failed: the caller"
+               + " waits until its requestTimeout and then fails with 
ExchangeTimedOutException. To make the caller fail"
+               + " at once, reply with a message that says the request failed 
and check it in the caller";
+    }
+
     /**
      * Callback task that is performed when the exchange has been processed
      */
@@ -229,6 +243,11 @@ public class EndpointMessageListener implements 
SessionAwareMessageListener {
             }
             if (exchange.isFailed() && exchange.getException() != null
                     && !(endpoint.isTransferException() && sendReply)) {
+                if (sendReply) {
+                    // no reply is sent, and the caller sees only its own 
timeout later: say so where this failure is logged
+                    return new RuntimeCamelException(
+                            noReplyMessage(exchange.getException(), 
replyDestination), exchange.getException());
+                }
                 return wrapRuntimeCamelException(exchange.getException());
             }
             return null;
diff --git 
a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsInOutFailedRequestMessageTest.java
 
b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsInOutFailedRequestMessageTest.java
new file mode 100644
index 000000000000..ec92132e7998
--- /dev/null
+++ 
b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsInOutFailedRequestMessageTest.java
@@ -0,0 +1,117 @@
+/*
+ * 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.jms;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ExchangeTimedOutException;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.infra.core.CamelContextExtension;
+import org.apache.camel.test.infra.core.TransientCamelContextExtension;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.springframework.util.ErrorHandler;
+
+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.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A consumer that fails on a message that expects a reply sends no reply: the 
failure it logs says so, as the caller
+ * sees only its own timeout later.
+ */
+public class JmsInOutFailedRequestMessageTest extends AbstractJMSTest {
+
+    @Order(2)
+    @RegisterExtension
+    public static CamelContextExtension camelContextExtension = new 
TransientCamelContextExtension();
+    protected ProducerTemplate template;
+
+    private final List<Throwable> requestFailures = new 
CopyOnWriteArrayList<>();
+    private final List<Throwable> eventFailures = new CopyOnWriteArrayList<>();
+
+    @Test
+    public void testTheFailureOfARequestSaysThatNoReplyIsSent() {
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> template.requestBody("direct:reserve", "ORD-1003"));
+        assertInstanceOf(ExchangeTimedOutException.class, e.getCause());
+
+        await().atMost(5, TimeUnit.SECONDS).until(() -> 
!requestFailures.isEmpty());
+        Throwable failure = requestFailures.get(0);
+        assertInstanceOf(IllegalStateException.class, failure.getCause());
+        String msg = failure.getMessage();
+        assertTrue(msg.startsWith("java.lang.IllegalStateException: not enough 
CAMEL-CAP. No reply is sent (JMSReplyTo "),
+                msg);
+        assertTrue(msg.contains("), as the exchange failed: the caller waits 
until its requestTimeout and then fails with"
+                                + " ExchangeTimedOutException"),
+                msg);
+        assertTrue(msg.endsWith("To make the caller fail at once, reply with a 
message that says the request failed and"
+                                + " check it in the caller"),
+                msg);
+        // no advice to send the exception back, which would be a serialized 
Java object (transferException)
+        assertFalse(msg.contains("transferException"), msg);
+    }
+
+    @Test
+    public void testTheFailureOfAMessageThatExpectsNoReplyIsAsBefore() {
+        
template.sendBody("activemq:queue:JmsInOutFailedRequestMessageTest.event", 
"E-1");
+
+        await().atMost(5, TimeUnit.SECONDS).until(() -> 
!eventFailures.isEmpty());
+        assertEquals("java.lang.IllegalStateException: bad event", 
eventFailures.get(0).getMessage());
+    }
+
+    @Override
+    protected String getComponentName() {
+        return "activemq";
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                getContext().getRegistry().bind("requestFailures", 
(ErrorHandler) requestFailures::add);
+                getContext().getRegistry().bind("eventFailures", 
(ErrorHandler) eventFailures::add);
+
+                
from("activemq:queue:JmsInOutFailedRequestMessageTest.stock?errorHandler=#requestFailures")
+                        .throwException(new IllegalStateException("not enough 
CAMEL-CAP"));
+                
from("activemq:queue:JmsInOutFailedRequestMessageTest.event?errorHandler=#eventFailures")
+                        .throwException(new IllegalStateException("bad 
event"));
+                
from("direct:reserve").to("activemq:queue:JmsInOutFailedRequestMessageTest.stock?requestTimeout=1000");
+            }
+        };
+    }
+
+    @Override
+    public CamelContextExtension getCamelContextExtension() {
+        return camelContextExtension;
+    }
+
+    @BeforeEach
+    void setUpRequirements() {
+        template = camelContextExtension.getProducerTemplate();
+    }
+}
diff --git 
a/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/consumer/EndpointMessageListener.java
 
b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/consumer/EndpointMessageListener.java
index 4b68a7fd5405..46d85405f49a 100644
--- 
a/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/consumer/EndpointMessageListener.java
+++ 
b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/consumer/EndpointMessageListener.java
@@ -245,6 +245,20 @@ public class EndpointMessageListener implements 
SessionMessageListener {
         LOG.trace("onMessage END");
     }
 
+    /**
+     * The failure of a message that expects a reply, as the listener 
container logs it. The exchange failed, so no
+     * reply is sent: the caller waits until its requestTimeout, and its 
ExchangeTimedOutException cannot say why.
+     */
+    static String noReplyMessage(Exception cause, Object replyDestination) {
+        String failure = cause.toString();
+        if (failure.endsWith(".")) {
+            failure = failure.substring(0, failure.length() - 1);
+        }
+        return failure + ". No reply is sent (JMSReplyTo " + replyDestination 
+ "), as the exchange failed: the caller"
+               + " waits until its requestTimeout and then fails with 
ExchangeTimedOutException. To make the caller fail"
+               + " at once, reply with a message that says the request failed 
and check it in the caller";
+    }
+
     protected Object getReplyToDestination(Message message) {
         // lets send a response back if we can
         Object destination = getReplyToDestination();
@@ -438,6 +452,10 @@ public class EndpointMessageListener implements 
SessionMessageListener {
                         // send the exception as reply, so null body and set 
the exception as the cause
                         body = null;
                         cause = exchange.getException();
+                    } else if (sendReply) {
+                        // no reply is sent, and the caller sees only its own 
timeout later: say so where this failure is logged
+                        rce = new RuntimeCamelException(
+                                noReplyMessage(exchange.getException(), 
replyDestination), exchange.getException());
                     } else {
                         // only throw exception if endpoint is not configured 
to transfer exceptions back to caller
                         // or if there is no reply destination to send the 
exception to
diff --git 
a/components/camel-sjms/src/test/java/org/apache/camel/component/sjms/consumer/InOutConsumerFailedRequestMessageTest.java
 
b/components/camel-sjms/src/test/java/org/apache/camel/component/sjms/consumer/InOutConsumerFailedRequestMessageTest.java
new file mode 100644
index 000000000000..43af720384d6
--- /dev/null
+++ 
b/components/camel-sjms/src/test/java/org/apache/camel/component/sjms/consumer/InOutConsumerFailedRequestMessageTest.java
@@ -0,0 +1,130 @@
+/*
+ * 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.sjms.consumer;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ExchangeTimedOutException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.sjms.support.JmsTestSupport;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+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.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A consumer that fails on a message that expects a reply sends no reply: the 
failure it logs says so, as the caller
+ * sees only its own timeout later.
+ */
+public class InOutConsumerFailedRequestMessageTest extends JmsTestSupport {
+
+    private static final FailureCaptor CAPTOR = new FailureCaptor();
+
+    @BeforeAll
+    static void addCaptor() {
+        ((Logger) 
LogManager.getLogger(SimpleMessageListenerContainer.class)).addAppender(CAPTOR);
+    }
+
+    @AfterAll
+    static void removeCaptor() {
+        ((Logger) 
LogManager.getLogger(SimpleMessageListenerContainer.class)).removeAppender(CAPTOR);
+    }
+
+    @Test
+    public void testTheFailureOfARequestSaysThatNoReplyIsSent() {
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> template.requestBody("direct:reserve", "ORD-1003"));
+        assertInstanceOf(ExchangeTimedOutException.class, e.getCause());
+
+        String msg = CAPTOR.awaitFailure("not enough CAMEL-CAP");
+        assertTrue(msg.startsWith("java.lang.IllegalStateException: not enough 
CAMEL-CAP. No reply is sent (JMSReplyTo "),
+                msg);
+        assertTrue(msg.contains("), as the exchange failed: the caller waits 
until its requestTimeout and then fails with"
+                                + " ExchangeTimedOutException"),
+                msg);
+        assertTrue(msg.endsWith("To make the caller fail at once, reply with a 
message that says the request failed and"
+                                + " check it in the caller"),
+                msg);
+        // no advice to send the exception back, which would be a serialized 
Java object (transferException)
+        assertFalse(msg.contains("transferException"), msg);
+    }
+
+    @Test
+    public void testTheFailureOfAMessageThatExpectsNoReplyIsAsBefore() {
+        
template.sendBody("sjms:queue:InOutConsumerFailedRequestMessageTest.event", 
"E-1");
+
+        assertEquals("java.lang.IllegalStateException: bad event", 
CAPTOR.awaitFailure("bad event"));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("sjms:queue:InOutConsumerFailedRequestMessageTest.stock")
+                        .throwException(new IllegalStateException("not enough 
CAMEL-CAP"));
+                from("sjms:queue:InOutConsumerFailedRequestMessageTest.event")
+                        .throwException(new IllegalStateException("bad 
event"));
+                
from("direct:reserve").to("sjms:queue:InOutConsumerFailedRequestMessageTest.stock?requestTimeout=1000");
+            }
+        };
+    }
+
+    /**
+     * The failures the listener container logs, with the exception it logs 
them with.
+     */
+    private static final class FailureCaptor extends AbstractAppender {
+        private final List<Throwable> failures = new CopyOnWriteArrayList<>();
+
+        FailureCaptor() {
+            super("InOutConsumerFailedRequestMessageTestCaptor", null, null, 
false, Property.EMPTY_ARRAY);
+            start();
+        }
+
+        @Override
+        public void append(LogEvent event) {
+            if (event.getThrown() != null) {
+                failures.add(event.getThrown());
+            }
+        }
+
+        /** The message of the logged failure that names the text, once it is 
logged. */
+        String awaitFailure(String text) {
+            await().atMost(5, TimeUnit.SECONDS).until(() -> find(text) != 
null);
+            return find(text);
+        }
+
+        private String find(String text) {
+            return failures.stream().map(Throwable::getMessage).filter(m -> m 
!= null && m.contains(text)).findFirst()
+                    .orElse(null);
+        }
+    }
+}

Reply via email to