This is an automated email from the ASF dual-hosted git repository.

Croway 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 4f9ffcdafb7b CAMEL-27008: camel-core - Aggregate EIP: a confirm that 
fails after a move to the dead letter channel is retried, and the exchange is 
not moved again
4f9ffcdafb7b is described below

commit 4f9ffcdafb7b67285551b40d5729cab7583aba7a
Author: croway <[email protected]>
AuthorDate: Mon Nov 16 09:40:34 2026 +0100

    CAMEL-27008: camel-core - Aggregate EIP: a confirm that fails after a move 
to the dead letter channel is retried, and the exchange is not moved again
    
    After moving an exhausted recovered exchange to the dead letter channel, the
    recover task called confirm unguarded: an exception ended the pass, and the
    exchange was recovered and moved to the dead letter channel again on the 
next
    pass, while the ids after it in the scan were skipped. A false result was
    ignored.
    
    The recover task now calls confirmWithResult there, and keeps the exchange 
as
    not confirmed when it throws or returns false, as onComplete does since
    CAMEL-26995, so the confirm is retried and the pass goes on. The redelivery
    state of the moved exchange is removed.
---
 .../spi/RecoverableAggregationRepository.java      |   9 +-
 .../processor/aggregate/AggregateProcessor.java    |  28 +++++-
 .../AggregateRecoverableConfirmFailureTest.java    | 110 ++++++++++++++++++---
 3 files changed, 130 insertions(+), 17 deletions(-)

diff --git 
a/core/camel-api/src/main/java/org/apache/camel/spi/RecoverableAggregationRepository.java
 
b/core/camel-api/src/main/java/org/apache/camel/spi/RecoverableAggregationRepository.java
index 22f16e31ebcb..f7f3acc81d90 100644
--- 
a/core/camel-api/src/main/java/org/apache/camel/spi/RecoverableAggregationRepository.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/spi/RecoverableAggregationRepository.java
@@ -140,11 +140,12 @@ public interface RecoverableAggregationRepository extends 
AggregationRepository
      * non-confirmed completed exchanges.
      * <p/>
      * It is not invoked for a group that was completed by its first exchange 
(for example with a completion size of 1),
-     * as such a group was never added to the repository, so there is nothing 
to confirm.
+     * as such a group was never added to the repository, so there is nothing 
to confirm. It is also invoked for a
+     * recovered exchange that was moved to the dead letter channel.
      * <p/>
-     * When it returns false or throws an exception, the exchange (which was 
processed) is kept as not confirmed: the
-     * recover task does not recover it, and invokes this method again for it, 
as long as {@link #scan(CamelContext)}
-     * returns its exchange id.
+     * When it returns false or throws an exception, the exchange (which was 
processed, or moved to the dead letter
+     * channel) is kept as not confirmed: the recover task does not recover 
it, and invokes this method again for it, as
+     * long as {@link #scan(CamelContext)} returns its exchange id.
      *
      * @param  camelContext the current CamelContext
      * @param  exchangeId   exchange id to confirm
diff --git 
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
index c4c0863df561..6bb0dc34acfb 100644
--- 
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
+++ 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
@@ -1917,8 +1917,10 @@ public class AggregateProcessor extends 
BaseProcessorSupport
                                                         exchange,
                                                         
exchange.getException());
                                     } else {
-                                        // it was ok, so confirm after it has 
been moved to dead letter channel, so we wont recover it again
-                                        recoverable.confirm(camelContext, 
exchangeId);
+                                        // it was ok, so it is done (remove 
its redelivery state as well), and confirm it,
+                                        // so we wont recover it again
+                                        
redeliveryState.remove(exchange.getExchangeId());
+                                        confirmMovedToDeadLetter(exchangeId);
                                     }
                                 } else {
                                     // update current redelivery state
@@ -1991,6 +1993,28 @@ public class AggregateProcessor extends 
BaseProcessorSupport
                 LOG.warn("Still unable to confirm removal of exchange {}.", 
exchangeId);
             }
         }
