org.apache.camel.impl
Class ThrottlingInflightRoutePolicy

java.lang.Object
  extended by org.apache.camel.support.ServiceSupport
      extended by org.apache.camel.impl.RoutePolicySupport
          extended by org.apache.camel.impl.ThrottlingInflightRoutePolicy
All Implemented Interfaces:
CamelContextAware, Service, ShutdownableService, RoutePolicy, StatefulService, SuspendableService

public class ThrottlingInflightRoutePolicy
extends RoutePolicySupport
implements CamelContextAware

A throttle based RoutePolicy which is capable of dynamic throttling a route based on number of current inflight exchanges.

This implementation supports two scopes ThrottlingInflightRoutePolicy.ThrottlingScope.Context and ThrottlingInflightRoutePolicy.ThrottlingScope.Route (is default). If context scope is selected then this implementation will use a EventNotifier to listen for events when Exchanges is done, and trigger the throttle(org.apache.camel.Route, org.apache.camel.Exchange) method. If the route scope is selected then no EventNotifier is in use, as there is already a Synchronization callback on the current Exchange which triggers the throttle(org.apache.camel.Route, org.apache.camel.Exchange) when the current Exchange is done.

Version:

Nested Class Summary
static class ThrottlingInflightRoutePolicy.ThrottlingScope
           
 
Field Summary
 
Fields inherited from class org.apache.camel.impl.RoutePolicySupport
log
 
Fields inherited from class org.apache.camel.support.ServiceSupport
shutdown, shuttingdown, started, starting, stopped, stopping, suspended, suspending
 
Constructor Summary
ThrottlingInflightRoutePolicy()
           
 
Method Summary
protected  CamelLogger createLogger()
           
protected  void doStart()
          Implementations override this method to support customized start/stop.
protected  void doStop()
          Implementations override this method to support customized start/stop.
 CamelContext getCamelContext()
          Get the CamelContext
 CamelLogger getLogger()
           
 LoggingLevel getLoggingLevel()
           
 int getMaxInflightExchanges()
           
 int getResumePercentOfMax()
           
 ThrottlingInflightRoutePolicy.ThrottlingScope getScope()
           
 void onExchangeDone(Route route, Exchange exchange)
          Callback invoked when an Exchange is done being routed, where it started from the given Route

Notice this callback is invoked when the Exchange is done and the Route is the route where the Exchange was started.

 void onInit(Route route)
          Callback invoked when the Route is being initialized
 void setCamelContext(CamelContext camelContext)
          Injects the CamelContext
 void setLogger(CamelLogger logger)
          Sets the logger to use for logging throttling activity.
 void setLoggingLevel(LoggingLevel loggingLevel)
          Sets the logging level to report the throttling activity.
 void setMaxInflightExchanges(int maxInflightExchanges)
          Sets the upper limit of number of concurrent inflight exchanges at which point reached the throttler should suspend the route.
 void setResumePercentOfMax(int resumePercentOfMax)
          Sets at which percentage of the max the throttler should start resuming the route.
 void setScope(ThrottlingInflightRoutePolicy.ThrottlingScope scope)
          Sets which scope the throttling should be based upon, either route or total scoped.
protected  void throttle(Route route, Exchange exchange)
          Throttles the route when Exchanges is done.
 String toString()
           
 
Methods inherited from class org.apache.camel.impl.RoutePolicySupport
getExceptionHandler, handleException, onExchangeBegin, onRemove, onResume, onStart, onStop, onSuspend, resumeRoute, setExceptionHandler, startConsumer, startRoute, stopConsumer, stopRoute, stopRoute, suspendRoute, suspendRoute
 
Methods inherited from class org.apache.camel.support.ServiceSupport
doResume, doShutdown, doSuspend, getStatus, getVersion, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isSuspended, isSuspending, resume, shutdown, start, stop, suspend
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ThrottlingInflightRoutePolicy

public ThrottlingInflightRoutePolicy()
Method Detail

toString

public String toString()
Overrides:
toString in class Object

getCamelContext

public CamelContext getCamelContext()
Description copied from interface: CamelContextAware
Get the CamelContext

Specified by:
getCamelContext in interface CamelContextAware
Returns:
camelContext the Camel context

setCamelContext

public void setCamelContext(CamelContext camelContext)
Description copied from interface: CamelContextAware
Injects the CamelContext

Specified by:
setCamelContext in interface CamelContextAware
Parameters:
camelContext - the Camel context

onInit

public void onInit(Route route)
Description copied from interface: RoutePolicy
Callback invoked when the Route is being initialized

Specified by:
onInit in interface RoutePolicy
Overrides:
onInit in class RoutePolicySupport
Parameters:
route - the route being initialized

onExchangeDone

public void onExchangeDone(Route route,
                           Exchange exchange)
Description copied from interface: RoutePolicy
Callback invoked when an Exchange is done being routed, where it started from the given Route

Notice this callback is invoked when the Exchange is done and the Route is the route where the Exchange was started. Most often its also the route where the exchange is done. However its possible to route an Exchange to other routes using endpoints such as direct or seda. Bottom line is that the Route parameter may not be the endpoint route and thus why we state its the starting route.

Specified by:
onExchangeDone in interface RoutePolicy
Overrides:
onExchangeDone in class RoutePolicySupport
Parameters:
route - the route where the exchange started from
exchange - the created exchange

throttle

protected void throttle(Route route,
                        Exchange exchange)
Throttles the route when Exchanges is done.

Parameters:
route - the route
exchange - the exchange

getMaxInflightExchanges

public int getMaxInflightExchanges()

setMaxInflightExchanges

public void setMaxInflightExchanges(int maxInflightExchanges)
Sets the upper limit of number of concurrent inflight exchanges at which point reached the throttler should suspend the route.

Is default 1000.

Parameters:
maxInflightExchanges - the upper limit of concurrent inflight exchanges

getResumePercentOfMax

public int getResumePercentOfMax()

setResumePercentOfMax

public void setResumePercentOfMax(int resumePercentOfMax)
Sets at which percentage of the max the throttler should start resuming the route.

Will by default use 70%.

Parameters:
resumePercentOfMax - the percentage must be between 0 and 100

getScope

public ThrottlingInflightRoutePolicy.ThrottlingScope getScope()

setScope

public void setScope(ThrottlingInflightRoutePolicy.ThrottlingScope scope)
Sets which scope the throttling should be based upon, either route or total scoped.

Parameters:
scope - the scope

getLoggingLevel

public LoggingLevel getLoggingLevel()

getLogger

public CamelLogger getLogger()

setLogger

public void setLogger(CamelLogger logger)
Sets the logger to use for logging throttling activity.

Parameters:
logger - the logger

setLoggingLevel

public void setLoggingLevel(LoggingLevel loggingLevel)
Sets the logging level to report the throttling activity.

Is default INFO level.

Parameters:
loggingLevel - the logging level

createLogger

protected CamelLogger createLogger()

doStart

protected void doStart()
                throws Exception
Description copied from class: ServiceSupport
Implementations override this method to support customized start/stop.

Important: See ServiceSupport.doStop() for more details.

Overrides:
doStart in class RoutePolicySupport
Throws:
Exception
See Also:
ServiceSupport.doStop()

doStop

protected void doStop()
               throws Exception
Description copied from class: ServiceSupport
Implementations override this method to support customized start/stop.

Important: Camel will invoke this 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.

Overrides:
doStop in class RoutePolicySupport
Throws:
Exception
See Also:
ServiceSupport.doStart()


Apache CAMEL