Class 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.
    The idea by the graceful shutdown strategy, is to stop taking in more new messages, and allow any existing inflight messages to complete. Then when there is no more inflight messages then the routes can be fully shutdown. This mean that if there is inflight messages then we will have to wait for these messages to complete. If they do not complete after a period of time, then a timeout triggers. And then a more aggressive strategy takes over.

    The idea by the forced shutdown strategy, is to stop continue processing messages. And force routes and its services to shutdown now. There is a risk when shutting down now, that some resources is not properly shutdown, which can cause side effects. The timeout value is by default 45 seconds, but can be customized.

    As this strategy will politely wait until all exchanges has been completed it can potential wait for a long time, and hence why a timeout value can be set. When the timeout triggers you can also specify whether the remainder consumers should be shutdown now or ignore.

    Will by default use a timeout of 45 seconds by which it will shutdown now the remaining consumers. This ensures that when shutting down Camel it at some point eventually will shutdown. This behavior can of course be configured using the 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

        BUILDED, FAILED, INITIALIZED, INITIALIZING, lock, NEW, SHUTDOWN, SHUTTINGDOWN, STARTED, STARTING, status, STOPPED, STOPPING, SUSPENDED, SUSPENDING
    • 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 route
      long 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 exchanges
      void 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 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 Detail

      • DefaultShutdownStrategy

        public DefaultShutdownStrategy()
      • DefaultShutdownStrategy

        public DefaultShutdownStrategy​(org.apache.camel.CamelContext camelContext)
    • Method Detail

      • shutdown

        public void shutdown​(org.apache.camel.CamelContext context,
                             List<org.apache.camel.spi.RouteStartupOrder> routes)
                      throws Exception
        Specified by:
        shutdown in interface org.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 interface org.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 interface org.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 interface org.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 interface org.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 interface org.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 interface org.apache.camel.spi.ShutdownStrategy
      • hasTimeoutOccurred

        public boolean hasTimeoutOccurred()
        Specified by:
        hasTimeoutOccurred in interface org.apache.camel.spi.ShutdownStrategy
      • setTimeout

        public void setTimeout​(long timeout)
        Specified by:
        setTimeout in interface org.apache.camel.spi.ShutdownStrategy
      • getTimeout

        public long getTimeout()
        Specified by:
        getTimeout in interface org.apache.camel.spi.ShutdownStrategy
      • setTimeUnit

        public void setTimeUnit​(TimeUnit timeUnit)
        Specified by:
        setTimeUnit in interface org.apache.camel.spi.ShutdownStrategy
      • getTimeUnit

        public TimeUnit getTimeUnit()
        Specified by:
        getTimeUnit in interface org.apache.camel.spi.ShutdownStrategy
      • setShutdownNowOnTimeout

        public void setShutdownNowOnTimeout​(boolean shutdownNowOnTimeout)
        Specified by:
        setShutdownNowOnTimeout in interface org.apache.camel.spi.ShutdownStrategy
      • isShutdownNowOnTimeout

        public boolean isShutdownNowOnTimeout()
        Specified by:
        isShutdownNowOnTimeout in interface org.apache.camel.spi.ShutdownStrategy
      • isShutdownRoutesInReverseOrder

        public boolean isShutdownRoutesInReverseOrder()
        Specified by:
        isShutdownRoutesInReverseOrder in interface org.apache.camel.spi.ShutdownStrategy
      • setShutdownRoutesInReverseOrder

        public void setShutdownRoutesInReverseOrder​(boolean shutdownRoutesInReverseOrder)
        Specified by:
        setShutdownRoutesInReverseOrder in interface org.apache.camel.spi.ShutdownStrategy
      • isSuppressLoggingOnTimeout

        public boolean isSuppressLoggingOnTimeout()
        Specified by:
        isSuppressLoggingOnTimeout in interface org.apache.camel.spi.ShutdownStrategy
      • setSuppressLoggingOnTimeout

        public void setSuppressLoggingOnTimeout​(boolean suppressLoggingOnTimeout)
        Specified by:
        setSuppressLoggingOnTimeout in interface org.apache.camel.spi.ShutdownStrategy
      • isLogInflightExchangesOnTimeout

        public boolean isLogInflightExchangesOnTimeout()
        Specified by:
        isLogInflightExchangesOnTimeout in interface org.apache.camel.spi.ShutdownStrategy
      • setLogInflightExchangesOnTimeout

        public void setLogInflightExchangesOnTimeout​(boolean logInflightExchangesOnTimeout)
        Specified by:
        setLogInflightExchangesOnTimeout in interface org.apache.camel.spi.ShutdownStrategy
      • getCamelContext

        public org.apache.camel.CamelContext getCamelContext()
        Specified by:
        getCamelContext in interface org.apache.camel.CamelContextAware
      • setCamelContext

        public void setCamelContext​(org.apache.camel.CamelContext camelContext)
        Specified by:
        setCamelContext in interface org.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 suspend
        consumers - 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 suspend
        consumer - 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 suspend
        consumer - the consumer to suspend
      • doStart

        protected void doStart()
                        throws Exception
        Overrides:
        doStart in class org.apache.camel.support.service.BaseService
        Throws:
        Exception
      • doStop

        protected void doStop()
                       throws Exception
        Overrides:
        doStop in class org.apache.camel.support.service.BaseService
        Throws:
        Exception
      • doShutdown

        protected void doShutdown()
                           throws Exception
        Overrides:
        doShutdown in class org.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