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 fc22af4649e4 CAMEL-26985: camel-controlbus - a stop of the route of
the exchange no longer waits for the shutdown timeout (#30042)
fc22af4649e4 is described below
commit fc22af4649e48cc9441ee6b5897fd1c250b01d13
Author: Federico Mariani <[email protected]>
AuthorDate: Sun Nov 15 23:05:56 2026 +0100
CAMEL-26985: camel-controlbus - a stop of the route of the exchange no
longer waits for the shutdown timeout (#30042)
* CAMEL-26985: camel-controlbus - a stop of the route of the exchange n…
…o longer waits for the shutdown timeout
A stop, suspend, restart or fail of the route that routes the exchange, or
of the route it came from, now runs on the
component executor as with async=true, since a graceful stop on the routing
thread waited for that same exchange.
* CAMEL-26985: camel-controlbus - fail keeps the exception of the excha…
…nge when run asynchronously, and a stop of a calling route is detected
The fail action reads the exception of the exchange on the calling thread,
as the exchange may be done (and reset when
pooled) by the time the task runs. A route on the route stack of the
exchange, such as a route that called the current
one with direct, is also a route of the exchange, so a stop of it runs
asynchronously as well.
* CAMEL-26985: camel-controlbus - docs: split the long sentences about …
…stopping the route of the exchange
Co-Authored-By: Claude <[email protected]>
---
.../camel/catalog/components/controlbus.json | 2 +-
.../camel/catalog/docs/controlbus-component.adoc | 23 +++
.../camel/component/controlbus/controlbus.json | 2 +-
.../src/main/docs/controlbus-component.adoc | 23 +++
.../component/controlbus/ControlBusEndpoint.java | 4 +
.../component/controlbus/ControlBusProducer.java | 62 ++++++--
.../controlbus/ControlBusFailCurrentRouteTest.java | 100 ++++++++++++
.../controlbus/ControlBusStopCurrentRouteTest.java | 177 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 9 ++
.../modules/ROOT/pages/graceful-shutdown.adoc | 5 +
.../dsl/ControlBusEndpointBuilderFactory.java | 10 ++
11 files changed, 402 insertions(+), 15 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/controlbus.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/controlbus.json
index 2c0db8d547b4..43b926da1018 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/controlbus.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/controlbus.json
@@ -36,7 +36,7 @@
"command": { "index": 0, "kind": "path", "displayName": "Command",
"group": "producer", "label": "", "required": true, "type": "enum", "javaType":
"java.lang.String", "enum": [ "route", "language" ], "deprecated": false,
"autowired": false, "secret": false, "description": "Command can be either
route or language" },
"language": { "index": 1, "kind": "path", "displayName": "Language",
"group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "org.apache.camel.spi.Language", "enum": [ "bean", "constant",
"datasonnet", "exchangeProperty", "file", "groovy", "header", "hl7terser",
"java", "joor", "jq", "jsonpath", "mvel", "python", "ref", "simple", "spel",
"tokenize", "xpath", "xquery", "xtokenize" ], "deprecated": false, "autowired":
false, "secret": false, "description": "All [...]
"action": { "index": 2, "kind": "parameter", "displayName": "Action",
"group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "java.lang.String", "enum": [ "start", "stop", "fail", "suspend",
"resume", "restart", "status", "stats" ], "deprecated": false, "autowired":
false, "secret": false, "description": "To denote an action that can be either:
start, stop, or status. To either start or stop a route, or to get the status
of the route as output in the message [...]
- "async": { "index": 3, "kind": "parameter", "displayName": "Async",
"group": "producer", "label": "", "required": false, "type": "boolean",
"javaType": "boolean", "deprecated": false, "autowired": false, "secret":
false, "defaultValue": false, "description": "Whether to execute the control
bus task asynchronously. Important: If this option is enabled, then any result
from the task is not set on the Exchange. This is only possible if executing
tasks synchronously." },
+ "async": { "index": 3, "kind": "parameter", "displayName": "Async",
"group": "producer", "label": "", "required": false, "type": "boolean",
"javaType": "boolean", "deprecated": false, "autowired": false, "secret":
false, "defaultValue": false, "description": "Whether to execute the control
bus task asynchronously. Important: If this option is enabled, then any result
from the task is not set on the Exchange. This is only possible if executing
tasks synchronously. A stop, suspend, res [...]
"loggingLevel": { "index": 4, "kind": "parameter", "displayName": "Logging
Level", "group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "org.apache.camel.LoggingLevel", "enum": [ "TRACE", "DEBUG",
"INFO", "WARN", "ERROR", "OFF" ], "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "INFO", "description": "Logging level used for
logging when task is done, or if any exceptions occurred during processing the
task." },
"restartDelay": { "index": 5, "kind": "parameter", "displayName": "Restart
Delay", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 1000, "description": "The delay in millis to use when
restarting a route." },
"routeId": { "index": 6, "kind": "parameter", "displayName": "Route Id",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "description": "To specify a route by its id. The special
keyword current indicates the current route." },
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/controlbus-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/controlbus-component.adoc
index e691bba99b5e..136e74e58e5c 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/controlbus-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/controlbus-component.adoc
@@ -132,6 +132,29 @@ This route checks every minute that the route `foo` runs,
and starts it when it
action: start
----
+The `current` route id refers to the route that routes the message, so a route
can stop itself:
+
+[source,yaml]
+----
+- route:
+ from:
+ uri: kafka:orders
+ steps:
+ - to:
+ uri: log:orders
+ - to:
+ uri: controlbus:route
+ parameters:
+ routeId: current
+ action: stop
+----
+
+A graceful stop waits for the inflight messages of the route, including the
one that asks for the stop.
+So a `stop`, `suspend`, `restart` or `fail` always runs asynchronously, even
without `async=true`, when it targets the current route, a route that called it
(such as with `direct`), or the route the message came from.
+The message continues, and the route stops once it is done.
+A copy of the message made by an EIP such as Split or Multicast does not know
the routes that called the route of that EIP.
+From such a copy, a stop of one of those routes needs `async=true`, unless it
is the route the message came from.
+
[[ControlBus-Gettingperformancestatistics]]
=== Getting performance statistics
diff --git
a/components/camel-controlbus/src/generated/resources/META-INF/org/apache/camel/component/controlbus/controlbus.json
b/components/camel-controlbus/src/generated/resources/META-INF/org/apache/camel/component/controlbus/controlbus.json
index 2c0db8d547b4..43b926da1018 100644
---
a/components/camel-controlbus/src/generated/resources/META-INF/org/apache/camel/component/controlbus/controlbus.json
+++
b/components/camel-controlbus/src/generated/resources/META-INF/org/apache/camel/component/controlbus/controlbus.json
@@ -36,7 +36,7 @@
"command": { "index": 0, "kind": "path", "displayName": "Command",
"group": "producer", "label": "", "required": true, "type": "enum", "javaType":
"java.lang.String", "enum": [ "route", "language" ], "deprecated": false,
"autowired": false, "secret": false, "description": "Command can be either
route or language" },
"language": { "index": 1, "kind": "path", "displayName": "Language",
"group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "org.apache.camel.spi.Language", "enum": [ "bean", "constant",
"datasonnet", "exchangeProperty", "file", "groovy", "header", "hl7terser",
"java", "joor", "jq", "jsonpath", "mvel", "python", "ref", "simple", "spel",
"tokenize", "xpath", "xquery", "xtokenize" ], "deprecated": false, "autowired":
false, "secret": false, "description": "All [...]
"action": { "index": 2, "kind": "parameter", "displayName": "Action",
"group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "java.lang.String", "enum": [ "start", "stop", "fail", "suspend",
"resume", "restart", "status", "stats" ], "deprecated": false, "autowired":
false, "secret": false, "description": "To denote an action that can be either:
start, stop, or status. To either start or stop a route, or to get the status
of the route as output in the message [...]
- "async": { "index": 3, "kind": "parameter", "displayName": "Async",
"group": "producer", "label": "", "required": false, "type": "boolean",
"javaType": "boolean", "deprecated": false, "autowired": false, "secret":
false, "defaultValue": false, "description": "Whether to execute the control
bus task asynchronously. Important: If this option is enabled, then any result
from the task is not set on the Exchange. This is only possible if executing
tasks synchronously." },
+ "async": { "index": 3, "kind": "parameter", "displayName": "Async",
"group": "producer", "label": "", "required": false, "type": "boolean",
"javaType": "boolean", "deprecated": false, "autowired": false, "secret":
false, "defaultValue": false, "description": "Whether to execute the control
bus task asynchronously. Important: If this option is enabled, then any result
from the task is not set on the Exchange. This is only possible if executing
tasks synchronously. A stop, suspend, res [...]
"loggingLevel": { "index": 4, "kind": "parameter", "displayName": "Logging
Level", "group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "org.apache.camel.LoggingLevel", "enum": [ "TRACE", "DEBUG",
"INFO", "WARN", "ERROR", "OFF" ], "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "INFO", "description": "Logging level used for
logging when task is done, or if any exceptions occurred during processing the
task." },
"restartDelay": { "index": 5, "kind": "parameter", "displayName": "Restart
Delay", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 1000, "description": "The delay in millis to use when
restarting a route." },
"routeId": { "index": 6, "kind": "parameter", "displayName": "Route Id",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "description": "To specify a route by its id. The special
keyword current indicates the current route." },
diff --git
a/components/camel-controlbus/src/main/docs/controlbus-component.adoc
b/components/camel-controlbus/src/main/docs/controlbus-component.adoc
index e691bba99b5e..136e74e58e5c 100644
--- a/components/camel-controlbus/src/main/docs/controlbus-component.adoc
+++ b/components/camel-controlbus/src/main/docs/controlbus-component.adoc
@@ -132,6 +132,29 @@ This route checks every minute that the route `foo` runs,
and starts it when it
action: start
----
+The `current` route id refers to the route that routes the message, so a route
can stop itself:
+
+[source,yaml]
+----
+- route:
+ from:
+ uri: kafka:orders
+ steps:
+ - to:
+ uri: log:orders
+ - to:
+ uri: controlbus:route
+ parameters:
+ routeId: current
+ action: stop
+----
+
+A graceful stop waits for the inflight messages of the route, including the
one that asks for the stop.
+So a `stop`, `suspend`, `restart` or `fail` always runs asynchronously, even
without `async=true`, when it targets the current route, a route that called it
(such as with `direct`), or the route the message came from.
+The message continues, and the route stops once it is done.
+A copy of the message made by an EIP such as Split or Multicast does not know
the routes that called the route of that EIP.
+From such a copy, a stop of one of those routes needs `async=true`, unless it
is the route the message came from.
+
[[ControlBus-Gettingperformancestatistics]]
=== Getting performance statistics
diff --git
a/components/camel-controlbus/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java
b/components/camel-controlbus/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java
index 9b7ca3b779bc..79e8f0a35f81 100644
---
a/components/camel-controlbus/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java
+++
b/components/camel-controlbus/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java
@@ -172,6 +172,10 @@ public class ControlBusEndpoint extends DefaultEndpoint {
* <p/>
* Important: If this option is enabled, then any result from the task is
not set on the Exchange. This is only
* possible if executing tasks synchronously.
+ * <p/>
+ * A stop, suspend, restart or fail of a route that routes the exchange
(the current route, or a route that called
+ * it, such as with direct), or of the route the exchange came from, is
always executed asynchronously, as the route
+ * waits for the exchange to complete before it stops.
*/
public void setAsync(boolean async) {
this.async = async;
diff --git
a/components/camel-controlbus/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java
b/components/camel-controlbus/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java
index f9d71556f355..842fc3299526 100644
---
a/components/camel-controlbus/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java
+++
b/components/camel-controlbus/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java
@@ -34,6 +34,7 @@ import org.apache.camel.Route;
import org.apache.camel.ServiceStatus;
import org.apache.camel.spi.CamelLogger;
import org.apache.camel.spi.Language;
+import org.apache.camel.spi.UnitOfWork;
import org.apache.camel.support.DefaultAsyncProducer;
import org.apache.camel.support.ExchangeHelper;
import org.apache.camel.util.ObjectHelper;
@@ -92,6 +93,12 @@ public class ControlBusProducer extends DefaultAsyncProducer
{
ActionTask task = new ActionTask(exchange);
if (getEndpoint().isAsync()) {
getEndpoint().getComponent().getExecutorService().submit(task);
+ } else if (task.stopsRouteOfExchange()) {
+ // the exchange is inflight in that route, and a graceful stop
waits for it to complete: on this thread the
+ // stop would wait for itself until the shutdown timeout
+ LOG.debug("Running the {} action of route {} asynchronously, as
the exchange is routed by that route",
+ getEndpoint().getAction(), task.routeId);
+ getEndpoint().getComponent().getExecutorService().submit(task);
} else {
task.run();
}
@@ -144,19 +151,51 @@ public class ControlBusProducer extends
DefaultAsyncProducer {
private final class ActionTask implements Runnable {
private final Exchange exchange;
+ private final String routeId;
+ // read on the calling thread: when the task runs asynchronously, the
exchange may be done (and reset when
+ // pooled) by the time it runs
+ private final Throwable cause;
private ActionTask(Exchange exchange) {
this.exchange = exchange;
+ String id = getEndpoint().getRouteId();
+ if (ObjectHelper.equal("current", id)) {
+ id = ExchangeHelper.getRouteId(exchange);
+ }
+ this.routeId = id;
+ Throwable c = null;
+ if ("fail".equals(getEndpoint().getAction())) {
+ c = exchange.getException();
+ if (c == null) {
+ c = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
Throwable.class);
+ }
+ }
+ this.cause = c;
+ }
+
+ /**
+ * Whether the action stops or suspends a route that routes the
exchange (the current route, or a route that
+ * called it, such as with direct), or the route the exchange came
from.
+ */
+ boolean stopsRouteOfExchange() {
+ String action = getEndpoint().getAction();
+ boolean stops = "stop".equals(action) || "suspend".equals(action)
|| "restart".equals(action)
+ || "fail".equals(action);
+ if (!stops || routeId == null) {
+ return false;
+ }
+ if (routeId.equals(ExchangeHelper.getRouteId(exchange)) ||
routeId.equals(exchange.getFromRouteId())) {
+ return true;
+ }
+ // the routes on the route stack of the exchange wait for it as
well
+ UnitOfWork uow = exchange.getUnitOfWork();
+ return uow != null && uow.routeStackLevel(r ->
routeId.equals(r.getRouteId())) > 0;
}
@Override
public void run() {
String action = getEndpoint().getAction();
- String id = getEndpoint().getRouteId();
-
- if (ObjectHelper.equal("current", id)) {
- id = ExchangeHelper.getRouteId(exchange);
- }
+ String id = routeId;
Object result = null;
String task = action + " route " + id;
@@ -266,15 +305,12 @@ public class ControlBusProducer extends
DefaultAsyncProducer {
private void failAction(String id) throws Exception {
LOG.debug("Stopping and failing route: {}", id);
- // is there any caused exception from the exchange to mark the
route as failed due
- Throwable cause = exchange.getException();
- if (cause == null) {
- cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
Throwable.class);
- }
- if (cause == null) {
- cause = new RejectedExecutionException("Route " + id + " is
forced stopped and marked as failed");
+ // mark the route as failed due to the exception of the exchange,
if any
+ Throwable failure = cause;
+ if (failure == null) {
+ failure = new RejectedExecutionException("Route " + id + " is
forced stopped and marked as failed");
}
- getEndpoint().getCamelContext().getRouteController().stopRoute(id,
cause);
+ getEndpoint().getCamelContext().getRouteController().stopRoute(id,
failure);
}
private void stopAction(String id) throws Exception {
diff --git
a/core/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusFailCurrentRouteTest.java
b/core/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusFailCurrentRouteTest.java
new file mode 100644
index 000000000000..b7d1c0b92bef
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusFailCurrentRouteTest.java
@@ -0,0 +1,100 @@
+/*
+ * 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.component.controlbus;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.Route;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.engine.PooledExchangeFactory;
+import org.apache.camel.spi.PooledObjectFactory;
+import org.apache.camel.spi.RouteError;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A fail of the route of the exchange runs asynchronously, so it must mark
the route as failed with the exception the
+ * exchange had when the control bus step ran, even when the task runs after
the exchange is done and back in the pool.
+ */
+public class ControlBusFailCurrentRouteTest extends ContextTestSupport {
+
+ // holds the control bus thread until the exchange is back in the pool
+ private final CountDownLatch exchangeReleased = new CountDownLatch(1);
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext camelContext = super.createCamelContext();
+ ExtendedCamelContext ecc = camelContext.getCamelContextExtension();
+ ecc.setExchangeFactory(new PooledExchangeFactory());
+ ecc.getExchangeFactory().setStatisticsEnabled(true);
+
camelContext.getExecutorServiceManager().addThreadFactoryListener((source,
factory) -> {
+ if (source instanceof ControlBusComponent) {
+ return task -> factory.newThread(() -> {
+ awaitExchangeReleased();
+ task.run();
+ });
+ }
+ return factory;
+ });
+ return camelContext;
+ }
+
+ private void awaitExchangeReleased() {
+ try {
+ exchangeReleased.await(10, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ @Test
+ public void testFailCurrentRouteFromOnException() {
+ PooledObjectFactory.Statistics stat
+ =
context.getCamelContextExtension().getExchangeFactoryManager().getStatistics();
+ await().atMost(5, TimeUnit.SECONDS).until(() ->
stat.getReleasedCounter() >= 1);
+ exchangeReleased.countDown();
+
+ Route route = context.getRoute("fail");
+ await().atMost(5, TimeUnit.SECONDS).until(() -> route.getLastError()
!= null);
+ RouteError error = route.getLastError();
+ assertTrue(error.isUnhealthy());
+ assertEquals(RouteError.Phase.STOP, error.getPhase());
+ assertEquals("Forced", error.getException().getMessage());
+
assertTrue(context.getRouteController().getRouteStatus("fail").isStopped());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("timer:fail?delay=10&repeatCount=1").routeId("fail")
+
.onException(IllegalArgumentException.class).handled(true)
+ .to("controlbus:route?routeId=current&action=fail")
+ .end()
+ .throwException(new
IllegalArgumentException("Forced"));
+ }
+ };
+ }
+}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStopCurrentRouteTest.java
b/core/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStopCurrentRouteTest.java
new file mode 100644
index 000000000000..a81a576572f8
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStopCurrentRouteTest.java
@@ -0,0 +1,177 @@
+/*
+ * 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.component.controlbus;
+
+import java.util.concurrent.TimeUnit;
+
+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.spi.RouteController;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A stop of the route that routes the exchange must not wait for that
exchange until the shutdown timeout.
+ */
+public class ControlBusStopCurrentRouteTest extends ContextTestSupport {
+
+ @BeforeEach
+ void shortShutdownTimeout() {
+ context.getShutdownStrategy().setTimeout(5);
+ context.getShutdownStrategy().setTimeUnit(TimeUnit.SECONDS);
+ }
+
+ @Test
+ public void testStopCurrentRoute() throws Exception {
+ getMockEndpoint("mock:current").expectedBodiesReceived("Hello World");
+
+ Exchange out = template.send("direct:current", e ->
e.getMessage().setBody("Hello World"));
+
+ assertNull(out.getException());
+ MockEndpoint.assertIsSatisfied(context);
+ RouteController controller = context.getRouteController();
+ await().atMost(5, TimeUnit.SECONDS).until(() ->
controller.getRouteStatus("current").isStopped());
+ }
+
+ @Test
+ public void testSuspendCurrentRoute() throws Exception {
+ getMockEndpoint("mock:suspend").expectedBodiesReceived("Hello World");
+
+ Exchange out = template.send("direct:suspend", e ->
e.getMessage().setBody("Hello World"));
+
+ assertNull(out.getException());
+ MockEndpoint.assertIsSatisfied(context);
+ RouteController controller = context.getRouteController();
+ await().atMost(5, TimeUnit.SECONDS).until(() ->
controller.getRouteStatus("suspend").isSuspended());
+ }
+
+ @Test
+ public void testStopRouteOfExchangeFromSubRoute() throws Exception {
+ getMockEndpoint("mock:sub").expectedBodiesReceived("Hello World");
+ getMockEndpoint("mock:main").expectedBodiesReceived("Hello World");
+
+ Exchange out = template.send("direct:main", e ->
e.getMessage().setBody("Hello World"));
+
+ assertNull(out.getException());
+ MockEndpoint.assertIsSatisfied(context);
+ RouteController controller = context.getRouteController();
+ await().atMost(5, TimeUnit.SECONDS).until(() ->
controller.getRouteStatus("main").isStopped());
+ assertTrue(controller.getRouteStatus("sub").isStarted());
+ }
+
+ @Test
+ public void testStopCallingRouteFromSubRoute() throws Exception {
+ // route mid called route stopMid with direct, so the exchange is
still inflight in mid
+ getMockEndpoint("mock:stopMid").expectedBodiesReceived("Hello World");
+ getMockEndpoint("mock:mid").expectedBodiesReceived("Hello World");
+
+ Exchange out = template.send("direct:chain", e ->
e.getMessage().setBody("Hello World"));
+
+ assertNull(out.getException());
+ MockEndpoint.assertIsSatisfied(context);
+ RouteController controller = context.getRouteController();
+ await().atMost(5, TimeUnit.SECONDS).until(() ->
controller.getRouteStatus("mid").isStopped());
+ assertTrue(controller.getRouteStatus("chain").isStarted());
+ }
+
+ @Test
+ public void testStopRouteOfSplitFromSplitCopy() throws Exception {
+ // the split copy has its own exchange id, and route split waits for
the exchange that is split
+ getMockEndpoint("mock:stopSplit").expectedBodiesReceived("Hello
World");
+ getMockEndpoint("mock:split").expectedBodiesReceived("Hello World");
+
+ Exchange out = template.send("direct:splitter", e ->
e.getMessage().setBody("Hello World"));
+
+ assertNull(out.getException());
+ MockEndpoint.assertIsSatisfied(context);
+ RouteController controller = context.getRouteController();
+ await().atMost(5, TimeUnit.SECONDS).until(() ->
controller.getRouteStatus("split").isStopped());
+ assertTrue(controller.getRouteStatus("splitter").isStarted());
+ }
+
+ @Test
+ public void testStopOtherRouteIsDoneWhenTheStepReturns() throws Exception {
+ // the route to stop does not route the exchange, so it is stopped on
the calling thread
+ getMockEndpoint("mock:other").expectedBodiesReceived("Stopped");
+
+ template.sendBody("direct:other", "Hello World");
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:current").routeId("current")
+ .to("controlbus:route?routeId=current&action=stop")
+ .to("mock:current");
+
+ from("direct:suspend").routeId("suspend")
+ .to("controlbus:route?routeId=current&action=suspend")
+ .to("mock:suspend");
+
+ from("direct:main").routeId("main")
+ .to("direct:sub")
+ .to("mock:main");
+
+ from("direct:sub").routeId("sub")
+ .to("controlbus:route?routeId=main&action=stop")
+ .to("mock:sub");
+
+ from("direct:chain").routeId("chain")
+ .to("direct:mid");
+
+ from("direct:mid").routeId("mid")
+ .to("direct:stopMid")
+ .to("mock:mid");
+
+ from("direct:stopMid").routeId("stopMid")
+ .to("controlbus:route?routeId=mid&action=stop")
+ .to("mock:stopMid");
+
+ from("direct:splitter").routeId("splitter")
+ .to("direct:split");
+
+ from("direct:split").routeId("split")
+ .split(body().tokenize(","))
+ .to("direct:stopSplit")
+ .end()
+ .to("mock:split");
+
+ from("direct:stopSplit").routeId("stopSplit")
+ .to("controlbus:route?routeId=split&action=stop")
+ .to("mock:stopSplit");
+
+ from("direct:other").routeId("other")
+ .to("controlbus:route?routeId=foo&action=stop")
+ .to("controlbus:route?routeId=foo&action=status")
+ .to("mock:other");
+
+ from("direct:foo").routeId("foo")
+ .to("mock:foo");
+ }
+ };
+ }
+}
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 cbfee955c23d..7b30527645df 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
@@ -32,6 +32,15 @@ camel.component.controlbus.allowed-languages = simple
The `route` command (`controlbus:route?action=...`) is not affected.
+=== camel-controlbus - stopping the route of the message runs asynchronously
+
+A `stop`, `suspend`, `restart` or `fail` action of the `route` command on a
route that routes the message
+(`routeId=current`, or a route that called it, such as with `direct`), or on
the route the message came from, now runs
+asynchronously, as with `async=true`.
+Before, it waited for that same message until the shutdown timeout (45 seconds
by default), then forced the stop and
+the message failed with `RouteStoppingException`. Now the message continues,
and the route stops once it is done, so
+the route is not yet stopped when the control bus step returns.
+
=== camel-clickup - the webhook signature check no longer logs signatures
The webhook consumer still logs the body it validates at `DEBUG`, but no
longer the signature from the `X-Signature` header, nor the signature it
calculates.
diff --git a/docs/user-manual/modules/ROOT/pages/graceful-shutdown.adoc
b/docs/user-manual/modules/ROOT/pages/graceful-shutdown.adoc
index e1c86c4d4960..eb44c839986c 100644
--- a/docs/user-manual/modules/ROOT/pages/graceful-shutdown.adoc
+++ b/docs/user-manual/modules/ROOT/pages/graceful-shutdown.adoc
@@ -191,6 +191,11 @@ It is possible to stop (will do a gracefully shut down) an
individual route usin
camelContext.getRouteController().stopRoute(routeId);
----
+NOTE: A graceful stop waits for the inflight messages of the route. When a
message stops its own route by calling
+`stopRoute` on the thread that routes it, the stop waits for that message
until the shutdown timeout. Stop the route
+from another thread instead, or use the
xref:components::controlbus-component.adoc[Control Bus]
+(`controlbus:route?routeId=current&action=stop`), which does so for the route
of the message.
+
TIP: Routes can also be stopped via JMX or external tools such as
https://hawt.io/[Hawtio].
=== Stopping and marking routes as failed due to an exception
diff --git
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/ControlBusEndpointBuilderFactory.java
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/ControlBusEndpointBuilderFactory.java
index 480ec18aa0bb..79d15f85d4be 100644
---
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/ControlBusEndpointBuilderFactory.java
+++
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/ControlBusEndpointBuilderFactory.java
@@ -70,6 +70,11 @@ public interface ControlBusEndpointBuilderFactory {
* Whether to execute the control bus task asynchronously. Important:
If
* this option is enabled, then any result from the task is not set on
* the Exchange. This is only possible if executing tasks
synchronously.
+ * A stop, suspend, restart or fail of a route that routes the exchange
+ * (the current route, or a route that called it, such as with direct),
+ * or of the route the exchange came from, is always executed
+ * asynchronously, as the route waits for the exchange to complete
+ * before it stops.
*
* The option is a: <code>boolean</code> type.
*
@@ -87,6 +92,11 @@ public interface ControlBusEndpointBuilderFactory {
* Whether to execute the control bus task asynchronously. Important:
If
* this option is enabled, then any result from the task is not set on
* the Exchange. This is only possible if executing tasks
synchronously.
+ * A stop, suspend, restart or fail of a route that routes the exchange
+ * (the current route, or a route that called it, such as with direct),
+ * or of the route the exchange came from, is always executed
+ * asynchronously, as the route waits for the exchange to complete
+ * before it stops.
*
* The option will be converted to a <code>boolean</code> type.
*