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.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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidprotected booleandoShutdown(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes, long timeout, TimeUnit timeUnit, boolean suspendOnly, boolean abortAfterTimeout, boolean forceShutdown) protected voiddoStart()protected voiddoStop()org.apache.camel.CamelContextFuture<?>Future for the current shutdown task, when a task is in progress.org.apache.camel.LoggingLevelprotected static intgetPendingInflightExchanges(org.apache.camel.spi.RouteStartupOrder order) Calculates the total number of inflight exchanges for the given routelongbooleanbooleanbooleanbooleanbooleanbooleanprotected voidlogInflightExchanges(org.apache.camel.CamelContext camelContext, List<org.apache.camel.spi.RouteStartupOrder> routes, boolean infoLevel) Logs information about the inflight exchangesvoidsetCamelContext(org.apache.camel.CamelContext camelContext) voidsetLoggingLevel(org.apache.camel.LoggingLevel loggingLevel) voidsetLogInflightExchangesOnTimeout(boolean logInflightExchangesOnTimeout) voidsetShutdownNowOnTimeout(boolean shutdownNowOnTimeout) voidsetShutdownRoutesInReverseOrder(boolean shutdownRoutesInReverseOrder) voidsetSuppressLoggingOnTimeout(boolean suppressLoggingOnTimeout) voidsetTimeout(long timeout) voidsetTimeUnit(TimeUnit timeUnit) voidshutdown(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes) voidshutdown(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes, long timeout, TimeUnit timeUnit) booleanshutdown(org.apache.camel.CamelContext context, org.apache.camel.spi.RouteStartupOrder route, long timeout, TimeUnit timeUnit, boolean abortAfterTimeout) voidshutdownForced(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes) protected voidshutdownNow(String routeId, List<org.apache.camel.Consumer> consumers) Shutdown all the consumers immediately.protected voidshutdownNow(String routeId, org.apache.camel.Consumer consumer) Shutdown the consumer immediately.protected voidshutdownRoutesNow(List<org.apache.camel.spi.RouteStartupOrder> routes) Shutdown all the consumers immediately.voidvoidsuspend(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes, long timeout, TimeUnit timeUnit) protected voidsuspendNow(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, doSuspend, fail, getStatus, init, isBuild, isInit, isNew, isRunAllowed, isShutdown, isStarted, isStarting, isStartingOrStarted, isStopped, isStopping, isStoppingOrStopped, isSuspended, isSuspending, isSuspendingOrSuspended, resume, shutdown, start, stop, suspendMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.camel.Service
build, close, init, start, stopMethods inherited from interface org.apache.camel.ShutdownableService
shutdownMethods inherited from interface org.apache.camel.StatefulService
getStatus, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isSuspendingMethods 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:
shutdownin 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:
shutdownForcedin 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:
suspendin 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:
shutdownin 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:
shutdownin 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:
suspendin 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:
isForceShutdownin interfaceorg.apache.camel.spi.ShutdownStrategy
-
hasTimeoutOccurred
public boolean hasTimeoutOccurred()- Specified by:
hasTimeoutOccurredin interfaceorg.apache.camel.spi.ShutdownStrategy
-
setTimeout
public void setTimeout(long timeout) - Specified by:
setTimeoutin interfaceorg.apache.camel.spi.ShutdownStrategy
-
getTimeout
public long getTimeout()- Specified by:
getTimeoutin interfaceorg.apache.camel.spi.ShutdownStrategy
-
setTimeUnit
- Specified by:
setTimeUnitin interfaceorg.apache.camel.spi.ShutdownStrategy
-
getTimeUnit
- Specified by:
getTimeUnitin interfaceorg.apache.camel.spi.ShutdownStrategy
-
setShutdownNowOnTimeout
public void setShutdownNowOnTimeout(boolean shutdownNowOnTimeout) - Specified by:
setShutdownNowOnTimeoutin interfaceorg.apache.camel.spi.ShutdownStrategy
-
isShutdownNowOnTimeout
public boolean isShutdownNowOnTimeout()- Specified by:
isShutdownNowOnTimeoutin interfaceorg.apache.camel.spi.ShutdownStrategy
-
isShutdownRoutesInReverseOrder
public boolean isShutdownRoutesInReverseOrder()- Specified by:
isShutdownRoutesInReverseOrderin interfaceorg.apache.camel.spi.ShutdownStrategy
-
setShutdownRoutesInReverseOrder
public void setShutdownRoutesInReverseOrder(boolean shutdownRoutesInReverseOrder) - Specified by:
setShutdownRoutesInReverseOrderin interfaceorg.apache.camel.spi.ShutdownStrategy
-
isSuppressLoggingOnTimeout
public boolean isSuppressLoggingOnTimeout()- Specified by:
isSuppressLoggingOnTimeoutin interfaceorg.apache.camel.spi.ShutdownStrategy
-
setSuppressLoggingOnTimeout
public void setSuppressLoggingOnTimeout(boolean suppressLoggingOnTimeout) - Specified by:
setSuppressLoggingOnTimeoutin interfaceorg.apache.camel.spi.ShutdownStrategy
-
isLogInflightExchangesOnTimeout
public boolean isLogInflightExchangesOnTimeout()- Specified by:
isLogInflightExchangesOnTimeoutin interfaceorg.apache.camel.spi.ShutdownStrategy
-
setLogInflightExchangesOnTimeout
public void setLogInflightExchangesOnTimeout(boolean logInflightExchangesOnTimeout) - Specified by:
setLogInflightExchangesOnTimeoutin interfaceorg.apache.camel.spi.ShutdownStrategy
-
getLoggingLevel
public org.apache.camel.LoggingLevel getLoggingLevel()- Specified by:
getLoggingLevelin interfaceorg.apache.camel.spi.ShutdownStrategy
-
setLoggingLevel
public void setLoggingLevel(org.apache.camel.LoggingLevel loggingLevel) - Specified by:
setLoggingLevelin interfaceorg.apache.camel.spi.ShutdownStrategy
-
getCamelContext
public org.apache.camel.CamelContext getCamelContext()- Specified by:
getCamelContextin interfaceorg.apache.camel.CamelContextAware
-
setCamelContext
public void setCamelContext(org.apache.camel.CamelContext camelContext) - Specified by:
setCamelContextin 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:
doStartin classorg.apache.camel.support.service.BaseService- Throws:
Exception
-
doStop
- Overrides:
doStopin classorg.apache.camel.support.service.BaseService- Throws:
Exception
-
doShutdown
- Overrides:
doShutdownin 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
-