Versions Compared

Key

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

...

Policy

Description

Round Robin

The exchanges are selected from in a round robin fashion. This is a well known and classic policy, which spreads the load evenly.

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

In case of failures the exchange will be tried on the next endpoint.

Weighted Round-Robin

Camel 2.5: The weighted load balancing policy allows you to specify a processing load distribution ratio for each server with respect to the others. In addition to the weight, endpoint selection is then further refined using round-robin distribution based on weight.

Weighted Random

Camel 2.5: The weighted load balancing policy allows you to specify a processing load distribution ratio for each server with respect to others.In addition to the weight, endpoint selection is then further refined using random distribution based on weight.

Custom

Camel 2.8: From Camel 2.8 onwards the preferred way of using a custom Load Balancer is to use this policy, instead of using the @deprecated ref attribute.

Circuit Breaker

Camel 2.14: Implements the Circuit Breaker pattern as described in "Release it!" book.

Tip
titleLoad balancing HTTP endpoints

If you are proxying and load balancing HTTP, then see this page for more details.

...

Using the Spring configuration

Code Block
languagexml

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <loadBalance>        
        <roundRobin/>
        <to uri="mock:x"/>        
        <to uri="mock:y"/>       
        <to uri="mock:z"/>                 
    </loadBalance>
  </route>
</camelContext>

...

Info
titleEnable stream caching if using streams

If you use streaming then you should enable Stream Cachingcaching when using the failover load balancer. This is needed so the stream can be re-read after failing over to the next processor.

...

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

Code Block
java
java

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

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

...

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

Code Block
xml
xml

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

...

An example using Java DSL:

Code Block
java
java

ArrayList<integer> distributionRatio = new ArrayList<integer>();
distributionRatio.add(4);
distributionRatio.add(2);
distributionRatio.add(1);

// round-robin
from("direct:start")
    .loadBalance().weighted(true, distributionRatio)
    .to("mock:x", "mock:y", "mock:z");

//random
from("direct:start")
    .loadBalance().weighted(false, distributionRatio)
    .to("mock:x", "mock:y", "mock:z");

And the same example using Spring XML:

Code Block
xml
xml

    <route>
      <from uri="direct:start"/>
      <loadBalance>
        <weighted roundRobin="false" distributionRatio="4 2 1"/>
          <to uri="mock:x"/>
          <to uri="mock:y"/>
          <to uri="mock:z"/>
      </loadBalance>
    </route>

...

An example using Java DSL:

Code Block
java
java

// round-robin
from("direct:start")
    .loadBalance().weighted(true, "4:2:1" distributionRatioDelimiter=":")
    .to("mock:x", "mock:y", "mock:z");

//random
from("direct:start")
    .loadBalance().weighted(false, "4,2,1")
    .to("mock:x", "mock:y", "mock:z");

And the same example using Spring XML:

Code Block
xml
xml

    <route>
      <from uri="direct:start"/>
      <loadBalance>
        <weighted roundRobin="false" distributionRatio="4-2-1" distributionRatioDelimiter="-" />
          <to uri="mock:x"/>
          <to uri="mock:y"/>
          <to uri="mock:z"/>
      </loadBalance>
    </route>

...

Notice in the XML DSL above we use <custom> which is only available in Camel 2.8 onwards. In older releases you would have to do as follows instead:

Code Block
xml
xml

      <loadBalance ref="myBalancer">
        <!-- these are the endpoints to balancer -->
        <to uri="mock:x"/>
        <to uri="mock:y"/>
        <to uri="mock:z"/>
      </loadBalance>

...

Wiki Markup
{snippet:id=e2|title=Custom load balancer implementation|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CustomLoadBalanceTest.java}

Circuit Breaker

The Circuit Breaker load balancer is a stateful pattern that monitors all calls for certain exceptions. Initially the Circuit Breaker is in closed state and passes all messages. If the are failures and the threshold is reached, it moves to open state and rejects all calls until halfOpenAfter timeout is reached. After this timeout is reached, if there is a new call, it will be passed and if the result is success the Circuit Breaker will move closed state, or to open state if there was an error.

An example using Java DSL:

Code Block
java
java
from("direct:start").loadBalance()
	.circuitBreaker(2, 1000L, MyExceptionProcessor.class)
    .to("mock:result");

And the same example using Spring XML:

Code Block
languagexml
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <loadBalance>        
        <circuitBreaker threshold="2" halfOpenAfter="1000L"/>
        <exception>MyExceptionProcessor</exception>
		<to uri="mock:result"/>                       
    </loadBalance>
  </route>
</camelContext>

The above example loads balance requests from direct:start to one of the available mock endpoint instances, in this case using a round robin policy.
For further examples of this pattern look at this junit test case

Include Page
Using This Pattern
Using This Pattern