Search code examples
groovyapache-camelthrottlingeip

Throttle requests in camel not working


Below are my 3 routes in my base groovy routes class deployed as base framework.

from("jms:queue:EndPoint1?concurrentConsumers=100")
                .routePolicyRef("myPolicy")
                .transacted()
                .log("Recieved From Endpoint1")
                /*.to("log:Recieved From Endpoint1?groupSize=100")*/
                .to("CommonEndpoint");

        from("jms:queue:EndPoint2?concurrentConsumers=50")
                .rootPolicyRef("myPolicy")
                /*.to("log:Recieved From Endpoint2?groupSize=100")*/
                .log("Recieved From Endpoint2")
                .to("CommonEndpoint");

        from("CommonEndpoint")
                .delay(50)
                /*.to("log:Delayed?groupSize=100")*/
                .log("Delayed");

Below is my timer route created in a bundle which refers to base framework.

from("timer://Timer1?fixedRate=true&period=60000")
                .to("jms:queue:EndPoint1");

and

from("timer://Timer2?fixedRate=true&period=60000")
                .to("jms:queue:EndPoint2");

which continuosly sends timer message to Endpoint1 and Enpoint2 which both sends message to commonendpoint. My ThrottlingInflightRoutePolicy is defined like below.

<bean id="myPolicy" class="org.apache.camel.impl.ThrottlingInflightRoutePolicy">
    <property name="scope" value="Context"/>
    <property name="maxInflightExchanges" value="20"/>
    <property name="resumePercentOfMax" value="10"/>
    <property name="loggingLevel" value="WARN"/>
</bean>

While checking log i can simply see the log trace of timer. Im not understanding how to throttle requests while checking log. is there anything im missing here?? What should be done in my code to test throttling....?


Solution

  • I am not sure how to implement the ThrottlingInflightPolicy you have setup, but you can implement a route like this that should accomplish your goal.

    from("jms:queue:EndPoint1?concurrentConsumers=20")
        .throttle(10)
        .to("Other_Logic_Or_Routing");
    

    Notes: maxInflightExchanges can be controlled by simply lowering concurrentConsumers to 20 Throttle component can ensure that your messaging rate does not exceed a limit.

    There are lots of ways to configure the throttle for your route so please lookup what you want to configure against the documentation. My example limits the route to processing 10 messages per second. http://camel.apache.org/throttler.html