Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

You configure the route policy as follows from Java DSL, using the routePolicy method:

Code Block

  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:

Code Block
xml
xml

   <bean id="myPolicy" class="com.mycompany.MyRoutePolicy"/>
   
   <route routePolicyRef="myPolicy">
       <from uri="seda:foo"/>
       <to uri="mock:result"/>
   </route>

...

In the example below, the route testRoute has a startPolicy and throttlePolicy applied concurrently. Both policies are applied as necessary on the route.

Code Block
xml
xml

   <bean id="date" class="org.apache.camel.routepolicy.quartz.SimpleDate"/>

    <bean id="startPolicy" class="org.apache.camel.routepolicy.quartz.SimpleScheduledRoutePolicy">
    	<property name="routeStartDate" ref="date"/>
    	<property name="routeStartRepeatCount" value="1"/>
    	<property name="routeStartRepeatInterval" value="3000"/>    	
    </bean>
    
    <bean id="throttlePolicy" class="org.apache.camel.impl.ThrottlingInflightRoutePolicy">
    	<property name="maxInflightExchanges" value="10"/>    	
	</bean>
	 	
    <camelContext id="testRouteContext" xmlns="http://camel.apache.org/schema/spring">
        <route id="testRoute" autoStartup="false" routePolicyRef="startPolicy, throttlePolicy">
            <from uri="seda:foo?concurrentConsumers=20"/>
            <to uri="mock:result"/>
        </route>
    </camelContext>
   </route>

Using RoutePolicyFactory

Available as of Camel 2.14

If you want to use a route policy for every route, you can use a org.apache.camel.spi.RoutePolicyFactory as a factory for creating a RoutePolicy instance for each route. This can be used when you want to use the same kind of route policy for every routes. Then you need to only configure the factory once, and every route created will have the policy assigned.

There is API on CamelContext to add a factory, as shown below

Code Block
context.addRoutePolicyFactory(new MyRoutePolicyFactory());

And from XML DSL you just define a <bean> with the factory

Code Block
<bean id="myRoutePolicyFactory" class="com.foo.MyRoutePolicyFactory"/>

The factory has a single method that creates the route policy

Code Block
    /**
     * Creates a new {@link org.apache.camel.spi.RoutePolicy} which will be assigned to the given route.
     *
     * @param camelContext the camel context
     * @param routeId      the route id
     * @param route        the route definition
     * @return the created {@link org.apache.camel.spi.RoutePolicy}, or <tt>null</tt> to not use a policy for this route
     */
    RoutePolicy createRoutePolicy(CamelContext camelContext, String routeId, RouteDefinition route);

Note you can have as many route policy factories as you want. Just call the addRoutePolicyFactory again, or declare the other factories as <bean> in XML.

See Also