Package org.apache.camel.impl.engine
Class DefaultShutdownStrategy
java.lang.Object
org.apache.camel.support.service.BaseService
org.apache.camel.support.service.ServiceSupport
org.apache.camel.impl.engine.DefaultShutdownStrategy
- All Implemented Interfaces:
AutoCloseable
,org.apache.camel.CamelContextAware
,org.apache.camel.Service
,org.apache.camel.ShutdownableService
,org.apache.camel.spi.HasCamelContext
,org.apache.camel.spi.ShutdownStrategy
,org.apache.camel.StatefulService
,org.apache.camel.StaticService
,org.apache.camel.SuspendableService
public class DefaultShutdownStrategy
extends org.apache.camel.support.service.ServiceSupport
implements org.apache.camel.spi.ShutdownStrategy, org.apache.camel.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:
- Graceful - By suspending/stopping consumers, and let any in-flight exchanges complete
- Forced - After a given period of time, a timeout occurred and if there are still pending exchanges to complete, then a more aggressive forced strategy is performed.
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,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,boolean)
with force=true on the services. This allows the
services to know they should force shutdown now.
When timeout occurred and a forced shutdown is happening, then there may be threads/tasks which are still inflight
which may be rejected continued being routed. By default this can cause WARN and ERRORs to be logged. The option
setSuppressLoggingOnTimeout(boolean)
can be used to suppress these logs, so they are logged at TRACE level
instead.
Also when a timeout occurred then information about the inflight exchanges is logged, if
isLogInflightExchangesOnTimeout()
is enabled (is by default). This allows end users to known where these
inflight exchanges currently are in the route(s), and how long time they have been inflight.
This information can also be obtained from the InflightRepository
at all time during
runtime.-
Field Summary
Fields inherited from class org.apache.camel.support.service.BaseService
BUILT, FAILED, INITIALIZED, INITIALIZING, lock, NEW, SHUTDOWN, SHUTTING_DOWN, STARTED, STARTING, status, STOPPED, STOPPING, SUSPENDED, SUSPENDING
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
protected boolean
doShutdown
(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes, long timeout, TimeUnit timeUnit, boolean suspendOnly, boolean abortAfterTimeout, boolean forceShutdown) protected void
doStart()
org.apache.camel.CamelContext
Future
<?> Future for the current shutdown task, when a task is in progress.org.apache.camel.LoggingLevel
protected static int
getPendingInflightExchanges
(org.apache.camel.spi.RouteStartupOrder order) Calculates the total number of inflight exchanges for the given routelong
boolean
boolean
boolean
boolean
boolean
boolean
protected void
logInflightExchanges
(org.apache.camel.CamelContext camelContext, List<org.apache.camel.spi.RouteStartupOrder> routes, boolean infoLevel) Logs information about the inflight exchangesvoid
setCamelContext
(org.apache.camel.CamelContext camelContext) void
setLoggingLevel
(org.apache.camel.LoggingLevel loggingLevel) void
setLogInflightExchangesOnTimeout
(boolean logInflightExchangesOnTimeout) void
setShutdownNowOnTimeout
(boolean shutdownNowOnTimeout) void
setShutdownRoutesInReverseOrder
(boolean shutdownRoutesInReverseOrder) void
setSuppressLoggingOnTimeout
(boolean suppressLoggingOnTimeout) void
setTimeout
(long timeout) void
setTimeUnit
(TimeUnit timeUnit) void
shutdown
(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes) void
shutdown
(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes, long timeout, TimeUnit timeUnit) boolean
shutdown
(org.apache.camel.CamelContext context, org.apache.camel.spi.RouteStartupOrder route, long timeout, TimeUnit timeUnit, boolean abortAfterTimeout) void
shutdownForced
(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes) protected void
shutdownNow
(String routeId, List<org.apache.camel.Consumer> consumers) Shutdown all the consumers immediately.protected void
shutdownNow
(String routeId, org.apache.camel.Consumer consumer) Shutdown the consumer immediately.protected void
shutdownRoutesNow
(List<org.apache.camel.spi.RouteStartupOrder> routes) Shutdown all the consumers immediately.void
void
suspend
(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes, long timeout, TimeUnit timeUnit) protected void
suspendNow
(String routeId, org.apache.camel.Consumer consumer) Suspends/stops the consumer immediately.Methods inherited from class org.apache.camel.support.service.BaseService
build, doBuild, doFail, doInit, doLifecycleChange, doResume, doStop, doSuspend, fail, getStatus, init, isBuild, isInit, isNew, isRunAllowed, isShutdown, isStarted, isStarting, isStartingOrStarted, isStopped, isStopping, isStoppingOrStopped, isSuspended, isSuspending, isSuspendingOrSuspended, 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
build, close, init, start, stop
Methods inherited from interface org.apache.camel.ShutdownableService
shutdown
Methods inherited from interface org.apache.camel.StatefulService
getStatus, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isSuspending
Methods inherited from interface org.apache.camel.SuspendableService
isSuspended, resume, suspend
-
Constructor Details
-
DefaultShutdownStrategy
public DefaultShutdownStrategy() -
DefaultShutdownStrategy
public DefaultShutdownStrategy(org.apache.camel.CamelContext camelContext)
-
-
Method Details
-
shutdown
public void shutdown(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes) throws Exception - Specified by:
shutdown
in interfaceorg.apache.camel.spi.ShutdownStrategy
- Throws:
Exception
-
shutdownForced
public void shutdownForced(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes) throws Exception - Specified by:
shutdownForced
in interfaceorg.apache.camel.spi.ShutdownStrategy
- Throws:
Exception
-
suspend
public void suspend(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes) throws Exception - Specified by:
suspend
in interfaceorg.apache.camel.spi.ShutdownStrategy
- Throws:
Exception
-
shutdown
public void shutdown(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes, long timeout, TimeUnit timeUnit) throws Exception - Specified by:
shutdown
in interfaceorg.apache.camel.spi.ShutdownStrategy
- Throws:
Exception
-
shutdown
public boolean shutdown(org.apache.camel.CamelContext context, org.apache.camel.spi.RouteStartupOrder route, long timeout, TimeUnit timeUnit, boolean abortAfterTimeout) throws Exception - Specified by:
shutdown
in interfaceorg.apache.camel.spi.ShutdownStrategy
- Throws:
Exception
-
suspend
public void suspend(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes, long timeout, TimeUnit timeUnit) throws Exception - Specified by:
suspend
in interfaceorg.apache.camel.spi.ShutdownStrategy
- Throws:
Exception
-
doShutdown
protected boolean doShutdown(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes, long timeout, TimeUnit timeUnit, boolean suspendOnly, boolean abortAfterTimeout, boolean forceShutdown) throws Exception - Throws:
Exception
-
isForceShutdown
public boolean isForceShutdown()- Specified by:
isForceShutdown
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
hasTimeoutOccurred
public boolean hasTimeoutOccurred()- Specified by:
hasTimeoutOccurred
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
setTimeout
public void setTimeout(long timeout) - Specified by:
setTimeout
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
getTimeout
public long getTimeout()- Specified by:
getTimeout
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
setTimeUnit
- Specified by:
setTimeUnit
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
getTimeUnit
- Specified by:
getTimeUnit
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
setShutdownNowOnTimeout
public void setShutdownNowOnTimeout(boolean shutdownNowOnTimeout) - Specified by:
setShutdownNowOnTimeout
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
isShutdownNowOnTimeout
public boolean isShutdownNowOnTimeout()- Specified by:
isShutdownNowOnTimeout
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
isShutdownRoutesInReverseOrder
public boolean isShutdownRoutesInReverseOrder()- Specified by:
isShutdownRoutesInReverseOrder
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
setShutdownRoutesInReverseOrder
public void setShutdownRoutesInReverseOrder(boolean shutdownRoutesInReverseOrder) - Specified by:
setShutdownRoutesInReverseOrder
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
isSuppressLoggingOnTimeout
public boolean isSuppressLoggingOnTimeout()- Specified by:
isSuppressLoggingOnTimeout
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
setSuppressLoggingOnTimeout
public void setSuppressLoggingOnTimeout(boolean suppressLoggingOnTimeout) - Specified by:
setSuppressLoggingOnTimeout
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
isLogInflightExchangesOnTimeout
public boolean isLogInflightExchangesOnTimeout()- Specified by:
isLogInflightExchangesOnTimeout
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
setLogInflightExchangesOnTimeout
public void setLogInflightExchangesOnTimeout(boolean logInflightExchangesOnTimeout) - Specified by:
setLogInflightExchangesOnTimeout
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
getLoggingLevel
public org.apache.camel.LoggingLevel getLoggingLevel()- Specified by:
getLoggingLevel
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
setLoggingLevel
public void setLoggingLevel(org.apache.camel.LoggingLevel loggingLevel) - Specified by:
setLoggingLevel
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
getCamelContext
public org.apache.camel.CamelContext getCamelContext()- Specified by:
getCamelContext
in interfaceorg.apache.camel.spi.HasCamelContext
-
setCamelContext
public void setCamelContext(org.apache.camel.CamelContext camelContext) - Specified by:
setCamelContext
in interfaceorg.apache.camel.CamelContextAware
-
getCurrentShutdownTaskFuture
Future for the current shutdown task, when a task is in progress. Important: This API is only for advanced use-cases. -
shutdownRoutesNow
Shutdown all the consumers immediately.- Parameters:
routes
- the routes to shutdown
-
shutdownNow
Shutdown all the consumers immediately.- Parameters:
routeId
- the route id to suspendconsumers
- the consumers to shutdown
-
shutdownNow
Shutdown the consumer immediately.- Parameters:
routeId
- the route id to suspendconsumer
- the consumer to shutdown
-
suspendNow
Suspends/stops the consumer immediately.- Parameters:
routeId
- the route id to suspendconsumer
- the consumer to suspend
-
doStart
- Overrides:
doStart
in classorg.apache.camel.support.service.BaseService
- Throws:
Exception
-
doShutdown
- Overrides:
doShutdown
in classorg.apache.camel.support.service.BaseService
- Throws:
Exception
-
getPendingInflightExchanges
protected static int getPendingInflightExchanges(org.apache.camel.spi.RouteStartupOrder order) Calculates the total number of inflight exchanges for the given route- Parameters:
order
- the route- Returns:
- number of inflight exchanges
-
logInflightExchanges
protected void logInflightExchanges(org.apache.camel.CamelContext camelContext, List<org.apache.camel.spi.RouteStartupOrder> routes, boolean infoLevel) Logs information about the inflight exchanges- Parameters:
infoLevel
- true to log at INFO level, false to log at DEBUG level
-