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 a18d67217610 CAMEL-26908: camel-core - an Error of an on completion no
longer skips the other on completions (#29899)
a18d67217610 is described below
commit a18d6721761045b949035cbf6f14dfd560aa945c
Author: allthingssecurity <[email protected]>
AuthorDate: Sat Nov 14 15:12:21 2026 +0530
CAMEL-26908: camel-core - an Error of an on completion no longer skips the
other on completions (#29899)
UnitOfWorkHelper caught only Exception around each on completion
(Synchronization) and its route aware callbacks, so an Error that is
not a VirtualMachineError escaped: the on completions after it did not
run (such as the commit or rollback of the consumer), the exchange
stayed in the inflight registry, and the caller got the Error.
doneSynchronization, the onBeforeRoute and onAfterRoute loops and
doneUow now also catch such an Error, log it as they log an exception,
and go on. A VirtualMachineError is thrown on as before. The 4.24
upgrade guide has the entry.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../processor/SplitterShareUnitOfWorkTest.java | 13 +-
.../processor/SynchronizationThrowsErrorTest.java | 180 +++++++++++++++++++++
.../processor/UnitOfWorkDoneThrowsErrorTest.java | 71 ++++++++
.../org/apache/camel/support/UnitOfWorkHelper.java | 28 +++-
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 9 ++
5 files changed, 290 insertions(+), 11 deletions(-)
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/SplitterShareUnitOfWorkTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/SplitterShareUnitOfWorkTest.java
index 3cd672bc59d6..263b5a99c380 100644
---
a/core/camel-core/src/test/java/org/apache/camel/processor/SplitterShareUnitOfWorkTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/SplitterShareUnitOfWorkTest.java
@@ -33,6 +33,8 @@ public class SplitterShareUnitOfWorkTest extends
ContextTestSupport {
private final List<UnitOfWork> uows = new ArrayList<>();
private final List<UnitOfWork> doneUows = new ArrayList<>();
private final List<String> doneBodies = new ArrayList<>();
+ private final List<Integer> doneLineCounts = new ArrayList<>();
+ private final List<Integer> doneResultCounts = new ArrayList<>();
@Test
public void testShareUnitOfWork() throws Exception {
@@ -59,6 +61,11 @@ public class SplitterShareUnitOfWorkTest extends
ContextTestSupport {
Assertions.assertEquals("A+B+C", doneBodies.get(0));
Assertions.assertEquals("A+B+C", doneBodies.get(1));
Assertions.assertEquals("A+B+C", doneBodies.get(2));
+
+ // should only be invoked after all is complete (3 line and 1 result):
asserted here, as an assertion in an on
+ // completion does not fail the test
+ Assertions.assertEquals(List.of(3, 3, 3), doneLineCounts);
+ Assertions.assertEquals(List.of(1, 1, 1), doneResultCounts);
}
@Override
@@ -76,10 +83,8 @@ public class SplitterShareUnitOfWorkTest extends
ContextTestSupport {
doneBodies.add(b);
var u = exchange.getUnitOfWork();
doneUows.add(u);
-
- // should only be invoked after all is
complete (3 line and 1 result)
- Assertions.assertEquals(3,
getMockEndpoint("mock:line").getReceivedCounter());
- Assertions.assertEquals(1,
getMockEndpoint("mock:result").getReceivedCounter());
+
doneLineCounts.add(getMockEndpoint("mock:line").getReceivedCounter());
+
doneResultCounts.add(getMockEndpoint("mock:result").getReceivedCounter());
}
});
})
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/SynchronizationThrowsErrorTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/SynchronizationThrowsErrorTest.java
new file mode 100644
index 000000000000..b64db470622a
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/SynchronizationThrowsErrorTest.java
@@ -0,0 +1,180 @@
+/*
+ * 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;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Route;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.Synchronization;
+import org.apache.camel.spi.SynchronizationRouteAware;
+import org.apache.camel.support.SynchronizationAdapter;
+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.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * An Error that is not a VirtualMachineError, thrown by an on completion (a
{@link Synchronization}) of an exchange, is
+ * logged and ignored as an exception is: the other on completions still run
(such as the commit of a consumer), and the
+ * unit of work ends (the exchange does not stay inflight).
+ */
+class SynchronizationThrowsErrorTest extends ContextTestSupport {
+
+ private final List<String> calls = new CopyOnWriteArrayList<>();
+
+ @Test
+ void testErrorOfOnCompleteRunsTheOtherOnCompletions() throws Exception {
+ Exchange out = send("direct:start", () -> new AssertionError("Forced
onComplete"));
+
+ assertNull(out.getException());
+ assertDone(List.of("failing:onComplete", "counting:onComplete"));
+ }
+
+ @Test
+ void testErrorOfOnFailureRunsTheOtherOnCompletions() throws Exception {
+ Exchange out = send("direct:fail", () -> new AssertionError("Forced
onFailure"));
+
+ assertInstanceOf(IllegalArgumentException.class, out.getException());
+ assertDone(List.of("failing:onFailure", "counting:onFailure"));
+ }
+
+ @Test
+ void testExceptionOfOnCompleteRunsTheOtherOnCompletions() throws Exception
{
+ // control: an exception was already logged and ignored
+ Exchange out = send("direct:start", () -> new
IllegalStateException("Forced onComplete"));
+
+ assertNull(out.getException());
+ assertDone(List.of("failing:onComplete", "counting:onComplete"));
+ }
+
+ @Test
+ void testErrorOfRouteAwareCallbacksRunsTheOtherOnCompletions() throws
Exception {
+ Exchange out = template.asyncSend("direct:routeAware", e ->
e.getMessage().setBody("Hello"))
+ .get(10, TimeUnit.SECONDS);
+
+ assertNull(out.getException());
+ // the route aware callbacks of both on completions (the failing one
first), when the exchange enters and leaves
+ // route b and leaves the route that added them, and then their on
completions
+ assertDone(List.of("failing:onBeforeRoute:b",
"counting:onBeforeRoute:b",
+ "failing:onAfterRoute:b", "counting:onAfterRoute:b",
+ "failing:onAfterRoute:routeAware",
"counting:onAfterRoute:routeAware",
+ "failing:onComplete", "counting:onComplete"));
+ }
+
+ private Exchange send(String uri, Supplier<Throwable> failure) throws
Exception {
+ // bounded, as a guard: before, the Error reached the caller
+ return template.asyncSend(uri, e -> {
+ e.getMessage().setBody("Hello");
+ e.setProperty("failure", failure);
+ }).get(10, TimeUnit.SECONDS);
+ }
+
+ private void assertDone(List<String> expected) {
+ await().atMost(5, TimeUnit.SECONDS).untilAsserted(() ->
assertEquals(expected, calls));
+ assertEquals(0, context.getInflightRepository().size(), "The exchange
must not stay inflight");
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ // the on completions run last added first: the failing one
runs before the counting one
+ from("direct:start").routeId("start")
+ .process(e -> addOnCompletions(e, false))
+ .to("mock:result");
+
+ from("direct:fail").routeId("fail")
+ .process(e -> addOnCompletions(e, false))
+ .throwException(new IllegalArgumentException("Forced
route"));
+
+ from("direct:routeAware").routeId("routeAware")
+ .process(e -> {
+ e.setProperty("failure", (Supplier<Throwable>) ()
-> new AssertionError("Forced"));
+ addOnCompletions(e, true);
+ })
+ .to("direct:b");
+
+ from("direct:b").routeId("b")
+ .to("mock:result");
+ }
+ };
+ }
+
+ private void addOnCompletions(Exchange exchange, boolean routeAware) {
+ @SuppressWarnings("unchecked")
+ Supplier<Throwable> failure = exchange.getProperty("failure",
Supplier.class);
+ exchange.getExchangeExtension().addOnCompletion(new
Recording("counting", routeAware, null));
+ exchange.getExchangeExtension().addOnCompletion(new
Recording("failing", routeAware, failure));
+ }
+
+ private final class Recording extends SynchronizationAdapter implements
SynchronizationRouteAware {
+ private final String name;
+ private final boolean routeAware;
+ private final Supplier<Throwable> failure;
+
+ Recording(String name, boolean routeAware, Supplier<Throwable>
failure) {
+ this.name = name;
+ this.routeAware = routeAware;
+ this.failure = failure;
+ }
+
+ @Override
+ public void onComplete(Exchange exchange) {
+ call("onComplete");
+ }
+
+ @Override
+ public void onFailure(Exchange exchange) {
+ call("onFailure");
+ }
+
+ @Override
+ public void onBeforeRoute(Route route, Exchange exchange) {
+ call("onBeforeRoute:" + route.getRouteId());
+ }
+
+ @Override
+ public void onAfterRoute(Route route, Exchange exchange) {
+ call("onAfterRoute:" + route.getRouteId());
+ }
+
+ @Override
+ public SynchronizationRouteAware getRouteSynchronization() {
+ return routeAware ? this : null;
+ }
+
+ private void call(String what) {
+ calls.add(name + ":" + what);
+ if (failure != null) {
+ Throwable t = failure.get();
+ if (t instanceof Error error) {
+ throw error;
+ }
+ throw (RuntimeException) t;
+ }
+ }
+ }
+}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/UnitOfWorkDoneThrowsErrorTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/UnitOfWorkDoneThrowsErrorTest.java
new file mode 100644
index 000000000000..8b69e23feb10
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/UnitOfWorkDoneThrowsErrorTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.engine.DefaultUnitOfWork;
+import org.apache.camel.spi.UnitOfWorkFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * An Error that is not a VirtualMachineError, thrown by the done of a unit of
work, is logged and ignored, as an
+ * exception is (UnitOfWorkHelper.doneUow).
+ */
+class UnitOfWorkDoneThrowsErrorTest extends ContextTestSupport {
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+
context.getCamelContextExtension().addContextPlugin(UnitOfWorkFactory.class,
exchange -> new DefaultUnitOfWork(
+ exchange) {
+ @Override
+ public void done(Exchange exchange) {
+ super.done(exchange);
+ throw new AssertionError("Forced done");
+ }
+ });
+ return context;
+ }
+
+ @Test
+ void testErrorOfDoneIsLoggedAndIgnored() throws Exception {
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ // bounded, as a guard: before, the Error reached the caller
+ Exchange out = template.asyncSend("direct:start", e ->
e.getMessage().setBody("Hello")).get(10, TimeUnit.SECONDS);
+
+ assertMockEndpointsSatisfied();
+ assertNull(out.getException());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:start").to("mock:result");
+ }
+ };
+ }
+}
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/UnitOfWorkHelper.java
b/core/camel-support/src/main/java/org/apache/camel/support/UnitOfWorkHelper.java
index 8fb1f769ffbc..9acb01fe06a9 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/UnitOfWorkHelper.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/UnitOfWorkHelper.java
@@ -52,7 +52,11 @@ public final class UnitOfWorkHelper {
// unit of work is done
try {
uow.done(exchange);
- } catch (Exception e) {
+ } catch (VirtualMachineError e) {
+ // left as before (thrown on)
+ throw e;
+ } catch (Throwable e) {
+ // also an Error, as for an exception
LOG.warn("Exception occurred during done UnitOfWork for Exchange:
{}. This exception will be ignored.",
exchange.getExchangeId(), e);
}
@@ -108,8 +112,12 @@ public final class UnitOfWorkHelper {
LOG.trace("Invoking synchronization.onComplete: {} with {}",
synchronization, exchange);
synchronization.onComplete(exchange);
}
- } catch (Exception e) {
- // must catch exceptions to ensure all synchronizations have a
chance to run
+ } catch (VirtualMachineError e) {
+ // left as before (thrown on)
+ throw e;
+ } catch (Throwable e) {
+ // must catch exceptions (and an Error, which would skip the next
synchronizations, such as the commit of a
+ // consumer, and the end of the unit of work) to ensure all
synchronizations have a chance to run
LOG.warn("Exception occurred during onCompletion. This exception
will be ignored.", e);
}
}
@@ -155,8 +163,11 @@ public final class UnitOfWorkHelper {
try {
log.trace("Invoking synchronization.onBeforeRoute: {} with
{}", synchronization, exchange);
routeSynchronization.onBeforeRoute(route, exchange);
- } catch (Exception e) {
- // must catch exceptions to ensure all synchronizations
have a chance to run
+ } catch (VirtualMachineError e) {
+ // left as before (thrown on)
+ throw e;
+ } catch (Throwable e) {
+ // must catch exceptions (and an Error) to ensure all
synchronizations have a chance to run
log.warn("Exception occurred during onBeforeRoute. This
exception will be ignored.", e);
}
}
@@ -188,8 +199,11 @@ public final class UnitOfWorkHelper {
try {
log.trace("Invoking synchronization.onAfterRoute: {} with
{}", synchronization, exchange);
routeSynchronization.onAfterRoute(route, exchange);
- } catch (Exception e) {
- // must catch exceptions to ensure all synchronizations
have a chance to run
+ } catch (VirtualMachineError e) {
+ // left as before (thrown on)
+ throw e;
+ } catch (Throwable e) {
+ // must catch exceptions (and an Error) to ensure all
synchronizations have a chance to run
log.warn("Exception occurred during onAfterRoute. This
exception will be ignored.", e);
}
}
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 a6dbbcbb5b8d..ae9477e56599 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
@@ -935,6 +935,15 @@ exception was dropped and that callback continued the
exchange as a success. A s
and logged at WARN; before, the exchange was continued a second time. There is
no option for the old behaviour; fix the
processor to set the exception on the exchange and call the callback, as an
`AsyncProcessor` must.
+=== camel-core - an Error of an on completion is logged, and the other on
completions run
+
+An `Error` that is not a `VirtualMachineError` (such as an `AssertionError` or
a `NoClassDefFoundError`), thrown by an
+on completion (`Synchronization`) of an exchange or by its `onBeforeRoute` /
`onAfterRoute` callback, is now logged at
+WARN and ignored, as an exception already was. The other on completions still
run (such as the commit or rollback of
+the consumer) and the exchange is no longer left inflight. Before, the `Error`
was thrown on to the code that ran the
+on completions (such as the consumer, or the caller of a `ProducerTemplate`).
An assertion inside an on completion no
+longer fails the caller: check its outcome in the route or the test instead.
+
=== camel-core - an Error of a contract transformer or validator, or in a
transacted step, fails the exchange
An `Error` that is not a `VirtualMachineError` (such as an `AssertionError` or
a `NoClassDefFoundError`) now fails the