org.apache.camel.impl
Class DefaultShutdownStrategy

java.lang.Object
  extended by org.apache.camel.support.ServiceSupport
      extended by org.apache.camel.impl.DefaultShutdownStrategy
All Implemented Interfaces:
CamelContextAware, Service, ShutdownableService, ShutdownStrategy, StatefulService, SuspendableService

public class DefaultShutdownStrategy
extends ServiceSupport
implements ShutdownStrategy, CamelContextAware

Default ShutdownStrategy which uses graceful shutdown.

Graceful shutdown ensures that any inflight and pending messages will be taken into account and it will wait until these exchanges has been completed.

This strategy will perform graceful shutdown in two steps:

The idea by the graceful shutdown strategy, is to stop taking in more new messages, and allow any existing inflight messages to complete. Then when there is no more inflight messages then the routes can be fully shutdown. This mean that if there is inflight messages then we will have to wait for these messages to complete. If they do not complete after a period of time, then a timeout triggers. And then a more aggressive strategy takes over.

The idea by the forced shutdown strategy, is to stop continue processing messages. And force routes and its services to shutdown now. There is a risk when shutting down now, that some resources is not properly shutdown, which can cause side effects. The timeout value is by default 300 seconds, but can be customized.

As this strategy will politely wait until all exchanges has been completed it can potential wait for a long time, and hence why a timeout value can be set. When the timeout triggers you can also specify whether the remainder consumers should be shutdown now or ignore.

Will by default use a timeout of 300 seconds (5 minutes) by which it will shutdown now the remaining consumers. This ensures that when shutting down Camel it at some point eventually will shutdown. This behavior can of course be configured using the setTimeout(long) and setShutdownNowOnTimeout(boolean) methods.

Routes will by default be shutdown in the reverse order of which they where started. You can customize this using the setShutdownRoutesInReverseOrder(boolean) method.

After route consumers have been shutdown, then any ShutdownPrepared services on the routes is being prepared for shutdown, by invoking ShutdownPrepared.prepareShutdown(boolean) which force=false.

Then if a timeout occurred and the strategy has been configured with shutdown-now on timeout, then the strategy performs a more aggressive forced shutdown, by forcing all consumers to shutdown and then invokes ShutdownPrepared.prepareShutdown(boolean) with force=true on the services. This allows the services to know they should force shutdown now.

Version:

Field Summary
 
Fields inherited from class org.apache.camel.support.ServiceSupport
shutdown, shuttingdown, started, starting, stopped, stopping, suspended, suspending
 
Constructor Summary
DefaultShutdownStrategy()
           
DefaultShutdownStrategy(CamelContext camelContext)
           
 
Method Summary
protected  void doShutdown()
          Implementations override this method to perform customized shutdown.
protected  boolean doShutdown(CamelContext context, List<RouteStartupOrder> routes, long timeout, TimeUnit timeUnit, boolean suspendOnly, boolean abortAfterTimeout, boolean forceShutdown)
           
protected  void doStart()
          Implementations override this method to support customized start/stop.
protected  void doStop()
          Implementations override this method to support customized start/stop.
 boolean forceShutdown(Service service)
          Whether a service is forced to shutdown.
 CamelContext getCamelContext()
          Get the CamelContext
 long getTimeout()
          Gets the timeout.
 TimeUnit getTimeUnit()
          Gets the time unit used
 boolean isShutdownNowOnTimeout()
          Whether to force shutdown of all consumers when a timeout occurred.
 boolean isShutdownRoutesInReverseOrder()
          Whether to shutdown routes in reverse order than they where started.
 void setCamelContext(CamelContext camelContext)
          Injects the CamelContext
 void setShutdownNowOnTimeout(boolean shutdownNowOnTimeout)
          Sets whether to force shutdown of all consumers when a timeout occurred and thus not all consumers was shutdown within that period.
 void setShutdownRoutesInReverseOrder(boolean shutdownRoutesInReverseOrder)
          Sets whether routes should be shutdown in reverse or the same order as they where started.
 void setTimeout(long timeout)
          Set an timeout to wait for the shutdown to complete.
 void setTimeUnit(TimeUnit timeUnit)
          Set the time unit to use
 void shutdown(CamelContext context, List<RouteStartupOrder> routes)
          Shutdown the routes
 void shutdown(CamelContext context, List<RouteStartupOrder> routes, long timeout, TimeUnit timeUnit)
          Shutdown the routes using a specified timeout instead of the default timeout values
 boolean shutdown(CamelContext context, RouteStartupOrder route, long timeout, TimeUnit timeUnit, boolean abortAfterTimeout)
          Shutdown the route using a specified timeout instead of the default timeout values and supports abortAfterTimeout mode
 void shutdownForced(CamelContext context, List<RouteStartupOrder> routes)
          Shutdown the routes, forcing shutdown being more aggressive, if timeout occurred.
protected static void shutdownNow(Consumer consumer)
          Shutdown the consumer immediately.
