This is an automated email from the ASF dual-hosted git repository.
apupier 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 f43584ac99c5 CAMEL-24925: camel-core - onException redeliveryDelay and
maximumRedeliveryDelay should accept durations such as 5s
f43584ac99c5 is described below
commit f43584ac99c5022c671144bf8de83a41fbdeab8d
Author: smjain <[email protected]>
AuthorDate: Wed Sep 23 16:50:39 2026 +0530
CAMEL-24925: camel-core - onException redeliveryDelay and
maximumRedeliveryDelay should accept durations such as 5s
The redeliveryDelay and maximumRedeliveryDelay options of the redelivery
policy are typed as java.time.Duration, and the error handler parses them
with CamelContextHelper.parseDuration. The redelivery policy of onException
is built by ExceptionPolicy, which still parsed them with parseLong. So
onException(...).redeliveryDelay("5s"), or the same in XML or YAML, started
fine, but on the first failure building the policy failed with "Error
parsing [5s] as a java.lang.Long". No redelivery happened and the original
exception was replaced by that parse error.
Parse both options with parseDuration, as the error handler does. Plain
numbers are still milliseconds. The deprecated
ErrorHandlerReifier.createRedeliveryPolicy(Map...) had the same copy of the
code and is changed the same way.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../processor/errorhandler/ExceptionPolicy.java | 5 +-
.../reifier/errorhandler/ErrorHandlerReifier.java | 5 +-
.../OnExceptionRedeliveryDelayDurationTest.java | 96 ++++++++++++++++++++++
3 files changed, 102 insertions(+), 4 deletions(-)
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/ExceptionPolicy.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/ExceptionPolicy.java
index 26b61e2f17d3..071eb128ed06 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/ExceptionPolicy.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/ExceptionPolicy.java
@@ -195,7 +195,7 @@ public class ExceptionPolicy {
}
if (definition.get(RedeliveryOption.redeliveryDelay) != null) {
answer.setRedeliveryDelay(
- CamelContextHelper.parseLong(context,
definition.get(RedeliveryOption.redeliveryDelay)));
+ CamelContextHelper.parseDuration(context,
definition.get(RedeliveryOption.redeliveryDelay)).toMillis());
}
if (definition.get(RedeliveryOption.asyncDelayedRedelivery) !=
null) {
answer.setAsyncDelayedRedelivery(
@@ -231,7 +231,8 @@ public class ExceptionPolicy {
}
if (definition.get(RedeliveryOption.maximumRedeliveryDelay) !=
null) {
answer.setMaximumRedeliveryDelay(
- CamelContextHelper.parseLong(context,
definition.get(RedeliveryOption.maximumRedeliveryDelay)));
+ CamelContextHelper.parseDuration(context,
definition.get(RedeliveryOption.maximumRedeliveryDelay))
+ .toMillis());
}
if (definition.get(RedeliveryOption.logStackTrace) != null) {
answer.setLogStackTrace(
diff --git
a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/errorhandler/ErrorHandlerReifier.java
b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/errorhandler/ErrorHandlerReifier.java
index 683756c3a321..efec07c9af87 100644
---
a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/errorhandler/ErrorHandlerReifier.java
+++
b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/errorhandler/ErrorHandlerReifier.java
@@ -152,7 +152,7 @@ public abstract class ErrorHandlerReifier<T extends
ErrorHandlerFactory> extends
}
if (definition.get(RedeliveryOption.redeliveryDelay) != null) {
answer.setRedeliveryDelay(
- CamelContextHelper.parseLong(context,
definition.get(RedeliveryOption.redeliveryDelay)));
+ CamelContextHelper.parseDuration(context,
definition.get(RedeliveryOption.redeliveryDelay)).toMillis());
}
if (definition.get(RedeliveryOption.asyncDelayedRedelivery) !=
null) {
answer.setAsyncDelayedRedelivery(
@@ -188,7 +188,8 @@ public abstract class ErrorHandlerReifier<T extends
ErrorHandlerFactory> extends
}
if (definition.get(RedeliveryOption.maximumRedeliveryDelay) !=
null) {
answer.setMaximumRedeliveryDelay(
- CamelContextHelper.parseLong(context,
definition.get(RedeliveryOption.maximumRedeliveryDelay)));
+ CamelContextHelper.parseDuration(context,
definition.get(RedeliveryOption.maximumRedeliveryDelay))
+ .toMillis());
}
if (definition.get(RedeliveryOption.logStackTrace) != null) {
answer.setLogStackTrace(
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRedeliveryDelayDurationTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRedeliveryDelayDurationTest.java
new file mode 100644
index 000000000000..23b97b705d2d
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionRedeliveryDelayDurationTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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.onexception;
+
+import java.util.EnumMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.processor.errorhandler.ExceptionPolicy;
+import
org.apache.camel.processor.errorhandler.ExceptionPolicy.RedeliveryOption;
+import org.apache.camel.processor.errorhandler.RedeliveryPolicy;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+/**
+ * The redelivery delays on onException are durations, the same as on the
error handler.
+ */
+public class OnExceptionRedeliveryDelayDurationTest extends ContextTestSupport
{
+
+ private final AtomicInteger attempts = new AtomicInteger();
+
+ @Test
+ public void testDurationRedeliveryDelay() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:error");
+ mock.expectedMessageCount(1);
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ assertEquals(3, attempts.get());
+ Exception cause =
mock.getReceivedExchanges().get(0).getProperty(Exchange.EXCEPTION_CAUGHT,
Exception.class);
+ assertInstanceOf(IllegalArgumentException.class, cause);
+ assertEquals("Forced", cause.getMessage());
+ }
+
+ @Test
+ public void testDurationValues() {
+ Map<RedeliveryOption, String> options = new
EnumMap<>(RedeliveryOption.class);
+ options.put(RedeliveryOption.redeliveryDelay, "5s");
+ options.put(RedeliveryOption.maximumRedeliveryDelay, "1m30s");
+ ExceptionPolicy policy = new ExceptionPolicy(
+ null, null, false, false, false, null, null, null, null, null,
null, options,
+ List.of(IllegalArgumentException.class.getName()));
+
+ RedeliveryPolicy answer = policy.createRedeliveryPolicy(context, new
RedeliveryPolicy());
+ assertEquals(5000, answer.getRedeliveryDelay());
+ assertEquals(90000, answer.getMaximumRedeliveryDelay());
+
+ // plain numbers are still milliseconds
+ options.put(RedeliveryOption.redeliveryDelay, "250");
+ options.put(RedeliveryOption.maximumRedeliveryDelay, "1000");
+ answer = policy.createRedeliveryPolicy(context, new
RedeliveryPolicy());
+ assertEquals(250, answer.getRedeliveryDelay());
+ assertEquals(1000, answer.getMaximumRedeliveryDelay());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ onException(IllegalArgumentException.class)
+
.maximumRedeliveries(2).redeliveryDelay("10ms").maximumRedeliveryDelay("1s")
+ .handled(true)
+ .to("mock:error");
+
+ from("direct:start")
+ .process(e -> {
+ attempts.incrementAndGet();
+ throw new IllegalArgumentException("Forced");
+ });
+ }
+ };
+ }
+}