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 f895736dfa38 CAMEL-26734: camel-core - a step after one that was 
redelivered gets all its redeliveries (#29416)
f895736dfa38 is described below

commit f895736dfa38e8c16e8408985f2826badd0956f5
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 8 16:46:32 2026 +0100

    CAMEL-26734: camel-core - a step after one that was redelivered gets all 
its redeliveries (#29416)
    
    A step after one that was redelivered no longer inherits the redelivery 
counter header of that step: the counter is reset when the exchange continues, 
and the over-the-limit state of nested error handlers (CAMEL-23469) is kept in 
an exchange property instead, so a later failure gets all its redeliveries 
instead of going straight to the dead letter channel.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../camel/catalog/docs/dead-letter-channel.adoc    |   6 +
 .../modules/eips/pages/dead-letter-channel.adoc    |   6 +
 .../errorhandler/RedeliveryErrorHandler.java       |  43 +++++-
 .../RedeliveryAfterRecoveredStepTest.java          | 147 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |  10 ++
 5 files changed, 210 insertions(+), 2 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/dead-letter-channel.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/dead-letter-channel.adoc
index b6148e33bb4b..40e3919a65d0 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/dead-letter-channel.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/dead-letter-channel.adoc
@@ -584,6 +584,12 @@ will append the following headers to the message with the 
state of the redeliver
 `retryWhile` or have unlimited maximum redelivery configured.
 |===
 
+When a redelivered step succeeds, the headers stay on the message, so the next 
steps can see that it was redelivered
+and how many times. A later failure, of another step of the route or of a 
route that gets the message next (such as
+over `seda`), is redelivered as any other: its redelivery attempts count from 
1 again, and it gets all the
+redeliveries of its error handler. When the dead letter channel or an 
`onException` handles a failure, or a failure is
+continued, the headers are removed.
+
 === Which endpoint failed?
 
 During routing messages, Camel will store an exchange property
diff --git 
a/core/camel-core-engine/src/main/docs/modules/eips/pages/dead-letter-channel.adoc
 
b/core/camel-core-engine/src/main/docs/modules/eips/pages/dead-letter-channel.adoc
index b6148e33bb4b..40e3919a65d0 100644
--- 
a/core/camel-core-engine/src/main/docs/modules/eips/pages/dead-letter-channel.adoc
+++ 
b/core/camel-core-engine/src/main/docs/modules/eips/pages/dead-letter-channel.adoc
@@ -584,6 +584,12 @@ will append the following headers to the message with the 
state of the redeliver
 `retryWhile` or have unlimited maximum redelivery configured.
 |===
 
+When a redelivered step succeeds, the headers stay on the message, so the next 
steps can see that it was redelivered
+and how many times. A later failure, of another step of the route or of a 
route that gets the message next (such as
+over `seda`), is redelivered as any other: its redelivery attempts count from 
1 again, and it gets all the
+redeliveries of its error handler. When the dead letter channel or an 
`onException` handles a failure, or a failure is
+continued, the headers are removed.
+
 === Which endpoint failed?
 
 During routing messages, Camel will store an exchange property
diff --git 
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
index 28f1b4b27230..6b57ddb71fd7 100644
--- 
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
+++ 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
@@ -98,6 +98,11 @@ public abstract class RedeliveryErrorHandler extends 
ErrorHandlerSupport
     // API. As any plain property, it shows in Exchange.getProperties() and in 
tracer or debugger listings, and
     // removeProperties("*") removes it.
     private static final String CONTINUED_EXCEPTION = 
"CamelErrorHandlerContinuedException";
+    // The redelivery counter header of a redelivery that is over: the step 
was redelivered and then succeeded. The
+    // headers stay on the message, so the next steps can see that it was 
redelivered, but a later failure (of another
+    // step, or of a route that gets the exchange or a copy of it next) does 
not count on from them (CAMEL-26734).
+    // Internal, as CONTINUED_EXCEPTION.
+    private static final String REDELIVERY_OVER = 
"CamelErrorHandlerRedeliveryOver";
 
     // factory
     protected PooledExchangeTaskFactory taskFactory;
@@ -744,6 +749,7 @@ public abstract class RedeliveryErrorHandler extends 
ErrorHandlerSupport
                 exchange.getIn().removeHeader(Exchange.REDELIVERED);
                 exchange.getIn().removeHeader(Exchange.REDELIVERY_COUNTER);
                 exchange.getIn().removeHeader(Exchange.REDELIVERY_MAX_COUNTER);
+                exchange.removeProperty(REDELIVERY_OVER);
                 exchange.getExchangeExtension().setRedeliveryExhausted(false);
                 exchange.getExchangeExtension().setRedeliveryCounter(-1);
                 exchange.getExchangeExtension().setRedeliveryMaxCounter(-1);
@@ -1651,6 +1657,9 @@ public abstract class RedeliveryErrorHandler extends 
ErrorHandlerSupport
                 // only process if the exchange hasn't failed
                 // and it has not been handled by the error processor
                 if (isDone(exchange)) {
+                    if (exchange.getException() == null) {
+                        markRedeliveryOver();
+                    }
                     AsyncCallback cb = callback;
                     taskFactory.release(this);
                     reactiveExecutor.schedule(cb);
@@ -1661,6 +1670,20 @@ public abstract class RedeliveryErrorHandler extends 
ErrorHandlerSupport
             });
         }
 
+        /**
+         * The redelivered step succeeded, so its redelivery is over. The 
redelivery headers stay, so the next steps can
+         * see that the message was redelivered, but they are marked as over: 
otherwise a later failure, of another step
+         * of the route or of a route that gets the exchange (or a copy of it, 
such as over seda) next, would count on
+         * from them (incrementRedeliveryCounter respects a higher header, for 
a nested error handler that failed) and
+         * get fewer redeliveries, or none (CAMEL-26734).
+         */
+        private void markRedeliveryOver() {
+            Integer counter = 
exchange.getIn().getHeader(Exchange.REDELIVERY_COUNTER, Integer.class);
+            if (counter != null) {
+                exchange.setProperty(REDELIVERY_OVER, counter);
+            }
+        }
+
         protected void prepareExchangeForContinue(Exchange exchange, boolean 
isDeadLetterChannel) {
             Exception caught = exchange.getException();
             if (caught != null) {
@@ -1676,6 +1699,7 @@ public abstract class RedeliveryErrorHandler extends 
ErrorHandlerSupport
             exchange.getIn().removeHeader(Exchange.REDELIVERED);
             exchange.getIn().removeHeader(Exchange.REDELIVERY_COUNTER);
             exchange.getIn().removeHeader(Exchange.REDELIVERY_MAX_COUNTER);
+            exchange.removeProperty(REDELIVERY_OVER);
             // also reset the exchange counter; a continued exchange is not 
redelivered
             exchange.getExchangeExtension().setRedeliveryCounter(-1);
             exchange.getExchangeExtension().setRedeliveryMaxCounter(-1);
@@ -1896,6 +1920,7 @@ public abstract class RedeliveryErrorHandler extends 
ErrorHandlerSupport
                 exchange.getIn().removeHeader(Exchange.REDELIVERED);
                 exchange.getIn().removeHeader(Exchange.REDELIVERY_COUNTER);
                 exchange.getIn().removeHeader(Exchange.REDELIVERY_MAX_COUNTER);
+                exchange.removeProperty(REDELIVERY_OVER);
                 exchange.getExchangeExtension().setRedeliveryExhausted(false);
                 exchange.getExchangeExtension().setRedeliveryCounter(-1);
                 exchange.getExchangeExtension().setRedeliveryMaxCounter(-1);
@@ -2313,9 +2338,12 @@ public abstract class RedeliveryErrorHandler extends 
ErrorHandlerSupport
             // because user routes may remove headers (e.g. removeHeaders("*"))
             // however, if the header carries a higher value from a nested 
error handler
             // (e.g. per-endpoint redelivery in routing slip), respect that 
accumulated count
-            Integer counter = in.getHeader(Exchange.REDELIVERY_COUNTER, 
Integer.class);
+            // (but not one of a redelivery that is over, see 
markRedeliveryOver)
+            Integer counter = liveRedeliveryCounter(exchange);
             int base = counter != null ? Math.max(redeliveryCounter, counter) 
: redeliveryCounter;
             int next = base + 1;
+            // the header counts this redelivery now, so it is no longer of 
one that is over
+            exchange.removeProperty(REDELIVERY_OVER);
             in.setHeader(Exchange.REDELIVERY_COUNTER, next);
             in.setHeader(Exchange.REDELIVERED, Boolean.TRUE);
             exchange.getExchangeExtension().setRedeliveryCounter(next);
@@ -2327,6 +2355,17 @@ public abstract class RedeliveryErrorHandler extends 
ErrorHandlerSupport
             return next;
         }
 
+        /**
+         * The redelivery counter header, unless it is of a redelivery that is 
over (see markRedeliveryOver).
+         */
+        private Integer liveRedeliveryCounter(Exchange exchange) {
+            Integer counter = 
exchange.getIn().getHeader(Exchange.REDELIVERY_COUNTER, Integer.class);
+            if (counter != null && 
counter.equals(exchange.getProperty(REDELIVERY_OVER))) {
+                return null;
+            }
+            return counter;
+        }
+
         /**
          * Decrements the redelivery counter when the failure handler takes 
over without processing the redelivery.
          */
@@ -2334,7 +2373,7 @@ public abstract class RedeliveryErrorHandler extends 
ErrorHandlerSupport
             Message in = exchange.getIn();
             // use the same Math.max(header, field) pattern as 
incrementRedeliveryCounter:
             // a nested error handler may have bumped the counter in the 
header beyond our field
-            Integer counter = in.getHeader(Exchange.REDELIVERY_COUNTER, 
Integer.class);
+            Integer counter = liveRedeliveryCounter(exchange);
             int base = counter != null ? Math.max(redeliveryCounter, counter) 
: redeliveryCounter;
             int prev = Math.max(base - 1, 0);
             in.setHeader(Exchange.REDELIVERY_COUNTER, prev);
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryAfterRecoveredStepTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryAfterRecoveredStepTest.java
new file mode 100644
index 000000000000..c1207e8eb4a5
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryAfterRecoveredStepTest.java
@@ -0,0 +1,147 @@
+/*
+ * 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.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.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * CAMEL-26734: a step that fails after another step was redelivered and then 
succeeded gets its own redeliveries. The
+ * redelivery headers of the step that recovered stay on the message, but they 
must not count for the next failure, in
+ * the same route or in a route that gets a copy of the exchange over seda.
+ */
+public class RedeliveryAfterRecoveredStepTest extends ContextTestSupport {
+
+    private final AtomicInteger oneCalls = new AtomicInteger();
+    private final AtomicInteger twoCalls = new AtomicInteger();
+    private final List<Object> twoSaw = new CopyOnWriteArrayList<>();
+
+    @Test
+    public void testNextStepIsRedelivered() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:dead").expectedMessageCount(0);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+        // step one: the first call and 2 redeliveries; step two: the first 
call and 1 redelivery
+        assertEquals(3, oneCalls.get());
+        assertEquals(2, twoCalls.get());
+        // the first call of step two still sees the headers of step one, and 
its redelivery counts from 1
+        assertEquals(List.of(2, 1), twoSaw);
+    }
+
+    @Test
+    public void testRouteAfterSedaHandOffIsRedelivered() throws Exception {
+        getMockEndpoint("mock:shipped").expectedMessageCount(1);
+        getMockEndpoint("mock:dead").expectedMessageCount(0);
+
+        template.sendBody("direct:handOff", "Hello World");
+
+        MockEndpoint.assertIsSatisfied(context);
+        assertEquals(3, oneCalls.get());
+        assertEquals(2, twoCalls.get());
+        assertEquals(List.of(2, 1), twoSaw);
+    }
+
+    @Test
+    public void testRedeliveryHeadersStayAfterTheStepRecovered() throws 
Exception {
+        MockEndpoint result = getMockEndpoint("mock:afterOne");
+        result.expectedMessageCount(1);
+
+        template.sendBody("direct:recoverOnly", "Hello World");
+
+        assertMockEndpointsSatisfied();
+        Exchange exchange = result.getReceivedExchanges().get(0);
+        // the next steps can still see that the message was redelivered
+        assertEquals(Boolean.TRUE, 
exchange.getMessage().getHeader(Exchange.REDELIVERED));
+        assertEquals(2, 
exchange.getMessage().getHeader(Exchange.REDELIVERY_COUNTER));
+        assertEquals(2, 
exchange.getMessage().getHeader(Exchange.REDELIVERY_MAX_COUNTER));
+        assertEquals(3, oneCalls.get());
+    }
+
+    @Test
+    public void testRoutingSlipRedeliveriesAfterARecoveredStep() throws 
Exception {
+        // as RoutingSlipNestedSlipRedeliveryTest: the routing slip step is 
redelivered twice, also when a step was
+        // redelivered and recovered before it
+        getMockEndpoint("mock:inner").expectedMessageCount(3);
+        getMockEndpoint("mock:dead").expectedMessageCount(1);
+
+        template.sendBody("direct:nested", "Hello World");
+
+        assertMockEndpointsSatisfied();
+        assertEquals(3, oneCalls.get());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                
errorHandler(deadLetterChannel("mock:dead").maximumRedeliveries(2).redeliveryDelay(0));
+
+                from("direct:start")
+                        .process(this::stepOne)
+                        .process(this::stepTwo)
+                        .to("mock:result");
+
+                from("direct:handOff")
+                        .process(this::stepOne)
+                        .to("seda:shipping");
+                from("seda:shipping")
+                        .process(this::stepTwo)
+                        .to("mock:shipped");
+
+                from("direct:nested")
+                        .process(this::stepOne)
+                        .routingSlip(constant("direct:slipThenFail"));
+                from("direct:slipThenFail").errorHandler(noErrorHandler())
+                        .routingSlip(constant("mock:inner"))
+                        .throwException(new IllegalStateException("boom"));
+
+                from("direct:recoverOnly")
+                        .process(this::stepOne)
+                        .to("mock:afterOne");
+            }
+
+            // fails twice, and succeeds at the second redelivery
+            private void stepOne(Exchange exchange) {
+                if (oneCalls.incrementAndGet() < 3) {
+                    throw new IllegalStateException("step one fails");
+                }
+            }
+
+            // fails once, at its first call
+            private void stepTwo(Exchange exchange) {
+                Integer counter = 
exchange.getMessage().getHeader(Exchange.REDELIVERY_COUNTER, Integer.class);
+                twoSaw.add(counter != null ? counter : "none");
+                if (twoCalls.incrementAndGet() == 1) {
+                    throw new IllegalStateException("step two fails once");
+                }
+            }
+        };
+    }
+}
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 49418725e57c..75245301ff3e 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
@@ -273,6 +273,16 @@ before calling an endpoint). Before, such a recovery was 
not counted. `ExchangeH
 `true` when the error handler redelivered the exchange, without looking at the 
header only. An exchange whose failure
 is continued (`continued(true)`) no longer keeps its redelivery counter, as 
with a handled failure.
 
+=== camel-core - a step after a redelivered step gets all its redeliveries
+
+When a step of a route was redelivered and then succeeded, a later failure (of 
another step of the route, or of a
+route that gets the exchange or a copy of it next, such as over `seda`) 
counted its redeliveries on from the
+`CamelRedeliveryCounter` header that the first step left on the message. With 
`maximumRedeliveries(2)`, a step after one
+that needed two redeliveries was not redelivered at all: the message went to 
the dead letter channel at its first
+failure. Now such a failure is redelivered as any other, from attempt 1, so 
such routes can redeliver more often than
+before. The `CamelRedelivered`, `CamelRedeliveryCounter` and 
`CamelRedeliveryMaxCounter` headers stay on the message
+after the redelivered step succeeded, as before.
+
 === camel-core - a nested split no longer changes the processed items of the 
outer split
 
 With `errorThreshold` or `maxFailedRecords` on a split that contains another 
split, the inner split changed the number of

Reply via email to