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 e09460c0207f CAMEL-26633: camel-core - Loop EIP: breakOnShutdown 
should not stick after route suspend or restart (#29369)
e09460c0207f is described below

commit e09460c0207f156dc1b625d3c63b286d87c0cf06
Author: allthingssecurity <[email protected]>
AuthorDate: Sat Nov 7 15:05:21 2026 +0530

    CAMEL-26633: camel-core - Loop EIP: breakOnShutdown should not stick after 
route suspend or restart (#29369)
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../org/apache/camel/processor/LoopProcessor.java  |  14 ++-
 .../processor/LoopBreakOnShutdownRestartTest.java  | 111 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |   7 ++
 3 files changed, 131 insertions(+), 1 deletion(-)

diff --git 
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/LoopProcessor.java
 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/LoopProcessor.java
index 3a01705c934b..636ecf0ea1d2 100644
--- 
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/LoopProcessor.java
+++ 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/LoopProcessor.java
@@ -50,7 +50,8 @@ public class LoopProcessor extends 
BaseDelegateProcessorSupport
     private String id;
     private String routeId;
     private String stepId;
-    private boolean shutdownPending;
+    // set when stopping (not on suspend), and reset on start
+    private volatile boolean shutdownPending;
     private final ReactiveExecutor reactiveExecutor;
     private final Expression expression;
     private final Predicate predicate;
@@ -88,6 +89,13 @@ public class LoopProcessor extends 
BaseDelegateProcessorSupport
         }
     }
 
+    @Override
+    protected void doStart() throws Exception {
+        // reset flag as the route may be restarted
+        shutdownPending = false;
+        super.doStart();
+    }
+
     @Override
     public boolean deferShutdown(ShutdownRunningTask shutdownRunningTask) {
         return !breakOnShutdown;
@@ -101,6 +109,10 @@ public class LoopProcessor extends 
BaseDelegateProcessorSupport
 
     @Override
     public void prepareShutdown(boolean suspendOnly, boolean forced) {
+        if (suspendOnly) {
+            // a suspended route is resumed later without being started again
+            return;
+        }
         shutdownPending = true;
     }
 
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/LoopBreakOnShutdownRestartTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/LoopBreakOnShutdownRestartTest.java
new file mode 100644
index 000000000000..85f81cc6a4cc
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/LoopBreakOnShutdownRestartTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A loop with breakOnShutdown must loop again after its route was stopped and 
started, or suspended and resumed.
+ */
+class LoopBreakOnShutdownRestartTest extends ContextTestSupport {
+
+    @Test
+    void testLoopAfterRouteStopAndStart() throws Exception {
+        sendAndExpectThreeIterations("a");
+
+        context.getRouteController().stopRoute("loop");
+        context.getRouteController().startRoute("loop");
+
+        sendAndExpectThreeIterations("b");
+    }
+
+    @Test
+    void testLoopAfterRouteSuspendAndResume() throws Exception {
+        sendAndExpectThreeIterations("a");
+
+        context.getRouteController().suspendRoute("loop");
+        context.getRouteController().resumeRoute("loop");
+
+        sendAndExpectThreeIterations("b");
+    }
+
+    @Test
+    void testLoopAfterCamelContextSuspendAndResume() throws Exception {
+        sendAndExpectThreeIterations("a");
+
+        context.suspend();
+        context.resume();
+
+        sendAndExpectThreeIterations("b");
+    }
+
+    @Test
+    void testLoopBreaksOnShutdownAfterRestart() throws Exception {
+        context.getRouteController().stopRoute("slow");
+        context.getRouteController().startRoute("slow");
+
+        MockEndpoint slow = getMockEndpoint("mock:slow");
+        template.sendBody("seda:slow", "c");
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
slow.getReceivedCounter() >= 1);
+
+        context.getRouteController().stopRoute("slow");
+
+        int received = slow.getReceivedCounter();
+        assertTrue(received < 100, "the loop did not break on shutdown: " + 
received + " iterations");
+    }
+
+    private void sendAndExpectThreeIterations(String body) throws Exception {
+        MockEndpoint iteration = getMockEndpoint("mock:iteration");
+        MockEndpoint result = getMockEndpoint("mock:result");
+        iteration.reset();
+        result.reset();
+        iteration.expectedBodiesReceived(body, body, body);
+        result.expectedBodiesReceived(body);
+
+        template.sendBody("direct:start", body);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start").routeId("loop")
+                        .loop(3).breakOnShutdown()
+                            .to("mock:iteration")
+                        .end()
+                        .to("mock:result");
+
+                from("seda:slow").routeId("slow")
+                        .loop(100).breakOnShutdown()
+                            .delay(20)
+                            .to("mock:slow")
+                        .end();
+            }
+        };
+    }
+}
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 f3b79ce4f4d6..db3944bfc278 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
@@ -314,6 +314,13 @@ or payload from the body after a timed out `pollEnrich` 
must keep it before the
 with the same message id, headers and attachments, so after a timeout it is no 
longer of the message type of the
 consumer (such as `GenericFileMessage` or `JmsMessage`).
 
+=== camel-core - Loop EIP breakOnShutdown
+
+The Loop EIP with `breakOnShutdown` now only breaks out when the route (or 
Camel) is stopped, and no longer on suspend.
+A suspend waits for the loop like any other inflight exchange. A loop that 
never ends on its own (such as a
+`doWhile` with `breakOnShutdown`) is therefore no longer broken at once by a 
suspend: the suspend waits until the
+shutdown timeout, and the forced suspend then breaks the loop and stops the 
consumer of the route.
+
 === camel-core - doFinally runs all its steps after a called route handled a 
failure
 
 When a `doTry` calls a route (such as with `direct`) whose error handler 
handles a failure (a dead letter channel or a

Reply via email to