Available as of Camel 2.2
Camel now supports a pluggable shutdown strategy using
Camel provides a default strategy in the
The default strategy will gracefully shutdown routes:
You can configure the timeout, and whether it should shutdown now remaining routes when the timeout occurred or ignore. See the setters on the class.
It will output to log the progress during graceful shutdown as shown in an example below
Notice how it waits while there are inflight exchanges still being processed before it can shutdown.
Suppressing logging due to timeout not allowing all inflight messages to complete
Available as of Camel 2.12
If a graceful shutdown could not shutdown cleanly within the given timeout period, then Camel performs a more aggressive shutdown by forcing routes and thread pools etc to shutdown. And as well the routing engine will reject continue processing Exchanges. If this happens you may see WARN logs about Exchanges being rejected and other failures due the forced shutdown.
If you do not want to see these logs, you can suppress this by setting the option SuppressLoggingOnTimeout to true.
Notice the suppress is a "best effort" though there may still be some logs coming from 3rd party libraries and whatnot, which Camel cannot control.
Logging inflight exchange information on timeout
Available as of Camel 2.15
If a graceful shutdown could not shutdown cleanly within the given timeout period, then Camel performs a more aggressive shutdown by forcing routes and thread pools etc to shutdown. When the timeout happens, then Camel logs information about the current inflight exchanges, which shows from which route the exchange origins, and where it currently is being routed. For example the logging below, shows that there is 1 inflight exchange, that origins from route1, and currently is still in route1 at the "delay1" node. The elapsed is time in millis how long at the current node (eg delay1) and duration is total time in mills.
If you enable DEBUG logging level on
If you do not want to see these logs, you can turn this off by setting the option logInflightExchangesOnTimeout to false.
Controlling ordering of routes
You can configure the order in which routes should be started, and thus also the same order they are being shutdown.
Fine grained configuration
You can control two areas that influence graceful shutdown in the Camel routing:
These options can be configured on two scopes:
This option can control how a given route should act during graceful shutdown. It has two values
For example in the route below we have two routes, where route 1 is dependent upon route 2. At shutdown we want route 1 to complete all its current messages and we also want the 2nd route to do this as well. So we can mark both routes to
A Java DSL based example to defer shutting down the 2nd route:
The same route in Spring XML would be:
Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
Defer shutting down internal routes only
Its best to only defer shutting down internal routes only. As public routes should shutdown as quickly as possible otherwise it will just keep intake new messages which will delay the shutdown processor. Or even have it timeout if a lot of new messages keep coming in.
This option control how a given route consumer acts during shutdown. Most route consumer will only operate on a single task (message), however the Batch Consumer can operate on many messages (in a batch). This option is for those kind of consumers. By default it uses the option
A Java DSL based example to complete all messages during shutting down the first route:
The same route in Spring XML would be:
Shutting down individual routes
Available as of Camel 2.3
If you develop your own Camel component or want to implement your own shutdown strategy then read this section for details.
You can implement your own strategy to control the shutdown by implementing the
When using Spring XML you then just define a spring bean which implements the
Batch Consumer should implement