When a CamelContext instance is shutting down, Apache Camel controls the
shutdown sequence using a pluggable shutdown strategy. The default
shutdown strategy implements the following shutdown sequence:
Routes are shut down in the reverse of the start-up order.
The shutdown strategy waits until the currently active exchanges have finished processing.
The shutdown sequence is bound by a timeout that defaults to 300 seconds. If the shutdown sequence exceeds this timeout, the shutdown strategy will force shutdown to occur, even if some tasks are still running.
Routes are shut down in the reverse of the start-up order. When a start-up
order is defined using the startupOrder attribute, the
first route to shut down is the route with the highest integer
value assigned by the start-up order and the last route to shut down is the route with the
lowest integer value assigned by the start-up order.
For example, in Example 10.2, the first route segment to be
shut down is the route with the ID, first, and the second
route segment to be shut down is the route with the ID, second. This example
illustrates a general rule, which you should observe when shutting down routes:
the routes that expose externally-accessible consumer endpoints should be shut
down first, because this helps to throttle the flow of messages through the
rest of the route graph.
You can manually stop a route having using the stopRoute()
method on the CamelContext instance. The following code shows how to stop a
route with the ID of nonAuto:
context.stopRoute("nonAuto");If a route is still processing messages when the shutdown starts, the shutdown strategy
normally waits until the currently active exchange has finished processing before shutting
down the route. This behavior can be configured on each route using the route's
shutdownRunningTask attribute, which can take either of the
following values:
CompleteCurrentTaskOnly(Default) Usually, a route operates on just a single message at a time, so you can safely shut down the route after the current task has completed.
CompleteAllTasksSpecify this option in order to shut down batch consumers gracefully. Some consumer endpoints (for example, File, FTP, Mail, iBATIS, and JPA) operate on a batch of messages at a time. For these endpoints, it is more appropriate to wait until all of the messages in the current batch have completed.
The code in Example 10.3 shuts down a File consumer endpoint
gracefully by specifying the CompleteAllTasks option.
Example 10.3. Shutting down a file consumer
<camelContext id="camel"> <!-- let this route complete all its pending messages when asked to shut down --> <route shutdownRunningTask="CompleteAllTasks"> <from uri="file:target/pending"/> <delay><constant>1000</constant></delay> <to uri="seda:foo"/> </route> </camelContext>








