Load BalancerThe Load Balancer Pattern allows you to delegate to one of a number of endpoints using a variety of different load balancing policies. Build in load balancing policiesCamel has out of the box the following policies:
Round RobinUsing the Fluent Builders from("direct:start").loadBalance(). roundRobin().to("mock:x", "mock:y", "mock:z"); Using the Spring configuration <bean id = "roundRobinRef" class="org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer" /> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:start"/> <loadBalance ref="roundRobinRef"> <to uri="mock:x"/> <to uri="mock:y"/> <to uri="mock:z"/> </loadBalance> </route> </camelContext> or <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:start"/> <loadBalance> <roundRobin/> <!-- This only support for Camel 1.5 --> <to uri="mock:x"/> <to uri="mock:y"/> <to uri="mock:z"/> </loadBalance> </route> </camelContext> So the above example will load balance requests from direct:start to one of the available mock endpoint instances, in this case using a round robbin policy. FailoverAvailable as of Camel 2.0 Here is a sample to failover only if a IOException related exception was thrown: from("direct:start") // here we will load balance if IOException was thrown // any other kind of exception will result in the Exchange as failed // to failover over any kind of exception we can just omit the exception // in the failOver DSL .loadBalance().failover(IOException.class) .to("direct:x", "direct:y", "direct:z"); You can specify multiple exceptions to failover as the option is varargs, for instance: loadBalance().failover(IOException.class, MyOtherException.class).to("direct:a", "direct:b"); Using failover in Spring DSLFailover can also be used from Spring DSL and you configure it as:
<loadBalance>
<failover>
<exception>java.io.IOException</exception>
<exception>com.mycompany.MyOtherException</exception>
</failover>
<to uri="direct:a"/>
<to uri="direct:b"/>
</loadBalance>
Using This PatternIf you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out. |