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 f794ea08b8ba CAMEL-27283: camel-core - Dynamic Router and Routing Slip
go on after an asynchronous step as after a synchronous one (#30530)
f794ea08b8ba is described below
commit f794ea08b8baacd832241b427b4c360fa3d82456
Author: allthingssecurity <[email protected]>
AuthorDate: Sat Nov 21 13:19:56 2026 +0530
CAMEL-27283: camel-core - Dynamic Router and Routing Slip go on after an
asynchronous step as after a synchronous one (#30530)
RoutingSlip wraps each step in the route's error handler, whose process()
returns false, so from the second step on
the EIP goes on in continueRoutingSlip. There an exception of the router
expression was set on the exchange the step
ran on (a copy of the message), so the message went on without it. After a
failed step the router was called again,
and ignoreInvalidEndpoints was not applied to the last step.
continueRoutingSlip now sets the exception on the original exchange, and
after each step it applies
ignoreInvalidEndpoints and checks for a failure before it asks for the next
step, as the synchronous loop does.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../org/apache/camel/processor/RoutingSlip.java | 51 +++--
.../AsyncEndpointDynamicRouterFailureTest.java | 209 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 13 ++
3 files changed, 246 insertions(+), 27 deletions(-)
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java
index 9c9cfed0c7b4..bfb2e86beb50 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java
@@ -302,15 +302,7 @@ public class RoutingSlip extends BaseProcessorSupport
implements Traceable, IdAw
}
// we ignore some kind of exceptions and allow us to continue
- if (isIgnoreInvalidEndpoints()) {
- FailedToCreateProducerException e =
current.getException(FailedToCreateProducerException.class);
- if (e != null) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Endpoint uri is invalid: {}. This exception
will be ignored.", endpoint, e);
- }
- current.setException(null);
- }
- }
+ ignoreInvalidEndpoint(current, endpoint);
// Decide whether to continue with the recipients or not; similar
logic to the Pipeline
// check for error if so we should break out
@@ -462,24 +454,12 @@ public class RoutingSlip extends BaseProcessorSupport
implements Traceable, IdAw
// continue processing the routing slip asynchronously
Exchange current = prepareExchangeForRoutingSlip(stepExchange,
endpoint);
- while (iter.hasNext(current)) {
+ // as when the step completed synchronously: ignore an invalid
endpoint of the step, and check for an error
+ // before the next step is asked for (so a dynamic router is not
called again after a failed step)
+ ignoreInvalidEndpoint(current, endpoint);
+ boolean more = continueProcessing(current, "so breaking out of the
routing slip", LOG);
- // we ignore some kind of exceptions and allow us to continue
- if (isIgnoreInvalidEndpoints()) {
- FailedToCreateProducerException e =
current.getException(FailedToCreateProducerException.class);
- if (e != null) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Endpoint uri is invalid: {}. This
exception will be ignored.", endpoint, e);
- }
- current.setException(null);
- }
- }
-
- // Decide whether to continue with the recipients or not;
similar logic to the Pipeline
- // check for error if so we should break out
- if (!continueProcessing(current, "so breaking out of the
routing slip", LOG)) {
- break;
- }
+ while (more && iter.hasNext(current)) {
Endpoint nextEndpoint;
boolean prototype = cacheSize < 0;
@@ -518,6 +498,9 @@ public class RoutingSlip extends BaseProcessorSupport
implements Traceable, IdAw
}
return;
}
+
+ ignoreInvalidEndpoint(current, nextEndpoint);
+ more = continueProcessing(current, "so breaking out of the
routing slip", LOG);
}
// logging current as it contains the exchange that might have
altered the payload and since
@@ -529,7 +512,9 @@ public class RoutingSlip extends BaseProcessorSupport
implements Traceable, IdAw
// copy results back to the original exchange
ExchangeHelper.copyResults(original, current);
} catch (Exception e) {
- stepExchange.setException(e);
+ // fail the original exchange: the step exchange is a copy of it,
unless the step was the first one, and the
+ // original callback is called next (for example a dynamic router
whose expression threw)
+ original.setException(e);
}
// okay we are completely done with the routing slip
@@ -537,6 +522,18 @@ public class RoutingSlip extends BaseProcessorSupport
implements Traceable, IdAw
originalCallback.done(false);
}
+ private void ignoreInvalidEndpoint(Exchange current, Endpoint endpoint) {
+ if (isIgnoreInvalidEndpoints()) {
+ FailedToCreateProducerException e =
current.getException(FailedToCreateProducerException.class);
+ if (e != null) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Endpoint uri is invalid: {}. This exception
will be ignored.", endpoint, e);
+ }
+ current.setException(null);
+ }
+ }
+ }
+
private static AsyncCallback getNextCallback(boolean prototype, Endpoint
nextEndpoint, AsyncCallback cb) {
final boolean prototypeEndpoint = prototype;
AsyncCallback cbNext = doneNext -> {
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointDynamicRouterFailureTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointDynamicRouterFailureTest.java
new file mode 100644
index 000000000000..1caac7c47646
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointDynamicRouterFailureTest.java
@@ -0,0 +1,209 @@
+/*
+ * 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.processor.async;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.camel.CamelExchangeException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.DynamicRouter;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.support.ExpressionAdapter;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+/**
+ * A dynamic router or routing slip whose step completed asynchronously goes
on as when the step completed
+ * synchronously: a failure of the router expression after that step fails the
exchange, the router is not asked again
+ * after a failed step, and an invalid last endpoint is ignored with
ignoreInvalidEndpoints.
+ */
+public class AsyncEndpointDynamicRouterFailureTest extends ContextTestSupport {
+
+ private static final String ASYNC_HI = "async:hi:camel?delay=0";
+ private static final String ASYNC_BYE = "async:bye:camel?delay=0";
+ private static final String INVALID =
"file:target/data/slip?fileExist=Append&tempPrefix=hello";
+
+ private final List<String> steps = new CopyOnWriteArrayList<>();
+ private final List<String> calls = new CopyOnWriteArrayList<>();
+
+ @Test
+ public void testRouterFailsAfterSyncStepThenAsyncStep() throws Exception {
+ steps.addAll(List.of("direct:foo", ASYNC_BYE));
+
+ assertRouterFailureFailsExchange();
+ }
+
+ @Test
+ public void testRouterFailsAfterTwoAsyncSteps() throws Exception {
+ steps.addAll(List.of(ASYNC_HI, ASYNC_BYE));
+
+ assertRouterFailureFailsExchange();
+ }
+
+ @Test
+ public void testRouterFailsAfterOneAsyncStep() throws Exception {
+ steps.add(ASYNC_BYE);
+
+ assertRouterFailureFailsExchange();
+ }
+
+ @Test
+ public void testRouterFailsAfterTwoSyncSteps() throws Exception {
+ steps.addAll(List.of("direct:foo", "direct:bar"));
+
+ assertRouterFailureFailsExchange();
+ }
+
+ @Test
+ public void testAnnotatedRouterFailsAfterSyncStepThenAsyncStep() throws
Exception {
+ steps.addAll(List.of("direct:foo", ASYNC_BYE));
+ MockEndpoint error = getMockEndpoint("mock:error");
+ error.expectedMessageCount(1);
+ getMockEndpoint("mock:result").expectedMessageCount(0);
+
+ template.sendBody("direct:annotated", "Hello Camel");
+
+ assertMockEndpointsSatisfied();
+ assertRouterFailure(error);
+ }
+
+ @Test
+ public void testRouterFailureCaughtByDoCatch() throws Exception {
+ steps.addAll(List.of("direct:foo", "direct:bar"));
+ getMockEndpoint("mock:caught").expectedMessageCount(1);
+ getMockEndpoint("mock:result").expectedMessageCount(0);
+ getMockEndpoint("mock:error").expectedMessageCount(0);
+
+ template.sendBody("direct:try", "Hello Camel");
+
+ assertMockEndpointsSatisfied();
+ assertEquals(steps.size() + 1, calls.size());
+ }
+
+ @Test
+ public void testRouterNotCalledAfterFailedAsyncStep() throws Exception {
+ steps.addAll(List.of("direct:foo",
"async:bye:camel?delay=0&failFirstAttempts=1", "mock:more"));
+ getMockEndpoint("mock:error").expectedMessageCount(1);
+ getMockEndpoint("mock:result").expectedMessageCount(0);
+ getMockEndpoint("mock:more").expectedMessageCount(0);
+
+ template.sendBody("direct:start", "Hello Camel");
+
+ assertMockEndpointsSatisfied();
+ // the router is asked for the 1st and the 2nd step only, as when the
failed step completed synchronously
+ assertEquals(2, calls.size());
+ }
+
+ @Test
+ public void testRoutingSlipIgnoresInvalidLastEndpointAfterAsyncStep()
throws Exception {
+ getMockEndpoint("mock:end").expectedBodiesReceived("Bye Camel");
+
+ template.sendBodyAndHeader("direct:slip", "Hello Camel", "slip",
ASYNC_BYE + "," + INVALID);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testRoutingSlipIgnoresInvalidLastEndpointAfterSyncStep()
throws Exception {
+ getMockEndpoint("mock:end").expectedBodiesReceived("Bye World");
+
+ template.sendBodyAndHeader("direct:slip", "Hello Camel", "slip",
"direct:foo," + INVALID);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ private void assertRouterFailureFailsExchange() throws Exception {
+ MockEndpoint error = getMockEndpoint("mock:error");
+ error.expectedMessageCount(1);
+ getMockEndpoint("mock:result").expectedMessageCount(0);
+
+ template.sendBody("direct:start", "Hello Camel");
+
+ assertMockEndpointsSatisfied();
+ assertRouterFailure(error);
+ }
+
+ private void assertRouterFailure(MockEndpoint error) {
+ Exception caught =
error.getReceivedExchanges().get(0).getProperty(Exchange.EXCEPTION_CAUGHT,
Exception.class);
+ Throwable cause = caught;
+ while (cause != null && !(cause instanceof IllegalStateException)) {
+ cause = cause.getCause();
+ }
+ assertInstanceOf(IllegalStateException.class, cause, "the router's
failure: " + caught);
+ assertEquals(steps.size() + 1, calls.size());
+ }
+
+ @DynamicRouter
+ public String route(String body) {
+ return slip(body);
+ }
+
+ public String slip(String body) {
+ int n = calls.size();
+ calls.add(body);
+ if (n < steps.size()) {
+ return steps.get(n);
+ }
+ throw new IllegalStateException("No route for " + body);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ context.addComponent("async", new MyAsyncComponent());
+
+ onException(IllegalStateException.class,
CamelExchangeException.class).handled(true).to("mock:error");
+
+ // an expression that throws (method() sets the exception on
the exchange instead)
+ from("direct:start").dynamicRouter(new ExpressionAdapter() {
+ @Override
+ public Object evaluate(Exchange exchange) {
+ return
slip(exchange.getMessage().getBody(String.class));
+ }
+ }).to("mock:result");
+
+ from("direct:try")
+ .doTry()
+ .dynamicRouter(new ExpressionAdapter() {
+ @Override
+ public Object evaluate(Exchange exchange) {
+ return
slip(exchange.getMessage().getBody(String.class));
+ }
+ }).end()
+ .to("mock:result")
+ .doCatch(IllegalStateException.class)
+ .to("mock:caught")
+ .end();
+
+ // a bean method annotated with @DynamicRouter (its expression
throws what the method throws)
+
from("direct:annotated").bean(AsyncEndpointDynamicRouterFailureTest.this,
"route").to("mock:result");
+
+
from("direct:slip").routingSlip(header("slip")).ignoreInvalidEndpoints().to("mock:end");
+
+ from("direct:foo").transform(constant("Bye World"));
+ from("direct:bar").transform(constant("Bye Bar"));
+ }
+ };
+ }
+}
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 7055213c78fc..0d6f85a9b627 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
@@ -262,6 +262,19 @@ This is what `xml` binding already did without its XML
data format. Add the JSON
(or `camel-jackson` for Jackson 2), or set `bindingMode` to `off`. With
`bindingMode` `auto`, a route that finds
neither a JSON nor an XML data format still starts, and logs a WARN as it
binds nothing.
+=== camel-core - Dynamic Router and Routing Slip go on the same way after
every step
+
+The steps of a Dynamic Router or Routing Slip are wrapped in the error handler
of the route, so from the second step on
+the EIP goes on through the code for a step that completed asynchronously.
That code now does what the code for a
+synchronous step did:
+
+* An exception thrown by the expression of a Dynamic Router (such as a bean
method annotated with `@DynamicRouter`, or
+ a custom `Expression`) after the second step fails the message. Before, it
was set on a copy of the message and lost:
+ the message went on as if the router had returned `null`, with the result of
an earlier step.
+* After a step that failed, the expression of a Dynamic Router is no longer
called again before the EIP stops.
+* With `ignoreInvalidEndpoints`, an endpoint whose producer cannot be created
is ignored also when it is the last one of
+ the slip. Before, the message failed with a
`FailedToCreateProducerException`.
+
=== camel-kamelet - the output data type of a Kamelet is applied when it hands
the message on
A Kamelet that declares a default output data type
(`spec.dataTypes.out.default`) now gives the message that type