+
+        private void confirmMovedToDeadLetter(String exchangeId) {
+            boolean confirmed;
+            try {
+                confirmed = recoverable.confirmWithResult(camelContext, 
exchangeId);
+            } catch (VirtualMachineError e) {
+                // left as before: a JVM-level failure is not handled, and 
ends the recover task
+                throw e;
+            } catch (RuntimeException | Error e) {
+                // as when it returns false, and go on with the other 
exchanges of the scan
+                LOG.warn("Error confirming the recovered exchange with id: {}, 
which was moved to the dead letter channel."
+                         + " It is kept as not confirmed: the recover task 
will try to confirm it again, and will not move it"
+                         + " again.",
+                        exchangeId, e);
+                confirmed = false;
+            }
+            if (!confirmed) {
+                // keep it as not confirmed, as a processed exchange whose 
confirm failed (AggregateOnCompletion), so the
+                // recover task tries to confirm it again instead of 
recovering it and moving it again
+                unconfirmedCompleteExchanges.add(exchangeId);
+            }
+        }
     }
 
     @Override
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregateRecoverableConfirmFailureTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregateRecoverableConfirmFailureTest.java
index 406248ec2b16..d139cf6a01e1 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregateRecoverableConfirmFailureTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregateRecoverableConfirmFailureTest.java
@@ -47,11 +47,14 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
  * With a {@link RecoverableAggregationRepository}, an aggregated exchange 
that was processed must be sent once, also
  * when the repository fails to confirm it: the recover task confirms it 
again, and does not send it again. CAMEL-17613
  * did this for a confirm that returns {@code false}, CAMEL-26995 for a 
confirm that throws (for example
- * JdbcAggregationRepository while the database is not available).
+ * JdbcAggregationRepository while the database is not available). The same 
holds for an aggregated exchange that the
+ * recover task moved to the dead letter channel (CAMEL-27008): it is moved 
once.
  */
 public class AggregateRecoverableConfirmFailureTest extends ContextTestSupport 
{
 
     private final ConfirmFailingRepository repository = new 
ConfirmFailingRepository();
+    // the aggregated bodies whose processing fails
+    private final Set<String> failingBodies = ConcurrentHashMap.newKeySet();
 
