You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 17 Next »

Load Balancer

The 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 policies

Camel has out of the box the following policies:

Policy

Description

Round Robin

The exchanges is selected in a round robin fashion. This is a well known and classic policy. This spreads the load even.

Random

A random endpoint is selected for each exchange

Sticky

Sticky load balancing using an Expression to calculate a correlation key to perform the sticky load balancing; rather like jsessionid in the web or JMSXGroupID in JMS.

Topic

Topic which sends to all destinations (rather like JMS Topics)

Failover

Camel 2.0: In case of failures the exchange is tried on the next endpoint.

Round Robin

Using the Fluent Builders

Error formatting macro: snippet: java.lang.NullPointerException

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.
For further examples of this pattern in use you could look at the junit test case

Failover

Available as of Camel 2.0
The failover load balancer is capable of trying the next processor in case an Exchange failed with an exception during processing.
You can configure the failover with a list of specific exception to only failover. If you do not specify any exceptions it will failover over any exceptions. It uses the same strategy for matching exceptions as the Exception Clause does for the onException.

The current implement of failover load balancer is a simple logic which always tries the first endpoint, and in case of an exception being thrown it tries the next in the list, and so forth. It has no state, and the next message will thus always start with the first endpoint.

Redelivery must be enabled

The failover load balancer requires you have enabled Camel Error Handler to use redelivery. By default Camel does not do this.

Here is a sample to failover only if a IOException related exception was thrown:

Error formatting macro: snippet: java.lang.NullPointerException

You can specify multiple exceptions to failover as the option is varargs, for instance:

// enable redelivery so failover can react
errorHandler(defaultErrorHandler().maximumRedeliveries(5));

from("direct:foo").
    loadBalance().failover(IOException.class, MyOtherException.class)
        .to("direct:a", "direct:b");

Using failover in Spring DSL

Failover can also be used from Spring DSL and you configure it as:

   <route errorHandlerRef="myErrorHandler">
      <from uri="direct:foo"/>
      <loadBalance>
          <failover>
              <exception>java.io.IOException</exception>
              <exception>com.mycompany.MyOtherException</exception>
          </failover>
          <to uri="direct:a"/>
          <to uri="direct:b"/>
      </loadBalance>
    </route>

Using This Pattern

If 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.

  • No labels