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 f2b23dba3da9 CAMEL-26431: camel-core - ThrottlingExceptionRoutePolicy:
do not count a failure on a suppressed exception (#28783)
f2b23dba3da9 is described below
commit f2b23dba3da92850cf05201fc1baa8d51f9ae64d
Author: allthingssecurity <[email protected]>
AuthorDate: Sun Nov 1 13:50:05 2026 +0530
CAMEL-26431: camel-core - ThrottlingExceptionRoutePolicy: do not count a
failure on a suppressed exception (#28783)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
...xceptionRoutePolicySuppressedExceptionTest.java | 206 +++++++++++++++++++++
.../throttling/ThrottlingExceptionRoutePolicy.java | 25 ++-
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 29 +++
.../modules/ROOT/pages/route-policy.adoc | 3 +
4 files changed, 256 insertions(+), 7 deletions(-)
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicySuppressedExceptionTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicySuppressedExceptionTest.java
new file mode 100644
index 000000000000..993c65e2cc8b
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicySuppressedExceptionTest.java
@@ -0,0 +1,206 @@
+/*
+ * 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.throttle;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.throttling.ThrottlingExceptionRoutePolicy;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+/**
+ * The exceptions of {@link ThrottlingExceptionRoutePolicy} are matched
against the exception the exchange fails with
+ * and its causes, as onException and doCatch do, and not against its
suppressed exceptions. Each route has its own
+ * policy that opens its circuit on the first failure it counts.
+ */
+class ThrottlingExceptionRoutePolicySuppressedExceptionTest extends
ContextTestSupport {
+
+ private final ThrottlingExceptionRoutePolicy finallyPolicy =
policy(IOException.class);
+ private final ThrottlingExceptionRoutePolicy onExceptionPolicy =
policy(IOException.class);
+ private final ThrottlingExceptionRoutePolicy redeliveryPolicy =
policy(IOException.class);
+ private final ThrottlingExceptionRoutePolicy addedPolicy =
policy(IOException.class);
+ private final ThrottlingExceptionRoutePolicy directPolicy =
policy(IOException.class);
+ private final ThrottlingExceptionRoutePolicy causePolicy =
policy(IOException.class);
+ private final ThrottlingExceptionRoutePolicy listedInTryPolicy =
policy(IOException.class);
+ private final ThrottlingExceptionRoutePolicy allPolicy = policy();
+
+ private static ThrottlingExceptionRoutePolicy policy(Class<?>...
exceptions) {
+ // a threshold of 1: the first failure that counts opens the circuit
+ return new ThrottlingExceptionRoutePolicy(1, 60000, 60000,
List.of(exceptions));
+ }
+
+ private Exchange send(String uri) {
+ return template.send(uri, e -> e.getMessage().setBody("hello"));
+ }
+
+ private static void assertSuppressed(Class<?> expected, Exception
exception) {
+ assertEquals(1, exception.getSuppressed().length, "suppressed
exceptions of " + exception);
+ assertInstanceOf(expected, exception.getSuppressed()[0]);
+ }
+
+ private static void assertNotCounted(ThrottlingExceptionRoutePolicy
policy) {
+ assertEquals(0, policy.getFailures());
+ assertEquals(1, policy.getSuccess());
+ assertEquals("closed", policy.getStateAsString());
+ }
+
+ private static void assertCounted(ThrottlingExceptionRoutePolicy policy) {
+ assertEquals(1, policy.getFailures());
+ assertEquals(0, policy.getSuccess());
+ assertEquals("opened", policy.getStateAsString());
+ }
+
+ @Test
+ void listedExceptionThrownInDoFinallyIsNotCounted() {
+ Exchange out = send("direct:finally");
+
+ assertInstanceOf(IllegalStateException.class, out.getException());
+ assertSuppressed(IOException.class, out.getException());
+ assertNotCounted(finallyPolicy);
+ }
+
+ @Test
+ void listedExceptionHandledByAFailingOnExceptionIsNotCounted() {
+ Exchange out = send("direct:onException");
+
+ // the FatalFallbackErrorHandler fails the exchange with the exception
of the onException block
+ assertInstanceOf(IllegalStateException.class, out.getException());
+ assertSuppressed(IOException.class, out.getException());
+ assertNotCounted(onExceptionPolicy);
+ }
+
+ @Test
+ void
listedExceptionOfTheFirstAttemptIsNotCountedWhenTheRedeliveryFailsWithAnother()
{
+ Exchange out = send("direct:redelivery");
+
+ assertInstanceOf(IllegalStateException.class, out.getException());
+ assertSuppressed(IOException.class, out.getException());
+ assertNotCounted(redeliveryPolicy);
+ }
+
+ @Test
+ void listedExceptionAddedAsSuppressedByAProcessorIsNotCounted() {
+ Exchange out = send("direct:added");
+
+ assertInstanceOf(IllegalStateException.class, out.getException());
+ assertSuppressed(IOException.class, out.getException());
+ assertNotCounted(addedPolicy);
+ }
+
+ // control
+ @Test
+ void listedExceptionThrownDirectlyIsCounted() {
+ Exchange out = send("direct:direct");
+
+ assertInstanceOf(IOException.class, out.getException());
+ assertCounted(directPolicy);
+ }
+
+ // control
+ @Test
+ void listedExceptionAsTheCauseIsCounted() {
+ Exchange out = send("direct:cause");
+
+ assertInstanceOf(IllegalStateException.class, out.getException());
+ assertInstanceOf(IOException.class, out.getException().getCause());
+ assertCounted(causePolicy);
+ }
+
+ // control: the exception of the doTry block decides, also when the
doFinally block throws an exception not listed
+ @Test
+ void listedExceptionThrownInDoTryIsCountedWhenDoFinallyThrowsAnother() {
+ Exchange out = send("direct:listedInTry");
+
+ assertInstanceOf(IOException.class, out.getException());
+ assertSuppressed(IllegalStateException.class, out.getException());
+ assertCounted(listedInTryPolicy);
+ }
+
+ // control: without exceptions to throttle on every failure counts
+ @Test
+ void everyFailureIsCountedWithoutExceptions() {
+ Exchange out = send("direct:all");
+
+ assertInstanceOf(IllegalStateException.class, out.getException());
+ assertSuppressed(IOException.class, out.getException());
+ assertCounted(allPolicy);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:finally").routePolicy(finallyPolicy)
+ .doTry()
+ .throwException(new IllegalStateException("try"))
+ .doFinally()
+ .throwException(new IOException("finally"))
+ .end();
+
+ from("direct:onException").routePolicy(onExceptionPolicy)
+ .onException(IOException.class).handled(true)
+ .throwException(new
IllegalStateException("onException"))
+ .end()
+ .throwException(new IOException("route"));
+
+ from("direct:redelivery").routePolicy(redeliveryPolicy)
+
.errorHandler(defaultErrorHandler().maximumRedeliveries(1).redeliveryDelay(0))
+ .process(e -> {
+ if
(e.getIn().getHeader(Exchange.REDELIVERY_COUNTER) == null) {
+ throw new IOException("first attempt");
+ }
+ throw new IllegalStateException("redelivery");
+ });
+
+ from("direct:added").routePolicy(addedPolicy)
+ .process(e -> {
+ // as try-with-resources does when closing a
resource fails
+ IllegalStateException failure = new
IllegalStateException("process");
+ failure.addSuppressed(new IOException("close"));
+ throw failure;
+ });
+
+ from("direct:direct").routePolicy(directPolicy)
+ .throwException(new IOException("direct"));
+
+ from("direct:cause").routePolicy(causePolicy)
+ .throwException(new IllegalStateException("wrapper",
new IOException("cause")));
+
+ from("direct:listedInTry").routePolicy(listedInTryPolicy)
+ .doTry()
+ .throwException(new IOException("try"))
+ .doFinally()
+ .throwException(new
IllegalStateException("finally"))
+ .end();
+
+ from("direct:all").routePolicy(allPolicy)
+ .doTry()
+ .throwException(new IllegalStateException("try"))
+ .doFinally()
+ .throwException(new IOException("finally"))
+ .end();
+ }
+ };
+ }
+}
diff --git
a/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
b/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
index 9ad737f8da7a..ebdcf84572aa 100644
---
a/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
+++
b/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
@@ -41,6 +41,7 @@ import org.apache.camel.spi.Configurer;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.RoutePolicy;
import org.apache.camel.support.RoutePolicySupport;
+import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.TimeSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -320,13 +321,7 @@ public class ThrottlingExceptionRoutePolicy extends
RoutePolicySupport implement
// (ie) assume we throttle on all exceptions
answer = true;
} else {
- for (Class<?> exception : throttledExceptions) {
- // will look in exception hierarchy
- if (exchange.getException(exception) != null) {
- answer = true;
- break;
- }
- }
+ answer = matchesException(exchange.getException());
}
}
@@ -339,6 +334,22 @@ public class ThrottlingExceptionRoutePolicy extends
RoutePolicySupport implement
return answer;
}
+ /**
+ * Whether the exception, or one of its causes, is one of the exceptions
to throttle on. Suppressed exceptions (such
+ * as an exception thrown in a doFinally block) are not matched, the same
as onException and doCatch.
+ */
+ private boolean matchesException(Throwable exception) {
+ // walk the caused by hierarchy
+ for (Throwable throwable :
ObjectHelper.createExceptionIterable(exception)) {
+ for (Class<?> type : throttledExceptions) {
+ if (type.isInstance(throwable)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
private void recordFailureTime(long time) {
failureLock.lock();
try {
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 eda9a6370f42..c919c687b9a1 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
@@ -195,6 +195,35 @@ of the exception the attempt fails with in these cases:
To fail over in these cases, add the type of the exception the attempt fails
with to the list.
+=== camel-core - ThrottlingExceptionRoutePolicy does not count a failure on a
suppressed exception
+
+The exceptions of `ThrottlingExceptionRoutePolicy` (its `exceptions` option,
or the list given to its constructor) are
+now matched against the exception the exchange fails with and its causes only,
as `onException`, `doCatch` and the
+failover load balancer do. Before, they also matched the suppressed exceptions
of that exception, so such a failure
+counted towards opening the circuit. Since 4.23, an exception thrown in a
`doFinally` block is added as a suppressed
+exception to the exception from the `doTry` block, so a `doFinally` that threw
one of the listed exceptions counted the
+failure, although the exception from the `doTry` block was not listed. The
same applies, since 4.23, when the `onWhen`
+predicate of a `doCatch` throws an exception: the exchange fails with the
exception of the predicate, and the original
+exception is added to it as a suppressed exception.
+
+A failure also no longer counts when one of the listed exceptions is only the
suppressed exception of the exception the
+exchange fails with in other cases, for example:
+
+* An `onException` block (or another error handler) fails while it handles the
exception: the exchange fails with the
+ new exception, and the original exception is added to it as a suppressed
exception (by the
+ `FatalFallbackErrorHandler`).
+* The error handler redelivers and the last redelivery fails with a different
exception than the first attempt: the
+ exchange fails with the exception of the last redelivery, and the exception
of the first attempt is added to it as a
+ suppressed exception.
+* An `onCompletion` that runs before the consumer (`modeBeforeConsumer`) or a
saga compensation fails, or a processor or
+ component adds a suppressed exception itself (such as Java's
try-with-resources when closing a resource fails).
+
+Unlike the `doFinally` and `onWhen` cases, these are not 4.23 regressions;
they change older behaviour intentionally, so
+that the policy matches exceptions as `onException` and `doCatch` do.
+
+To count these failures, add the type of the exception the exchange fails with
to `exceptions`, or leave `exceptions`
+empty to count every failure.
+
=== camel-core - JsonObject and JsonArray convert to their JSON text
The camel-util-json types `org.apache.camel.util.json.JsonObject` and
`JsonArray` (given by `convertBodyTo: JsonObject`,
diff --git a/docs/user-manual/modules/ROOT/pages/route-policy.adoc
b/docs/user-manual/modules/ROOT/pages/route-policy.adoc
index 3322ef7a43c1..d852695212d9 100644
--- a/docs/user-manual/modules/ROOT/pages/route-policy.adoc
+++ b/docs/user-manual/modules/ROOT/pages/route-policy.adoc
@@ -39,6 +39,9 @@ fast.
* `org.apache.camel.throttling.ThrottlingExceptionRoutePolicy` - a throttling
based policy modeled after the circuit breaker. This policy will stop consuming
from an endpoint based on the type of exceptions that are thrown and the
threshold setting.
+The exception an exchange fails with and its causes are matched against the
exceptions to throttle on (the same as
+`onException` and `doCatch`). Suppressed exceptions, such as an exception
thrown in a `doFinally` block, are not
+matched.
* `org.apache.camel.impl.cluster.ClusteredRoutePolicy` - is the foundation for
`camel-cluster`
which handles Camel routes to become active if a node becomes leader in a
cluster.