Tuesday, October 26, 2010

Weighted Round-Robin and Random Load balancing further simplified in Camel

I have further simplified the weighted round-robin and random load balancing support in Camel 2.6 based on comments and feedback from Claus Ibsen (http://davsclaus.blogspot.com/).

The changes are primarily in 2 areas

1> In Camel 2.5, in the case where the distributionRatio did not match the number
     of specified load balancing processors, I had opted to use best effort load balancing
     at runtime along with log warnings. In Camel 2.6, this behavior has now been
     changed to throw an exception during route startup to indicate that the
     distributionRatio does not match the number of processors.

2> In Camel 2.5 , the distributionRatio was passed to DSL or Spring XML as a List 
     as shown in
         a> http://opensourceknowledge.blogspot.com/2010/10/added-support-for-weighted-round-robin.html
         b> Camel Documentation (http://camel.apache.org/load-balancer.html).
               I have modified this in Camel 2.6 based on Claus' recommendation and made 

               it easier for the user by changing the distribution ratio to a String that expects
               an input of integer weights separated by a delimiter. The delimiter can also be
               influenced using distributionRatioDelimiter String (the default delimiter being ",").

Given below are examples of how this simplifies the DSL for Camel 2.6

public void testRoundRobin() throws Exception {

        x.expectedMessageCount(5);
        y.expectedMessageCount(2);
        z.expectedMessageCount(1);

        context.addRoutes(new RouteBuilder() {
            public void configure() {
                
                // START SNIPPET: example
                from("direct:start").loadBalance().
                weighted(true, "4,2,1").to("mock:x", "mock:y", "mock:z");
                // END SNIPPET: example
            }
        });
        context.start();
        
        sendMessages(1, 2, 3, 4, 5, 6, 7, 8);
        
        assertMockEndpointsSatisfied();
        x.expectedBodiesReceived(1, 4, 6, 7, 8);
        y.expectedBodiesReceived(2, 5);
        z.expectedBodiesReceived(3);
    }

For Spring XML examples, please check out the following links

weightedRoundRobinLoadBalance.xml
weightedRandomLoadBalance.xml

No comments: