camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject [CONF] Apache Camel > Load Balancer
Date Wed, 10 Mar 2010 12:18:00 GMT
    <base href="">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="">Load
     <h4>Page <b>edited</b> by             <a href="">Claus
     <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>
<th class='confluenceTh'> Policy </th>
<th class='confluenceTh'> Description </th>
<td class='confluenceTd'> <a href=""
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>
<td class='confluenceTd'> <a href=""
rel="nofollow">Random</a> </td>
<td class='confluenceTd'> A random endpoint is selected for each exchange </td>
<td class='confluenceTd'> <a href=""
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>
<td class='confluenceTd'> <a href=""
rel="nofollow">Topic</a> </td>
<td class='confluenceTd'> Topic which sends to all destinations (rather like JMS Topics)
<td class='confluenceTd'> <a href=""
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>

<h3><a name="LoadBalancer-RoundRobin"></a>Round Robin</h3>

<p><b>Camel 1.x behavior</b><br/>
The round robin load balancer can actually be used to failover with Camel 1.x. This is no
longer possible in Camel 2.x as the underlying <a href="/confluence/display/CAMEL/Error+Handler"
title="Error Handler">Error Handler</a> foundation has been significantly overhauled
in Camel 2.x. Frankly the round robin load balancer in Camel 1.x was not thought to be used
in a failover scenario.</p>

<p><b>Camel 2.x behavior</b><br/>
The round robin load balancer is not meant to work with failover, for that you should use
the dedicated <b>failover</b> load balancer. The round robin load balancer will
only change to next endpoint per message.</p>

<p>The round robin load balancer is statefull as it keeps state which endpoint to use
next time.</p>

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

<p><b>Using the Spring configuration</b></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;camelContext id=<span class="code-quote">"camel"</span> xmlns=<span
class="code-quote">"http:<span class="code-comment">//"</span>&gt;
</span>  &lt;route&gt;
    &lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;
        &lt;to uri=<span class="code-quote">"mock:x"</span>/&gt;     
        &lt;to uri=<span class="code-quote">"mock:y"</span>/&gt;     
        &lt;to uri=<span class="code-quote">"mock:z"</span>/&gt;     

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

<p>It has the following options:</p>
<table class='confluenceTable'><tbody>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
<td class='confluenceTd'> inheritErrorHandler </td>
<td class='confluenceTd'> boolean </td>
<td class='confluenceTd'> true </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> Whether or not the <a href="/confluence/display/CAMEL/Error+Handler"
title="Error Handler">Error Handler</a> configured on the route should be used or
not. You can disable it if you want the failover to trigger immediately and failover to the
next endpoint. On the other hand if you have this option enabled, then Camel will first let
the <a href="/confluence/display/CAMEL/Error+Handler" title="Error Handler">Error Handler</a>
try to process the message. The <a href="/confluence/display/CAMEL/Error+Handler" title="Error
Handler">Error Handler</a> may have been configured to redelivery and use delays
between attempts. If you have enabled a number of redeliveries then Camel will try to redeliver
to the <b>same</b> endpoint, and only failover to the next endpoint, when the
<a href="/confluence/display/CAMEL/Error+Handler" title="Error Handler">Error Handler</a>
is exhausted. </td>
<td class='confluenceTd'> maximumFailoverAttempts </td>
<td class='confluenceTd'> int </td>
<td class='confluenceTd'> -1 </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> A value to indicate after X
failver attempts we should exhaust (give up). Use -1 to indicate newer give up and always
try to failover. Use 0 to newer failover. And use e.g. 3 to failover at most 3 times before
giving up. This option can be used whether or not round robin is enabled or not. </td>
<td class='confluenceTd'> roundRobin </td>
<td class='confluenceTd'> boolean </td>
<td class='confluenceTd'> false </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> Whether or not the <tt>failover</tt>
load balancer should operate in round robin mode or not. If not, then it will <b>always</b>
start from the first endpoint when a new message is to be processed. In other words it restart
from the top for every message. If round robin is enabled, then it keeps state and will continue
with the next endpoint in a round robin fashion. When using round robin it will not <em>stick</em>
to last known good endpoint, it will always pick the next endpoint to use. </td>

<p><b>Camel 2.2 or older behavior</b><br/>
The current implement of failover load balancer is a simple logic which <b>always</b>
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 <b>always</b>
start with the first endpoint.</p>

<p><b>Camel 2.3 onwards behavior</b><br/>
The <tt>failover</tt> load balancer now supports round robin mode, which allows
you to failover in a round robin fashion. See the <tt>roundRobin</tt> option.</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>In Camel 2.2 or older 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. In Camel 2.3 onwards this is not required as such, as you can mix and match.
See the <tt>inheritErrorHandler</tt> option.</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>);

<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

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

<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">&lt;route errorHandlerRef=<span class="code-quote">"myErrorHandler"</span>&gt;</span>
      <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:foo"</span>/&gt;</span>
      <span class="code-tag">&lt;loadBalance&gt;</span>
          <span class="code-tag">&lt;failover&gt;</span>
              <span class="code-tag">&lt;exception&gt;</span><span
              <span class="code-tag">&lt;exception&gt;</span>com.mycompany.MyOtherException<span
          <span class="code-tag">&lt;/failover&gt;</span>
          <span class="code-tag">&lt;to uri=<span class="code-quote">"direct:a"</span>/&gt;</span>
          <span class="code-tag">&lt;to uri=<span class="code-quote">"direct:b"</span>/&gt;</span>
      <span class="code-tag">&lt;/loadBalance&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>

<h4><a name="LoadBalancer-Usingfailoverinroundrobinmode"></a>Using failover
in round robin mode</h4>

<p>An example using Java DSL:</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">// Use failover load balancer in stateful round robin
</span>    <span class="code-comment">// which mean it will failover immediately
in <span class="code-keyword">case</span> of an exception
</span>    <span class="code-comment">// as it does NOT inherit error handler.
It will also keep retrying as
</span>    <span class="code-comment">// its configured to newer exhaust.
</span>    .loadBalance().failover(-1, <span class="code-keyword">false</span>,
<span class="code-keyword">true</span>).
        to(<span class="code-quote">"direct:bad"</span>, <span class="code-quote">"direct:bad2"</span>,
<span class="code-quote">"direct:good"</span>, <span class="code-quote">"direct:good2"</span>);

<p>And the same example using Spring XML:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
    <span class="code-tag">&lt;loadBalance&gt;</span>
        &lt;!-- failover using stateful round robin,
             which will keep retrying forever those 4 endpoints until success.
             You can set the maximumFailoverAttempt to break out after X attempts --&gt;
        <span class="code-tag">&lt;failover roundRobin=<span class="code-quote">"true"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"direct:bad"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"direct:bad2"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"direct:good"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"direct:good2"</span>/&gt;</span>
    <span class="code-tag">&lt;/loadBalance&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Disabled inheritErrorHandler</b><br
/><p>You can configure <tt>inheritErrorHandler=false</tt> if you want
to failover to the next endpoint as fast as possible. By disabling the <a href="/confluence/display/CAMEL/Error+Handler"
title="Error Handler">Error Handler</a> you ensure it does not <em>intervene</em>
which allows the <tt>failover</tt> load balancer to handle failover asap. By also
enabling <tt>roundRobin</tt> mode, then it will keep retrying until it success.
You can then configure the <tt>maximumFailoverAttempts</tt> option to a high value
to let it eventually exhaust (give up) and fail.</p></td></tr></table></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 id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href=""
class="grey">Change Notification Preferences</a>

       <a href="">View
       <a href="">View
       <a href=";showCommentArea=true#addcomment">Add

View raw message