Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

For this reason, Apache Camel provides a set of features to support graceful shutdown of applications. Graceful shutdown gives you full control over the stopping and starting of routes, enabling you to control the shutdown order of routes and enabling current tasks to run to completion.

Setting Route ID

It is a good best practice to assign a unique route ID to each of the routes that we build. This will make logging messages and management features more informative and easier to track. Additionally, the use of route IDs will provide greater control over the stopping and starting of routes.

...

Code Block
<camelContext id="CamelContextID" xmlns="http://camel.apache.org/schema/spring">
  <route id="myCustomRouteId" >
    <from uri="SourceURI"/>
    <process ref="someProcessorId"/>
    <to uri="TargetURI"/>
  </route>
</camelContext>

Manually Start and Stop Routes

There may be a need for us to manually start or stop a route at any time. We can perform this in Java by invoking the startRoute() and stopRoute() methods on the CamelContext instance. For example, to start the route having the route ID, notAuto, invoke the startRoute() method on the CamelContext instance, context:

...

Code Block
context.stopRoute("nonAuto");

Startup Order

By default, Apache Camel starts up routes in a non-deterministic order. However, in some application situations, it could be vital to control the order of the route startup. Each route must be assigned a unique startup order value. You can choose any positive integer value that is less than 1000

...

NOTE: Each route must be assigned a unique startup order value. You can choose any positive integer value that is less than 1000

Shutdown Order

When we our instance is shutting down, Apache Camel controls the shutdown sequence using a pluggable shutdown strategy. This strategy implements the following shutdown sequence:

  1. All Routes are shut down in the reverse of the start-up order.

  2. In normal situations, the shutdown strategy waits until the currently active exchanges have finished processing.

  3. Shutdown sequence is bound by a timeout (default of 300 seconds). If the shutdown sequence exceeds this timeout, the shutdown strategy will force shutdown to occur, even if some tasks are still running.

Shutdown Sequence of Routes

By default our routes are shut down in the reverse sequence of the start-up order. That is, when a start-up order is defined using the startupOrder() command (in Java DSL) or startupOrder attribute (in XML DSL), the first route to shut down is the route with the highest integer value assigned by the start-up order.

Shut Down Running Tasks

There may be situations where a route is still processing messages and 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.

...

Code Block
<!-- let this route complete all its pending messages when asked to shut down -->
    <route id="one"
           startupOrder="2"
           shutdownRunningTask="CompleteAllTasks">
        <from uri="file:target/pending"/>
        <delay><constant>1000</constant></delay>
        <to uri="seda:foo"/>
    </route>

Shutdown Timeout

As mentioned earlier, the shutdown timeout has a default value of 300 seconds. We can change the value of the timeout by invoking the setTimeout() method on the shutdown strategy. As an illustration, we can change the timeout value to 600 seconds:

...