You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

How can I stop a route from a route

The CamelContext provides API for managing routes at runtime. It has a stopRoute(id) and startRoute(id) methods.

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");
    });

Important: It is not best practice to stop a route as shown above, as stopping a route using the same thread that routes messages can be tricky. Its better to spin off a separate thread that stops the route, or to use a boolean flag, and then flip the flag, from the route. And then have another process that monitors that flag, and reacts, to stop the route.

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.

See Also

  • No labels