protected  void shutdownNow(List<Consumer> consumers)
          Shutdown all the consumers immediately.
protected  void shutdownRoutesNow(List<RouteStartupOrder> routes)
          Shutdown all the consumers immediately.
 void suspend(CamelContext context, List<RouteStartupOrder> routes)
          Suspends the routes
 void suspend(CamelContext context, List<RouteStartupOrder> routes, long timeout, TimeUnit timeUnit)
          Suspends the routes using a specified timeout instead of the default timeout values
protected static void suspendNow(Consumer consumer)
          Suspends/stops the consumer immediately.
 
Methods inherited from class org.apache.camel.support.ServiceSupport
doResume, doSuspend, getStatus, getVersion, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isSuspended, isSuspending, resume, shutdown, start, stop, suspend
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.camel.Service
start, stop
 

Constructor Detail

DefaultShutdownStrategy

public DefaultShutdownStrategy()

DefaultShutdownStrategy

public DefaultShutdownStrategy(CamelContext camelContext)
Method Detail

shutdown

public void shutdown(CamelContext context,
                     List<RouteStartupOrder> routes)
              throws Exception
Description copied from interface: ShutdownStrategy
Shutdown the routes

Specified by:
shutdown in interface ShutdownStrategy
Parameters:
context - the camel context
routes - the routes, ordered by the order they was started
Throws:
Exception - is thrown if error shutting down the consumers, however its preferred to avoid this

shutdownForced

public void shutdownForced(CamelContext context,
                           List<RouteStartupOrder> routes)
                    throws Exception
Description copied from interface: ShutdownStrategy
Shutdown the routes, forcing shutdown being more aggressive, if timeout occurred.

This operation is used when CamelContext is shutting down, to ensure Camel will shutdown if messages seems to be stuck.

Specified by:
shutdownForced in interface ShutdownStrategy
Parameters:
context - the camel context
routes - the routes, ordered by the order they was started
Throws:
Exception - is thrown if error shutting down the consumers, however its preferred to avoid this

suspend

public void suspend(CamelContext context,
                    List<RouteStartupOrder> routes)
             throws Exception
Description copied from interface: ShutdownStrategy
Suspends the routes

Specified by:
suspend in interface ShutdownStrategy
Parameters:
context - the camel context
routes - the routes, ordered by the order they was started
Throws:
Exception - is thrown if error suspending the consumers, however its preferred to avoid this

shutdown

public void shutdown(CamelContext context,
                     List<RouteStartupOrder> routes,
                     long timeout,
                     TimeUnit timeUnit)
              throws Exception
Description copied from interface: ShutdownStrategy
Shutdown the routes using a specified timeout instead of the default timeout values

Specified by:
shutdown in interface ShutdownStrategy
Parameters:
context - the camel context
routes - the routes, ordered by the order they was started
timeout - timeout
timeUnit - the unit to use
Throws:
Exception - is thrown if error shutting down the consumers, however its preferred to avoid this

shutdown

public boolean shutdown(CamelContext context,
                        RouteStartupOrder route,
                        long timeout,
                        TimeUnit timeUnit,
                        boolean abortAfterTimeout)
                 throws Exception
Description copied from interface: ShutdownStrategy
Shutdown the route using a specified timeout instead of the default timeout values and supports abortAfterTimeout mode

Specified by:
shutdown in interface ShutdownStrategy
Parameters:
context - the camel context
route - the route
timeout - timeout
timeUnit - the unit to use
abortAfterTimeout - should abort shutdown after timeout
Returns:
true if the route is stopped before the timeout
Throws:
Exception - is thrown if error shutting down the consumer, however its preferred to avoid this

suspend

public void suspend(CamelContext context,
                    List<RouteStartupOrder> routes,
                    long timeout,
                    TimeUnit timeUnit)
             throws Exception
Description copied from interface: ShutdownStrategy
Suspends the routes using a specified timeout instead of the default timeout values

Specified by:
suspend in interface ShutdownStrategy
Parameters:
context - the camel context
routes - the routes, ordered by the order they was started
timeout - timeout
timeUnit - the unit to use
Throws:
Exception - is thrown if error suspending the consumers, however its preferred to avoid this

doShutdown

protected boolean doShutdown(CamelContext context,
                             List<RouteStartupOrder> routes,
                             long timeout,
                             TimeUnit timeUnit,
                             boolean suspendOnly,
                             boolean abortAfterTimeout,
                             boolean forceShutdown)
                      throws Exception
Throws:
Exception

forceShutdown

public boolean forceShutdown(Service service)
Description copied from interface: ShutdownStrategy
Whether a service is forced to shutdown.

Can be used to signal to services that they are no longer allowed to run, such as if a forced shutdown is currently in progress.

For example the Camel RedeliveryErrorHandler uses this information to know if a forced shutdown is in progress, and then break out of redelivery attempts.

Specified by:
forceShutdown in interface ShutdownStrategy
Parameters:
service - the service
Returns:
true indicates the service is to be forced to shutdown, false the service can keep running.

