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

Compare with Current View Page History

« Previous Version 5 Next »

Graceful Shutdown

Available as of Camel 2.2

Camel now supports a pluggable shutdown strategy using org.apache.camel.spi.ShutdownStrategy. Its responsible for shutting down routes in a graceful manner. The other resources will still be handled by CamelContext to shutdown. This leaves the problem at hand with properly shutting down all the routes in a reliable manner to the ShutdownStrategy.

Camel provides a default strategy in the org.apache.camel.impl.DefaultShutdownStrategy which is capable of doing that.

DefaultShutdownStrategy

The default strategy will graceful shutdowns routes:

  • in the same order they where started
  • let pending and current in flight exchanges run to completion before shutting down
  • using a timeout of 300 seconds which then forces a shutdown now

You can configure the timeout and whether it should shutdown now remainder 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

2009-12-20 10:56:53,055 [main           ] INFO  DefaultCamelContext            - Apache Camel  (CamelContext:camel-1) is stopping
2009-12-20 10:56:53,056 [main           ] INFO  DefaultShutdownStrategy        - Starting to graceful shutdown routes (timeout 300 seconds)
2009-12-20 10:56:53,059 [1: ShutdownTask] INFO  DefaultShutdownStrategy        - Waiting as there are still 5 inflight exchanges to complete before we can shutdown
2009-12-20 10:56:54,060 [1: ShutdownTask] INFO  DefaultShutdownStrategy        - Waiting as there are still 4 inflight exchanges to complete before we can shutdown
2009-12-20 10:56:55,061 [1: ShutdownTask] INFO  DefaultShutdownStrategy        - Waiting as there are still 3 inflight exchanges to complete before we can shutdown
2009-12-20 10:56:56,065 [1: ShutdownTask] INFO  DefaultShutdownStrategy        - Waiting as there are still 2 inflight exchanges to complete before we can shutdown
2009-12-20 10:56:57,066 [1: ShutdownTask] INFO  DefaultShutdownStrategy        - Waiting as there are still 1 inflight exchanges to complete before we can shutdown
2009-12-20 10:56:58,069 [main           ] INFO  DefaultShutdownStrategy        - Graceful shutdown of routes complete in 5 seconds.
2009-12-20 10:56:58,072 [main           ] INFO  DefaultInflightRepository      - Shutting down with no inflight exchanges.
2009-12-20 10:56:58,077 [main           ] INFO  DefaultCamelContext            - Apache Camel  (CamelContext:camel-1) stopped

Notice how it waits while there are inflight exchanges still be processed before it can shutdown.

ShutdownStrategy

You can implement your own strategy to control the shutdown by implementing the org.apache.camel.spi.ShutdownStrategy and the set it on the CamelContext using the setShutdownStrategy method.

When using Spring XML you then just define a spring bean which implements the org.apache.camel.spi.ShutdownStrategy and Camel will lookup it up at startup and use it instead of its default. See more at Advanced configuration of CamelContext using Spring.

ShutdownAware

The interface org.apache.camel.spi.ShutdownAware is an optional interface consumers can implement to have fine grained control during shutdown. The ShutdownStrategy must be able to deal with consumers which implements this interface. This interface was introduced to cater for in memory consumers such as SEDA which potentially have a number of pending messages on its internal in memory queues. What this allows is to let it control the shutdown process to let it complete its pending messages.

The method getPendingExchangesSize should return the number of pending messages which reside on the in memory queues.
The method deferShutdown should return false to defer the shutdown to very last when there are no more pending and inflight messages.

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.
See more at Configuring route startup ordering and autostartup.

JMX managed

The ShutdownStrategy is JMX aware as well so you can manage it from a JMX console. For example you can change the timeout value.

See Also

  • No labels