If you want to stop the route you are currently using, you can’t do it using
context.stopRoute()
because it will wait for the current “inflight” message to be processed until “the end”.
So you need to do this async:
// .... private void shutdownRoute() { ShutdownRoute shutdownRoute = new ShutdownRoute(camelContext); new Thread(shutdownRoute).start(); try { Thread.sleep(1000); // makes sure route is stop before this message finished } catch (InterruptedException e) { // whatever } } // ... class ShutdownRoute implements Runnable { /** log instance */ private Logger log = LoggerFactory.getLogger(ShutdownRoute.class.getName()); private final CamelContext context; ShutdownRoute(final CamelContext cContext) { context = cContext; } @Override public void run() { try { context.stopRoute(YOUR_ROUTE_ID); } catch (Exception e) { log.error("Failed to stop route",e); } } }
We do this when using the circuit breaker pattern. In the error handling processor we check if the circuit breaker goes to the OPEN state. If so, the route stops itself. Via another route we check periodically if the circuit breaker is in the HALF-OPEN state and start the route again.