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 2b2bc93a7a25 CAMEL-26983: camel-jms, camel-sjms - a poison message
goes to the error handler without redeliveries (#30045)
2b2bc93a7a25 is described below
commit 2b2bc93a7a25ed971b6630a692135b63e290bb2a
Author: Federico Mariani <[email protected]>
AuthorDate: Sun Nov 15 19:03:40 2026 +0100
CAMEL-26983: camel-jms, camel-sjms - a poison message goes to the error
handler without redeliveries (#30045)
Co-authored-by: Claude <[email protected]>
---
.../component/jms/EndpointMessageListener.java | 2 +
.../jms/JmsPoisonMessageRedeliveryTest.java | 143 +++++++++++++++++++++
.../sjms/consumer/EndpointMessageListener.java | 2 +
.../sjms/consumer/PoisonMessageRedeliveryTest.java | 79 ++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 7 +
5 files changed, 233 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 b34926d0a741..9297aa4a64d4 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
@@ -97,6 +97,8 @@ public class EndpointMessageListener implements
SessionAwareMessageListener {
} catch (Exception e) {
// any problems with eager loading then set an exception
so Camel error handler can react
exchange.setException(e);
+ // the payload cannot be read again, so there is nothing
to redeliver
+
exchange.getExchangeExtension().setRedeliveryExhausted(true);
String text = eagerPoisonBody;
try {
text =
endpoint.getCamelContext().resolveLanguage("simple")
diff --git
a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsPoisonMessageRedeliveryTest.java
b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsPoisonMessageRedeliveryTest.java
new file mode 100644
index 000000000000..727a6aba719b
--- /dev/null
+++
b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsPoisonMessageRedeliveryTest.java
@@ -0,0 +1,143 @@
+/*
+ * 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.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.Serial;
+import java.io.Serializable;
+
+import jakarta.jms.Message;
+import jakarta.jms.Session;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.infra.core.CamelContextExtension;
+import org.apache.camel.test.infra.core.DefaultCamelContextExtension;
+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.jms.support.converter.MessageConversionException;
+import org.springframework.jms.support.converter.MessageConverter;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A poison message (eagerLoadingOfProperties) goes to the error handler
without redeliveries, as its payload cannot be
+ * read again: the route is not run with the poison body.
+ */
+public class JmsPoisonMessageRedeliveryTest extends AbstractJMSTest {
+
+ @Order(2)
+ @RegisterExtension
+ public static CamelContextExtension camelContextExtension = new
DefaultCamelContextExtension();
+
+ protected CamelContext context;
+ protected ProducerTemplate template;
+
+ @Test
+ public void testPoisonMessageGoesToDeadLetterChannel() throws Exception {
+ MockEndpoint dead = getMockEndpoint("mock:dead");
+ dead.expectedMessageCount(1);
+
+
template.sendBody("activemq:queue:JmsPoisonMessageRedeliveryTest.dlc?objectMessageEnabled=true",
new Poison());
+
+ MockEndpoint.assertIsSatisfied(context);
+ assertEquals(0, getMockEndpoint("mock:dlc-step").getReceivedCounter());
+ Exchange exchange = dead.getReceivedExchanges().get(0);
+ assertTrue(exchange.getIn().getBody(String.class).startsWith("Poison
JMS message payload: "));
+ Exception caught = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
Exception.class);
+ assertNotNull(caught);
+ assertTrue(caught.getMessage().contains("poison!"),
caught.getMessage());
+ }
+
+ @Test
+ public void testPoisonMessageGoesToOnException() throws Exception {
+ MockEndpoint poison = getMockEndpoint("mock:poison");
+ poison.expectedMessageCount(1);
+ poison.expectedBodiesReceived("Poison JMS message payload: cannot
convert this message");
+
+
template.sendBody("activemq:queue:JmsPoisonMessageRedeliveryTest.converter",
"Hello World");
+
+ MockEndpoint.assertIsSatisfied(context);
+ assertEquals(0,
getMockEndpoint("mock:converter-step").getReceivedCounter());
+ }
+
+ @Override
+ protected String getComponentName() {
+ return "activemq";
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ getContext().getRegistry().bind("poisonConverter", new
PoisonConverter());
+
+
from("activemq:queue:JmsPoisonMessageRedeliveryTest.dlc?eagerLoadingOfProperties=true&objectMessageEnabled=true")
+
.errorHandler(deadLetterChannel("mock:dead").maximumRedeliveries(2).redeliveryDelay(0))
+ .to("mock:dlc-step");
+
+
from("activemq:queue:JmsPoisonMessageRedeliveryTest.converter?eagerLoadingOfProperties=true&messageConverter=#poisonConverter")
+
.onException(Exception.class).maximumRedeliveries(2).redeliveryDelay(0).handled(true)
+ .to("mock:poison")
+ .end()
+ .to("mock:converter-step");
+ }
+ };
+ }
+
+ @Override
+ public CamelContextExtension getCamelContextExtension() {
+ return camelContextExtension;
+ }
+
+ @BeforeEach
+ void setUpRequirements() {
+ context = camelContextExtension.getContext();
+ template = camelContextExtension.getProducerTemplate();
+ }
+
+ public static class Poison implements Serializable {
+ @Serial
+ private static final long serialVersionUID = 1L;
+
+ @Serial
+ private void readObject(ObjectInputStream in) throws IOException {
+ throw new IOException("poison!");
+ }
+ }
+
+ private static class PoisonConverter implements MessageConverter {
+ @Override
+ public Message toMessage(Object object, Session session) {
+ throw new MessageConversionException("not used");
+ }
+
+ @Override
+ public Object fromMessage(Message message) {
+ throw new MessageConversionException("cannot convert this
message");
+ }
+ }
+}
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 3c9bb148ec50..4b68a7fd5405 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
@@ -165,6 +165,8 @@ public class EndpointMessageListener implements
SessionMessageListener {
} catch (Exception e) {
// any problems with eager loading then set an exception
so Camel error handler can react
exchange.setException(e);
+ // the payload cannot be read again, so there is nothing
to redeliver
+
exchange.getExchangeExtension().setRedeliveryExhausted(true);
String text = eagerPoisonBody;
try {
text =
endpoint.getCamelContext().resolveLanguage("simple")
diff --git
a/components/camel-sjms/src/test/java/org/apache/camel/component/sjms/consumer/PoisonMessageRedeliveryTest.java
b/components/camel-sjms/src/test/java/org/apache/camel/component/sjms/consumer/PoisonMessageRedeliveryTest.java
new file mode 100644
index 000000000000..5610ce0949fa
--- /dev/null
+++
b/components/camel-sjms/src/test/java/org/apache/camel/component/sjms/consumer/PoisonMessageRedeliveryTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.Serial;
+import java.io.Serializable;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.sjms.support.JmsTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A poison message (eagerLoadingOfProperties) goes to the error handler
without redeliveries, as its payload cannot be
+ * read again: the route is not run with the poison body.
+ */
+public class PoisonMessageRedeliveryTest extends JmsTestSupport {
+
+ @Override
+ protected boolean useJmx() {
+ return false;
+ }
+
+ @Test
+ public void testPoisonMessageGoesToDeadLetterChannel() throws Exception {
+ MockEndpoint dead = getMockEndpoint("mock:dead");
+ dead.expectedMessageCount(1);
+
+
template.sendBody("sjms:queue:PoisonMessageRedeliveryTest?objectMessageEnabled=true",
new Poison());
+
+ MockEndpoint.assertIsSatisfied(context);
+ assertEquals(0, getMockEndpoint("mock:step").getReceivedCounter());
+ Exchange exchange = dead.getReceivedExchanges().get(0);
+ assertTrue(exchange.getIn().getBody(String.class).startsWith("Poison
JMS message payload: "));
+ assertNotNull(exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
Exception.class));
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+
from("sjms:queue:PoisonMessageRedeliveryTest?eagerLoadingOfProperties=true&objectMessageEnabled=true")
+
.errorHandler(deadLetterChannel("mock:dead").maximumRedeliveries(2).redeliveryDelay(0))
+ .to("mock:step");
+ }
+ };
+ }
+
+ public static class Poison implements Serializable {
+ @Serial
+ private static final long serialVersionUID = 1L;
+
+ @Serial
+ private void readObject(ObjectInputStream in) throws IOException {
+ throw new IOException("poison!");
+ }
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 028d217aef2c..050e2b1e7250 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -401,6 +401,13 @@ The camel-sjms and camel-sjms2 consumers now map a
received `jakarta.jms.StreamM
bytes, as the camel-jms consumer does, so a body sent with
`jmsMessageType=Stream` arrives as the bytes that were sent.
Before, the body was the `jakarta.jms.StreamMessage` itself. To get the JMS
message, use `mapJmsMessage=false`.
+=== camel-jms, camel-sjms, camel-sjms2 - a poison message goes to the error
handler without redeliveries
+
+With `eagerLoadingOfProperties=true`, a poison message (one whose payload or
properties cannot be read) now goes to
+the error handler (such as the dead letter channel or `onException`) without
redeliveries, with its `eagerPoisonBody`
+body. Before, when the error handler had redeliveries, the first step of the
route was redelivered with that body and,
+when it succeeded, the message went on through the route and never reached the
error handler.
+
=== camel-keycloak - Keycloak 26.8.2 requires the kc-admin-api client scope
This is a change in Keycloak, not in Camel: from Keycloak 26.8.2 the Admin
REST API accepts only access tokens that