Class Throttler

  • All Implemented Interfaces:
    AutoCloseable, org.apache.camel.AsyncProcessor, org.apache.camel.Processor, org.apache.camel.Service, org.apache.camel.ShutdownableService, org.apache.camel.spi.HasId, org.apache.camel.spi.IdAware, org.apache.camel.spi.RouteIdAware, org.apache.camel.StatefulService, org.apache.camel.SuspendableService, org.apache.camel.Traceable

    public class Throttler
    extends org.apache.camel.support.AsyncProcessorSupport
    implements org.apache.camel.Traceable, org.apache.camel.spi.IdAware, org.apache.camel.spi.RouteIdAware
    A Throttler will set a limit on the maximum number of message exchanges which can be sent to a processor within a specific time period.

    This pattern can be extremely useful if you have some external system which meters access; such as only allowing 100 requests per second; or if huge load can cause a particular system to malfunction or to reduce its throughput you might want to introduce some throttling. This throttle implementation is thread-safe and is therefore safe to be used by multiple concurrent threads in a single route. The throttling mechanism is a DelayQueue with maxRequestsPerPeriod permits on it. Each permit is set to be delayed by timePeriodMillis (except when the throttler is initialized or the throttle rate increased, then there is no delay for those permits). Callers trying to acquire a permit from the DelayQueue will block if necessary. The end result is a rolling window of time. Where from the callers point of view in the last timePeriodMillis no more than maxRequestsPerPeriod have been allowed to be acquired.

    • 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 
      Constructor Description
      Throttler​(org.apache.camel.CamelContext camelContext, org.apache.camel.Expression maxRequestsPerPeriodExpression, long timePeriodMillis, ScheduledExecutorService asyncExecutor, boolean shutdownAsyncExecutor, boolean rejectExecution, org.apache.camel.Expression correlation)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void doShutdown()  
      protected void doStart()  
      protected void doStop()  
      int getCurrentMaximumRequestsPerPeriod()
      Gets the current maximum request per period value.
      String getId()  
      org.apache.camel.Expression getMaximumRequestsPerPeriodExpression()  
      String getRouteId()  
      long getTimePeriodMillis()  
      String getTraceLabel()  
      boolean isAsyncDelayed()  
      boolean isCallerRunsWhenRejected()  
      boolean isRejectExecution()  
      boolean process​(org.apache.camel.Exchange exchange, org.apache.camel.AsyncCallback callback)  
      protected boolean processAsynchronously​(org.apache.camel.Exchange exchange, org.apache.camel.AsyncCallback callback, org.apache.camel.processor.Throttler.ThrottlingState throttlingState)
      Delegate blocking on the DelayQueue to an asyncExecutor.
      void setAsyncDelayed​(boolean asyncDelayed)  
      void setCallerRunsWhenRejected​(boolean callerRunsWhenRejected)  
      void setId​(String id)  
      void setMaximumRequestsPerPeriodExpression​(org.apache.camel.Expression maxRequestsPerPeriodExpression)
      Sets the maximum number of requests per time period expression
      void setRejectExecution​(boolean rejectExecution)  
      void setRouteId​(String routeId)  
      void setTimePeriodMillis​(long timePeriodMillis)
      Sets the time period during which the maximum number of requests apply
      String toString()  
      • Methods inherited from class org.apache.camel.support.AsyncProcessorSupport

        process, processAsync
      • 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.spi.IdAware

        setGeneratedId
      • 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

      • Throttler

        public Throttler​(org.apache.camel.CamelContext camelContext,
                         org.apache.camel.Expression maxRequestsPerPeriodExpression,
                         long timePeriodMillis,
                         ScheduledExecutorService asyncExecutor,
                         boolean shutdownAsyncExecutor,
                         boolean rejectExecution,
                         org.apache.camel.Expression correlation)
    • Method Detail

      • process

        public boolean process​(org.apache.camel.Exchange exchange,
                               org.apache.camel.AsyncCallback callback)
        Specified by:
        process in interface org.apache.camel.AsyncProcessor
      • processAsynchronously

        protected boolean processAsynchronously​(org.apache.camel.Exchange exchange,
                                                org.apache.camel.AsyncCallback callback,
                                                org.apache.camel.processor.Throttler.ThrottlingState throttlingState)
        Delegate blocking on the DelayQueue to an asyncExecutor. Except if the executor rejects the submission and isCallerRunsWhenRejected() is enabled, then this method will delegate back to process(), but not before changing the exchange state to stop any recursion.
      • doStart

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

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

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

        public boolean isRejectExecution()
      • setRejectExecution

        public void setRejectExecution​(boolean rejectExecution)
      • isAsyncDelayed

        public boolean isAsyncDelayed()
      • setAsyncDelayed

        public void setAsyncDelayed​(boolean asyncDelayed)
      • isCallerRunsWhenRejected

        public boolean isCallerRunsWhenRejected()
      • setCallerRunsWhenRejected

        public void setCallerRunsWhenRejected​(boolean callerRunsWhenRejected)
      • getId

        public String getId()
        Specified by:
        getId in interface org.apache.camel.spi.HasId
      • setId

        public void setId​(String id)
        Specified by:
        setId in interface org.apache.camel.spi.IdAware
      • getRouteId

        public String getRouteId()
        Specified by:
        getRouteId in interface org.apache.camel.spi.RouteIdAware
      • setRouteId

        public void setRouteId​(String routeId)
        Specified by:
        setRouteId in interface org.apache.camel.spi.RouteIdAware
      • setMaximumRequestsPerPeriodExpression

        public void setMaximumRequestsPerPeriodExpression​(org.apache.camel.Expression maxRequestsPerPeriodExpression)
        Sets the maximum number of requests per time period expression
      • getMaximumRequestsPerPeriodExpression

        public org.apache.camel.Expression getMaximumRequestsPerPeriodExpression()
      • getCurrentMaximumRequestsPerPeriod

        public int getCurrentMaximumRequestsPerPeriod()
        Gets the current maximum request per period value. If it is grouped throttling applied with correlationExpression than the max per period within the group will return
      • setTimePeriodMillis

        public void setTimePeriodMillis​(long timePeriodMillis)
        Sets the time period during which the maximum number of requests apply
      • getTimePeriodMillis

        public long getTimePeriodMillis()
      • getTraceLabel

        public String getTraceLabel()
        Specified by:
        getTraceLabel in interface org.apache.camel.Traceable