public class DefaultShutdownStrategy extends ServiceSupport implements ShutdownStrategy, CamelContextAware
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:
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.shutdown, shuttingdown, started, starting, stopped, stopping, suspended, suspending
Constructor and Description |
---|
DefaultShutdownStrategy() |
DefaultShutdownStrategy(CamelContext camelContext) |
Modifier and Type | Method and Description |
---|---|
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 |
Future<?> |
getCurrentShutdownTaskFuture() |
protected static int |
getPendingInflightExchanges(RouteStartupOrder order)
Calculates the total number of inflight exchanges for the given route
|
long |
getTimeout()
Gets the timeout.
|
TimeUnit |
getTimeUnit()
Gets the time unit used
|
boolean |
hasTimeoutOccurred()
Whether a timeout has occurred during a shutdown.
|
boolean |
isLogInflightExchangesOnTimeout()
Whether to log information about the inflight
Exchange s which are still running
during a shutdown which didn't complete without the given timeout. |
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.
|
boolean |
isSuppressLoggingOnTimeout()
Whether Camel should try to suppress logging during shutdown and timeout was triggered,
meaning forced shutdown is happening.
|
protected static void |
logInflightExchanges(CamelContext camelContext,
List<RouteStartupOrder> routes,
boolean infoLevel)
Logs information about the inflight exchanges
|
void |
setCamelContext(CamelContext camelContext)
Injects the
CamelContext |
void |
setLogInflightExchangesOnTimeout(boolean logInflightExchangesOnTimeout)
Sets whether to log information about the inflight
Exchange s which are still running
during a shutdown which didn't complete without the given timeout. |
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 |
setSuppressLoggingOnTimeout(boolean suppressLoggingOnTimeout)
Whether Camel should try to suppress logging during shutdown and timeout was triggered,
meaning forced shutdown is happening.
|
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.
|
doResume, doSuspend, getStatus, getVersion, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isStoppingOrStopped, isSuspended, isSuspending, isSuspendingOrSuspended, resume, shutdown, start, stop, suspend
public DefaultShutdownStrategy()
public DefaultShutdownStrategy(CamelContext camelContext)
public void shutdown(CamelContext context, List<RouteStartupOrder> routes) throws Exception
ShutdownStrategy
shutdown
in interface ShutdownStrategy
context
- the camel contextroutes
- the routes, ordered by the order they was startedException
- is thrown if error shutting down the consumers, however its preferred to avoid thispublic void shutdownForced(CamelContext context, List<RouteStartupOrder> routes) throws Exception
ShutdownStrategy
CamelContext
is shutting down, to ensure Camel will shutdown
if messages seems to be stuck.shutdownForced
in interface ShutdownStrategy
context
- the camel contextroutes
- the routes, ordered by the order they was startedException
- is thrown if error shutting down the consumers, however its preferred to avoid thispublic void suspend(CamelContext context, List<RouteStartupOrder> routes) throws Exception
ShutdownStrategy
suspend
in interface ShutdownStrategy
context
- the camel contextroutes
- the routes, ordered by the order they was startedException
- is thrown if error suspending the consumers, however its preferred to avoid thispublic void shutdown(CamelContext context, List<RouteStartupOrder> routes, long timeout, TimeUnit timeUnit) throws Exception
ShutdownStrategy
shutdown
in interface ShutdownStrategy
context
- the camel contextroutes
- the routes, ordered by the order they was startedtimeout
- timeouttimeUnit
- the unit to useException
- is thrown if error shutting down the consumers, however its preferred to avoid thispublic boolean shutdown(CamelContext context, RouteStartupOrder route, long timeout, TimeUnit timeUnit, boolean abortAfterTimeout) throws Exception
ShutdownStrategy
shutdown
in interface ShutdownStrategy
context
- the camel contextroute
- the routetimeout
- timeouttimeUnit
- the unit to useabortAfterTimeout
- should abort shutdown after timeoutException
- is thrown if error shutting down the consumer, however its preferred to avoid thispublic void suspend(CamelContext context, List<RouteStartupOrder> routes, long timeout, TimeUnit timeUnit) throws Exception
ShutdownStrategy
suspend
in interface ShutdownStrategy
context
- the camel contextroutes
- the routes, ordered by the order they was startedtimeout
- timeouttimeUnit
- the unit to useException
- is thrown if error suspending the consumers, however its preferred to avoid thisprotected boolean doShutdown(CamelContext context, List<RouteStartupOrder> routes, long timeout, TimeUnit timeUnit, boolean suspendOnly, boolean abortAfterTimeout, boolean forceShutdown) throws Exception
Exception
public boolean forceShutdown(Service service)
ShutdownStrategy
RedeliveryErrorHandler
uses this information
to know if a forced shutdown is in progress, and then break out of redelivery attempts.forceShutdown
in interface ShutdownStrategy
service
- the servicepublic boolean hasTimeoutOccurred()
ShutdownStrategy
hasTimeoutOccurred
in interface ShutdownStrategy
public void setTimeout(long timeout)
ShutdownStrategy
Long.MAX_VALUE
The default timeout unit is SECONDSsetTimeout
in interface ShutdownStrategy
timeout
- timeoutpublic long getTimeout()
ShutdownStrategy
getTimeout
in interface ShutdownStrategy
public void setTimeUnit(TimeUnit timeUnit)
ShutdownStrategy
setTimeUnit
in interface ShutdownStrategy
timeUnit
- the unit to usepublic TimeUnit getTimeUnit()
ShutdownStrategy
getTimeUnit
in interface ShutdownStrategy
public void setShutdownNowOnTimeout(boolean shutdownNowOnTimeout)
ShutdownStrategy
CamelContext
has been shutdown.setShutdownNowOnTimeout
in interface ShutdownStrategy
shutdownNowOnTimeout
- true to force shutdown, false to leave them runningpublic boolean isShutdownNowOnTimeout()
ShutdownStrategy
isShutdownNowOnTimeout
in interface ShutdownStrategy
public boolean isShutdownRoutesInReverseOrder()
ShutdownStrategy
isShutdownRoutesInReverseOrder
in interface ShutdownStrategy
public void setShutdownRoutesInReverseOrder(boolean shutdownRoutesInReverseOrder)
ShutdownStrategy
setShutdownRoutesInReverseOrder
in interface ShutdownStrategy
shutdownRoutesInReverseOrder
- true to shutdown in reverse orderpublic boolean isSuppressLoggingOnTimeout()
ShutdownStrategy
isSuppressLoggingOnTimeout
in interface ShutdownStrategy
public void setSuppressLoggingOnTimeout(boolean suppressLoggingOnTimeout)
ShutdownStrategy
setSuppressLoggingOnTimeout
in interface ShutdownStrategy
suppressLoggingOnTimeout
- true to suppress logging, false to log as usual.public boolean isLogInflightExchangesOnTimeout()
ShutdownStrategy
Exchange
s which are still running
during a shutdown which didn't complete without the given timeout.isLogInflightExchangesOnTimeout
in interface ShutdownStrategy
public void setLogInflightExchangesOnTimeout(boolean logInflightExchangesOnTimeout)
ShutdownStrategy
Exchange
s which are still running
during a shutdown which didn't complete without the given timeout.setLogInflightExchangesOnTimeout
in interface ShutdownStrategy
logInflightExchangesOnTimeout
- true to log information about the inflight exchanges, false to not logpublic CamelContext getCamelContext()
CamelContextAware
CamelContext
getCamelContext
in interface CamelContextAware
public void setCamelContext(CamelContext camelContext)
CamelContextAware
CamelContext
setCamelContext
in interface CamelContextAware
camelContext
- the Camel contextpublic Future<?> getCurrentShutdownTaskFuture()
protected void shutdownRoutesNow(List<RouteStartupOrder> routes)
routes
- the routes to shutdownprotected void shutdownNow(List<Consumer> consumers)
consumers
- the consumers to shutdownprotected static void shutdownNow(Consumer consumer)
consumer
- the consumer to shutdownprotected static void suspendNow(Consumer consumer)
consumer
- the consumer to suspendprotected void doStart() throws Exception
ServiceSupport
ServiceSupport.doStop()
for more details.doStart
in class ServiceSupport
Exception
ServiceSupport.doStop()
protected void doStop() throws Exception
ServiceSupport
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.doStop
in class ServiceSupport
Exception
ServiceSupport.doStart()
protected void doShutdown() throws Exception
ServiceSupport
doShutdown
in class ServiceSupport
Exception
protected static int getPendingInflightExchanges(RouteStartupOrder order)
order
- the routeprotected static void logInflightExchanges(CamelContext camelContext, List<RouteStartupOrder> routes, boolean infoLevel)
infoLevel
- true to log at INFO level, false to log at DEBUG levelApache Camel