     @Test
     public void testConfirmThrows() throws Exception {
@@ -93,26 +96,105 @@ public class AggregateRecoverableConfirmFailureTest 
extends ContextTestSupport {
         assertEquals(2, repository.confirms("C+D"), "C+D should be confirmed 
twice");
     }
 
+    @Test
+    public void testDeadLetterConfirmThrows() throws Exception {
+        // A+B fails every time: after 2 redeliveries the recover task moves 
it to the dead letter channel, and then
+        // confirms it, which throws once
+        failingBodies.add("A+B");
+        repository.throwOnConfirm("A+B", 1);
+
+        MockEndpoint dead = moveToDeadLetter("A+B");
+
+        // the recover task confirms A+B again
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
repository.completedBodies().isEmpty());
+        assertMovedOnce(dead, "A+B");
+        assertEquals(2, repository.confirms("A+B"), "A+B should be confirmed 
twice");
+    }
+
+    @Test
+    public void testDeadLetterConfirmReturnsFalse() throws Exception {
+        // a repository that keeps the exchange when its confirm returns 
false: the result must not be ignored
+        failingBodies.add("A+B");
+        repository.falseOnConfirm("A+B", 1);
+
+        MockEndpoint dead = moveToDeadLetter("A+B");
+
+        // the recover task confirms A+B again
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
repository.completedBodies().isEmpty());
+        assertMovedOnce(dead, "A+B");
+        assertEquals(2, repository.confirms("A+B"), "A+B should be confirmed 
twice");
+    }
+
+    @Test
+    public void testDeadLetterConfirmKeepsThrowing() throws Exception {
+        // the confirm of A+B after it was moved to the dead letter channel 
keeps failing, and A+B comes first in the
+        // scan: the recover task must move C+D to the dead letter channel and 
confirm it anyway
+        failingBodies.add("A+B");
+        failingBodies.add("C+D");
+        repository.throwOnConfirm("A+B", Integer.MAX_VALUE);
+
+        MockEndpoint dead = moveToDeadLetter("A+B", "C+D");
+
+        await("C+D confirmed while the confirm of A+B keeps 
failing").atMost(10, TimeUnit.SECONDS)
+                .until(() -> 
List.of("A+B").equals(repository.completedBodies()));
+        assertMovedOnce(dead, "A+B", "C+D");
+        assertTrue(repository.confirms("A+B") > 2, "The recover task should 
try to confirm A+B again");
+        assertEquals(1, repository.confirms("C+D"), "C+D should be confirmed 
once");
+    }
+
     /**
-     * Sends a group of two exchanges for each aggregated body ("A+B" is sent 
as A and B), and waits until each
-     * aggregated exchange is sent.
+     * Sends a group of two exchanges for each aggregated body ("A+B" is sent 
as A and B).
      */
-    private MockEndpoint sendGroups(String... aggregatedBodies) throws 
Exception {
-        MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedBodiesReceived((Object[]) aggregatedBodies);
+    private void send(String... aggregatedBodies) {
         for (int i = 0; i < aggregatedBodies.length; i++) {
             for (String body : aggregatedBodies[i].split("\\+")) {
                 template.sendBodyAndHeader("direct:start", body, "id", i);
             }
         }
+    }
+
+    /**
+     * Sends a group of two exchanges for each aggregated body, and waits 
until each aggregated exchange is sent.
+     */
+    private MockEndpoint sendGroups(String... aggregatedBodies) throws 
Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived((Object[]) aggregatedBodies);
+        send(aggregatedBodies);
         mock.assertIsSatisfied();
         return mock;
     }
 
-    private void assertSentOnce(MockEndpoint mock, String... aggregatedBodies) 
{
+    /**
+     * Sends a group of two exchanges for each aggregated body, whose 
processing fails, and waits until the recover task
+     * has moved each aggregated exchange to the dead letter channel.
+     */
+    private MockEndpoint moveToDeadLetter(String... aggregatedBodies) throws 
Exception {
+        MockEndpoint dead = getMockEndpoint("mock:dead");
+        dead.expectedMinimumMessageCount(aggregatedBodies.length);
+        send(aggregatedBodies);
+        dead.assertIsSatisfied();
+        return dead;
+    }
+
+    private void assertMovedOnce(MockEndpoint dead, String... 
aggregatedBodies) {
+        awaitCompleteRecoverPass();
+
+        List<String> moved = new ArrayList<>();
+        for (Exchange exchange : dead.getReceivedExchanges()) {
+            moved.add(exchange.getMessage().getBody(String.class));
+        }
+        assertEquals(List.of(aggregatedBodies), moved,
+                "Each aggregated exchange should be moved to the dead letter 
channel once");
+    }
+
+    private void awaitCompleteRecoverPass() {
         // let the recover task run completely at least once more (a pass is 
complete when the next scan starts)
         int scans = repository.scans.get();
         await().atMost(10, TimeUnit.SECONDS).until(() -> 
repository.scans.get() >= scans + 2);
+    }
+
+    private void assertSentOnce(MockEndpoint mock, String... aggregatedBodies) 
{
+        awaitCompleteRecoverPass();
 
         List<String> received = new ArrayList<>();
         for (Exchange exchange : mock.getReceivedExchanges()) {
@@ -131,7 +213,13 @@ public class AggregateRecoverableConfirmFailureTest 
extends ContextTestSupport {
                 from("direct:start")
                         .aggregate(header("id"), new 
BodyInAggregatingStrategy()).aggregationRepository(repository)
                         .completionSize(2)
-                        .to("mock:result");
+                        .to("mock:result")
+                        .process(exchange -> {
+                            String body = 
exchange.getMessage().getBody(String.class);
+                            if (failingBodies.contains(body)) {
+                                throw new IllegalArgumentException("Simulated 
failure: cannot process " + body);
+                            }
+                        });
             }
         };
     }
@@ -140,7 +228,7 @@ public class AggregateRecoverableConfirmFailureTest extends 
ContextTestSupport {
      * An in-memory recoverable repository which, like 
JdbcAggregationRepository, moves the exchange of a completed
      * group to a completed store, and deletes it from there when it is 
confirmed. The scan returns the completed
      * exchanges in the order they were completed. A confirm can be made to 
throw, or to return {@code false}, for an
-     * aggregated body.
+     * aggregated body. A recovered exchange is moved to mock:dead after 2 
redeliveries.
      */
     private static final class ConfirmFailingRepository extends ServiceSupport
             implements RecoverableAggregationRepository {
@@ -277,7 +365,7 @@ public class AggregateRecoverableConfirmFailureTest extends 
ContextTestSupport {
 
         @Override
         public String getDeadLetterUri() {
-            return null;
+            return "mock:dead";
         }
 
         @Override
@@ -286,7 +374,7 @@ public class AggregateRecoverableConfirmFailureTest extends 
ContextTestSupport {
 
         @Override
         public int getMaximumRedeliveries() {
-            return 0;
+            return 2;
         }
     }
 }

Reply via email to