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.CamelContextAwareDefaultShutdownStrategywhich 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)andsetShutdownNowOnTimeout(boolean)methods. Routes will by default be shutdown in the reverse order of which they where started. You can customize this using thesetShutdownRoutesInReverseOrder(boolean)method. After route consumers have been shutdown, then anyShutdownPreparedservices on the routes is being prepared for shutdown, by invokingShutdownPrepared.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 invokesShutdownPrepared.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 optionsetSuppressLoggingOnTimeout(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, ifisLogInflightExchangesOnTimeout()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 theInflightRepositoryat all time during runtime.
-
-
Constructor Summary
Constructors Constructor Description DefaultShutdownStrategy()DefaultShutdownStrategy(org.apache.camel.CamelContext camelContext)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voiddoShutdown()protected 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.CamelContextgetCamelContext()Future<?>getCurrentShutdownTaskFuture()org.apache.camel.LoggingLevelgetLoggingLevel()protected static intgetPendingInflightExchanges(org.apache.camel.spi.RouteStartupOrder order)Calculates the total number of inflight exchanges for the given routelonggetTimeout()TimeUnitgetTimeUnit()booleanhasTimeoutOccurred()booleanisForceShutdown()booleanisLogInflightExchangesOnTimeout()booleanisShutdownNowOnTimeout()booleanisShutdownRoutesInReverseOrder()booleanisSuppressLoggingOnTimeout()protected 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.voidsuspend(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes)voidsuspend(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, suspend
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
-
-
Method Detail
-
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
public void setTimeUnit(TimeUnit timeUnit)
- Specified by:
setTimeUnitin interfaceorg.apache.camel.spi.ShutdownStrategy
-
getTimeUnit
public TimeUnit 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
public Future<?> getCurrentShutdownTaskFuture()
-
shutdownRoutesNow
protected void shutdownRoutesNow(List<org.apache.camel.spi.RouteStartupOrder> routes)
Shutdown all the consumers immediately.- Parameters:
routes- the routes to shutdown
-
shutdownNow
protected void shutdownNow(String routeId, List<org.apache.camel.Consumer> consumers)
Shutdown all the consumers immediately.- Parameters:
routeId- the route id to suspendconsumers- the consumers to shutdown
-
shutdownNow
protected void shutdownNow(String routeId, org.apache.camel.Consumer consumer)
Shutdown the consumer immediately.- Parameters:
routeId- the route id to suspendconsumer- the consumer to shutdown
-
suspendNow
protected void suspendNow(String routeId, org.apache.camel.Consumer consumer)
Suspends/stops the consumer immediately.- Parameters:
routeId- the route id to suspendconsumer- the consumer to suspend
-
doStart
protected void doStart() throws Exception- Overrides:
doStartin classorg.apache.camel.support.service.BaseService- Throws:
Exception
-
doStop
protected void doStop() throws Exception- Overrides:
doStopin classorg.apache.camel.support.service.BaseService- Throws:
Exception
-
doShutdown
protected void doShutdown() throws Exception- 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
-
-