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
DefaultShutdownStrategy
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)
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 anyShutdownPrepared
services 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 theInflightRepository
at 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 void
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)
protected void
doStart()
protected void
doStop()
boolean
forceShutdown(org.apache.camel.Service service)
org.apache.camel.CamelContext
getCamelContext()
Future<?>
getCurrentShutdownTaskFuture()
protected static int
getPendingInflightExchanges(org.apache.camel.spi.RouteStartupOrder order)
Calculates the total number of inflight exchanges for the given routelong
getTimeout()
TimeUnit
getTimeUnit()
boolean
hasTimeoutOccurred()
boolean
isLogInflightExchangesOnTimeout()
boolean
isShutdownNowOnTimeout()
boolean
isShutdownRoutesInReverseOrder()
boolean
isSuppressLoggingOnTimeout()
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
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
suspend(org.apache.camel.CamelContext context, List<org.apache.camel.spi.RouteStartupOrder> routes)
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, 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:
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
-
forceShutdown
public boolean forceShutdown(org.apache.camel.Service service)
- Specified by:
forceShutdown
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
public void setTimeUnit(TimeUnit timeUnit)
- Specified by:
setTimeUnit
in interfaceorg.apache.camel.spi.ShutdownStrategy
-
getTimeUnit
public TimeUnit 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
-
getCamelContext
public org.apache.camel.CamelContext getCamelContext()
- Specified by:
getCamelContext
in interfaceorg.apache.camel.CamelContextAware
-
setCamelContext
public void setCamelContext(org.apache.camel.CamelContext camelContext)
- Specified by:
setCamelContext
in 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:
doStart
in classorg.apache.camel.support.service.BaseService
- Throws:
Exception
-
doStop
protected void doStop() throws Exception
- Overrides:
doStop
in classorg.apache.camel.support.service.BaseService
- Throws:
Exception
-
doShutdown
protected void doShutdown() throws Exception
- 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
-
-