RoutePolicy
Available as of Camel 2.1
A route policy org.apache.camel.spi.RoutePolicy
is used to control route(s) at runtime. For example you can use it to determine whether a route should be running or not. However the policies is can support any kind of use cases.
How it works
You associate a route with a given RoutePolicy
and then during runtime Camel will invoke callbacks on this policy where you can implement your custom logic.
Camel provides a support class that is a good base class to extend org.apache.camel.impl.RoutePolicySupport
.
There are two callbacks invoked
onExchangeBegin
onExchangeDone
See the javadoc of theorg.apache.camel.spi.RoutePolicy
for more details.
And also the implementation of theorg.apache.camel.impl.ThrottlingRoutePolicy
for a concrete example.
Camel provides the following policies out of the box
org.apache.camel.impl.ThrottlingRoutePolicy
- a throttling based policy that automatic suspends/resumes route(s) based on metrics from the current in flight exchanges. You can use this to dynamic throttle e.g. a JMS consumer to avoid it consuming too fast.
ThrottlingRoutePolicy
The throttling route policy has the following options:
Option |
Default |
Description |
---|---|---|
scope |
Route |
A scope for either |
maxInflightExchanges |
1000 |
The maximum threshold when the throttling will start to suspend the route if the current number of inlfight exchanges is higher than this value. |
resumePercentOfMax |
70 |
A percentage 0..100 which defines when the throttling should resume again in case it has been suspended. |
loggingLevel |
INFO |
The logging level used for logging the throttling activity. |
logger |
ThrottlingRoutePolicy |
The logger category. |
Configuring policy
You configure the route policy as follows from Java DSL, using the routePolicy
method:
RoutePolicy myPolicy = new MyRoutePolicy(); from("seda:foo").routePolicy(myPolicy).to("mock:result");
In Spring XML its a bit different as follows using the routePolicyRef
attribute:
<bean id="myPolicy" class="com.mycompany.MyRoutePolicy"/> <route outePolicyRef="myPolicy"> <from uri="seda:foo"/> <to uri="mock:result"/> </route>
See Also
- Architecture
- The
camel-example-route-throttling
example in the Camel distribution