...
Stopping a route during routing an existing message is a bit tricky. The reason for that is Camel will Graceful Shutdown the route you are stopping. And if you do that while a message is being routed the Graceful Shutdown will try to wait until that message has been processed. Now that message can easily be yourself. So to cater for that you have to tell Camel that you are done routing this message which you do by removing it from the in flight registry. The follow code shows how you can stop a route from a Processor:
...
from("direct:start")
.to("bean:foo?method=doSomething").routeId("myCoolRoute")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
// remove myself from the in flight registry so we can stop this route without trouble
context.getInflightRepository().remove(exchange);
// stop this route
context.stopRoute("myCoolRoute");
});
The best practice for stopping a route from a route, is to either
- signal to another thread to stop the route
- spin off a new thread to stop the route
Using another thread to stop the route is also what is normally used when stopping Camel itself, or for example when an application in a server is stopped etc. Its too tricky and hard to stop a route using the same thread that currently is processing a message from the route. This is not advised to do, and can cause unforeseen side effects.
Using a latch to stop Camel from a route
In this example we use a CountdownLatch
to signal when Camel should stop, triggered from a route.
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopCamelFromRouteTest.java} |
And in the route we call the latch as shown:
Wiki Markup |
---|
{snippet:id=e2|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopCamelFromRouteTest.java} |
Using a thread to stop a route from a route
In this example we use a separate Thread
to stop the route, triggered from the route itself.
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopRouteFromRouteTest.java} |
And in the route we create the thread and call the stopRoute
method as shown:
Wiki Markup |
---|
{snippet:id=e2|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopRouteFromRouteTest.java} |
Alternative solutions
...
Camel provides another feature for managing routes at runtime which is RoutePolicy.
And CamelContext also provides API for suspend/resume of routes, and shutdown as well.
- suspend/resume is faster than stop/start. For example a HTTP server will still run but deny any incoming requests.
Where as if it was stopped the HTTP listener would have been stopped. - shutdown means the route is being removed from CamelContext and cannot be started again. Its also removed from JMX.
A route must have been stopped prior to be shutdown.
See more details about the Lifecycle.
Tip |
---|
You can also use the ControlBus component to let it stop/start routes. |