Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

The default strategy will gracefully shutdown routes:

  • Camel 2.2: in the same order they was started
  • Camel 2.3: in the reverse order they was started. The option shutdownRoutesInReverseOrder can be used to use the old behavior.
  • 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 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.

...

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 org.apache.camel.impl.DefaultShutdownStrategy then it logs the same inflight exchange information during graceful shutdown

...

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 control two areas that influence graceful shutdown in the Camel routing:

  • ShutdownRoute
  • ShutdownRunningTask

These options can be configured on two scopes: context and route. Where a route will fallback to the context scoped option, if not explicit configured. (same principle as Error Handler, etc.).

...

A Java DSL based example to defer shutting down the 2nd route:

...

{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ShutdownDeferTest.java}

...

The same route in Spring XML would be:

...

{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/ShutdownDeferTest.xml}

...

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.

ShutdownRunningTask

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 CompleteCurrentTaskOnly which mean that the current in progress task (message) will be completed and then the consumer will shutdown. The other option CompleteAllTasks allows the consumer to complete all the tasks (messages) before shutting down. For example a File consumer will process all the pending files it has picked up before shutting down.

A Java DSL based example to complete all messages during shutting down the first route:

...

{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ShutdownCompleteAllTasksTest.java}

...

The same route in Spring XML would be:

...

{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/ShutdownCompleteAllTasksTest.xml}

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.

...

Batch Consumer should implement ShutdownAware so they properly support the ShutdownRunningTask option. See GenericFileConsumer for an example.

See Also