<html>
<head>
<base href="http://cwiki.apache.org/confluence">
<link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&forWysiwyg=true"
type="text/css">
</head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
<h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/Load+Balancer">Load
Balancer</a></h2>
<h4>Page <b>edited</b> by <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
</h4>
<br/>
<div class="notificationGreySide">
<h3><a name="LoadBalancer-LoadBalancer"></a>Load Balancer</h3>
<p>The Load Balancer Pattern allows you to delegate to one of a number of endpoints
using a variety of different load balancing policies.</p>
<h3><a name="LoadBalancer-Buildinloadbalancingpolicies"></a>Build in load
balancing policies</h3>
<p>Camel has out of the box the following policies:</p>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Policy </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/processor/loadbalancer/RoundRobinLoadBalancer.html"
rel="nofollow">Round Robin</a> </td>
<td class='confluenceTd'> The exchanges is selected in a round robin fashion. This is
a well known and classic policy. This spreads the load even. </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/processor/loadbalancer/RandomLoadBalancer.html"
rel="nofollow">Random</a> </td>
<td class='confluenceTd'> A random endpoint is selected for each exchange </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/processor/loadbalancer/StickyLoadBalancer.html"
rel="nofollow">Sticky</a> </td>
<td class='confluenceTd'> 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. </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/processor/loadbalancer/TopicLoadBalancer.html"
rel="nofollow">Topic</a> </td>
<td class='confluenceTd'> Topic which sends to all destinations (rather like JMS Topics)
</td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/processor/loadbalancer/FailOverLoadBalancer.html"
rel="nofollow">Failover</a> </td>
<td class='confluenceTd'> <b>Camel 2.0:</b> In case of failures the exchange
is tried on the next endpoint. </td>
</tr>
</tbody></table>
<h3><a name="LoadBalancer-RoundRobin"></a>Round Robin</h3>
<p><b>Using the <a href="/confluence/display/CAMEL/Fluent+Builders" title="Fluent
Builders">Fluent Builders</a></b></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>).loadBalance().
roundRobin().to(<span class="code-quote">"mock:x"</span>, <span class="code-quote">"mock:y"</span>,
<span class="code-quote">"mock:z"</span>);
</pre>
</div></div>
<p><b>Using the Spring configuration</b></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<bean id = <span class="code-quote">"roundRobinRef"</span> class=<span
class="code-quote">"org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer"</span>
/>
<camelContext id=<span class="code-quote">"camel"</span> xmlns=<span
class="code-quote">"http:<span class="code-comment">//camel.apache.org/schema/spring"</span>>
</span> <route>
<from uri=<span class="code-quote">"direct:start"</span>/>
<loadBalance ref=<span class="code-quote">"roundRobinRef"</span>>
<to uri=<span class="code-quote">"mock:x"</span>/>
<to uri=<span class="code-quote">"mock:y"</span>/>
<to uri=<span class="code-quote">"mock:z"</span>/>
</loadBalance>
</route>
</camelContext>
</pre>
</div></div>
<p>or</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<camelContext id=<span class="code-quote">"camel"</span> xmlns=<span
class="code-quote">"http:<span class="code-comment">//camel.apache.org/schema/spring"</span>>
</span> <route>
<from uri=<span class="code-quote">"direct:start"</span>/>
<loadBalance>
<roundRobin/> <!-- This only support <span class="code-keyword">for</span>
Camel 1.5 -->
<to uri=<span class="code-quote">"mock:x"</span>/>
<to uri=<span class="code-quote">"mock:y"</span>/>
<to uri=<span class="code-quote">"mock:z"</span>/>
</loadBalance>
</route>
</camelContext>
</pre>
</div></div>
<p>So the above example will load balance requests from <b>direct:start</b>
to one of the available <b>mock endpoint</b> instances, in this case using a round
robbin policy.<br/>
For further examples of this pattern in use you could look at the <a href="http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoadBalanceTest.java?view=markup"
rel="nofollow">junit test case</a></p>
<h3><a name="LoadBalancer-Failover"></a>Failover</h3>
<p><b>Available as of Camel 2.0</b><br/>
The <tt>failover</tt> load balancer is capable of trying the next processor in
case an <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
failed with an <tt>exception</tt> during processing.<br/>
You can configure the <tt>failover</tt> 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 <a href="/confluence/display/CAMEL/Exception+Clause"
title="Exception Clause">Exception Clause</a> does for the <b>onException</b>.</p>
<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Redelivery must be enabled</b><br
/><p>The failover load balancer requires you have enabled Camel <a href="/confluence/display/CAMEL/Error+Handler"
title="Error Handler">Error Handler</a> to use redelivery. By default Camel does
<b>not</b> do this.</p></td></tr></table></div>
<p>Here is a sample to failover only if a <tt>IOException</tt> related exception
was thrown:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
<span class="code-comment">// here we will load balance <span class="code-keyword">if</span>
IOException was thrown
</span> <span class="code-comment">// any other kind of exception will result
in the Exchange as failed
</span> <span class="code-comment">// to failover over any kind of exception
we can just omit the exception
</span> <span class="code-comment">// in the failOver DSL
</span> .loadBalance().failover(IOException.class)
.to(<span class="code-quote">"direct:x"</span>, <span class="code-quote">"direct:y"</span>,
<span class="code-quote">"direct:z"</span>);
</pre>
</div></div>
<p>You can specify multiple exceptions to failover as the option is varargs, for instance:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// enable redelivery so failover can react
</span>errorHandler(defaultErrorHandler().maximumRedeliveries(5));
from(<span class="code-quote">"direct:foo"</span>).
loadBalance().failover(IOException.class, MyOtherException.class)
.to(<span class="code-quote">"direct:a"</span>, <span class="code-quote">"direct:b"</span>);
</pre>
</div></div>
<h4><a name="LoadBalancer-UsingfailoverinSpringDSL"></a>Using failover in
Spring DSL</h4>
<p>Failover can also be used from Spring DSL and you configure it as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag"><route errorHandlerRef=<span class="code-quote">"myErrorHandler"</span>></span>
<span class="code-tag"><from uri=<span class="code-quote">"direct:foo"</span>/></span>
<span class="code-tag"><loadBalance></span>
<span class="code-tag"><failover></span>
<span class="code-tag"><exception></span>java.io.IOException<span
class="code-tag"></exception></span>
<span class="code-tag"><exception></span>com.mycompany.MyOtherException<span
class="code-tag"></exception></span>
<span class="code-tag"></failover></span>
<span class="code-tag"><to uri=<span class="code-quote">"direct:a"</span>/></span>
<span class="code-tag"><to uri=<span class="code-quote">"direct:b"</span>/></span>
<span class="code-tag"></loadBalance></span>
<span class="code-tag"></route></span>
</pre>
</div></div>
<h4><a name="LoadBalancer-UsingThisPattern"></a>Using This Pattern</h4>
<p>If you would like to use this EIP Pattern then please read the <a href="/confluence/display/CAMEL/Getting+Started"
title="Getting Started">Getting Started</a>, you may also find the <a href="/confluence/display/CAMEL/Architecture"
title="Architecture">Architecture</a> useful particularly the description of <a
href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a> and <a
href="/confluence/display/CAMEL/URIs" title="URIs">URIs</a>. Then you could try out
some of the <a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a>
first before trying this pattern out.</p>
</div>
<div id="commentsSection" class="wiki-content pageSection">
<div style="float: right;">
<a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
</div>
<a href="http://cwiki.apache.org/confluence/display/CAMEL/Load+Balancer">View
Online</a>
|
<a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=81397&revisedVersion=16&originalVersion=15">View
Change</a>
|
<a href="http://cwiki.apache.org/confluence/display/CAMEL/Load+Balancer?showComments=true&showCommentArea=true#addcomment">Add
Comment</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
|