setTimeout

public void setTimeout(long timeout)
Description copied from interface: ShutdownStrategy
Set an timeout to wait for the shutdown to complete.

Setting a value of 0 or negative will disable timeout and wait until complete (potential blocking forever)

The default timeout unit is SECONDS

Specified by:
setTimeout in interface ShutdownStrategy
Parameters:
timeout - timeout

getTimeout

public long getTimeout()
Description copied from interface: ShutdownStrategy
Gets the timeout.

Use positive value to set the timeout

The default timeout unit is SECONDS

Specified by:
getTimeout in interface ShutdownStrategy
Returns:
the timeout

setTimeUnit

public void setTimeUnit(TimeUnit timeUnit)
Description copied from interface: ShutdownStrategy
Set the time unit to use

Specified by:
setTimeUnit in interface ShutdownStrategy
Parameters:
timeUnit - the unit to use

getTimeUnit

public TimeUnit getTimeUnit()
Description copied from interface: ShutdownStrategy
Gets the time unit used

Specified by:
getTimeUnit in interface ShutdownStrategy
Returns:
the time unit

setShutdownNowOnTimeout

public void setShutdownNowOnTimeout(boolean shutdownNowOnTimeout)
Description copied from interface: ShutdownStrategy
Sets whether to force shutdown of all consumers when a timeout occurred and thus not all consumers was shutdown within that period.

You should have good reasons to set this option to false as it means that the routes keep running and is halted abruptly when CamelContext has been shutdown.

Specified by:
setShutdownNowOnTimeout in interface ShutdownStrategy
Parameters:
shutdownNowOnTimeout - true to force shutdown, false to leave them running

isShutdownNowOnTimeout

public boolean isShutdownNowOnTimeout()
Description copied from interface: ShutdownStrategy
Whether to force shutdown of all consumers when a timeout occurred.

Specified by:
isShutdownNowOnTimeout in interface ShutdownStrategy
Returns:
force shutdown or not

isShutdownRoutesInReverseOrder

public boolean isShutdownRoutesInReverseOrder()
Description copied from interface: ShutdownStrategy
Whether to shutdown routes in reverse order than they where started.

This option is by default set to true.

Specified by:
isShutdownRoutesInReverseOrder in interface ShutdownStrategy
Returns:
true if routes should be shutdown in reverse order.

setShutdownRoutesInReverseOrder

public void setShutdownRoutesInReverseOrder(boolean shutdownRoutesInReverseOrder)
Description copied from interface: ShutdownStrategy
Sets whether routes should be shutdown in reverse or the same order as they where started.

Specified by:
setShutdownRoutesInReverseOrder in interface ShutdownStrategy
Parameters:
shutdownRoutesInReverseOrder - true to shutdown in reverse order

getCamelContext

public CamelContext getCamelContext()
Description copied from interface: CamelContextAware
Get the CamelContext

Specified by:
getCamelContext in interface CamelContextAware
Returns:
camelContext the Camel context

setCamelContext

public void setCamelContext(CamelContext camelContext)
Description copied from interface: CamelContextAware
Injects the CamelContext

Specified by:
setCamelContext in interface CamelContextAware
Parameters:
camelContext - the Camel context

shutdownRoutesNow

protected void shutdownRoutesNow(List<RouteStartupOrder> routes)
Shutdown all the consumers immediately.

Parameters:
routes - the routes to shutdown

shutdownNow

protected void shutdownNow(List<Consumer> consumers)
Shutdown all the consumers immediately.

Parameters:
consumers - the consumers to shutdown

shutdownNow

protected static void shutdownNow(Consumer consumer)
Shutdown the consumer immediately.

Parameters:
consumer - the consumer to shutdown

suspendNow

protected static void suspendNow(Consumer consumer)
Suspends/stops the consumer immediately.

Parameters:
consumer - the consumer to suspend

doStart

protected void doStart()
                throws Exception
Description copied from class: ServiceSupport
Implementations override this method to support customized start/stop.

Important: See ServiceSupport.doStop() for more details.

Specified by:
doStart in class ServiceSupport
Throws:
Exception
See Also:
ServiceSupport.doStop()

doStop

protected void doStop()
               throws Exception
Description copied from class: ServiceSupport
Implementations override this method to support customized start/stop.

Important: Camel will invoke this ServiceSupport.doStop() method when the service is being stopped. This method will also be invoked if the service is still in uninitialized state (eg has not been started). The method is always called to allow the service to do custom logic when the service is being stopped, such as when CamelContext is shutting down.

Specified by:
doStop in class ServiceSupport
Throws:
Exception
See Also:
ServiceSupport.doStart()

doShutdown

protected void doShutdown()
                   throws Exception
Description copied from class: ServiceSupport
Implementations override this method to perform customized shutdown.

Overrides:
doShutdown in class ServiceSupport
Throws:
Exception


Apache CAMEL