camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Throttler
Date Sat, 16 Apr 2011 12:13:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Throttler">Throttler</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (4)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{div:class=confluenceTableSmall} <br>||
Name || Default Value || Description || <br></td></tr>
            <tr><td class="diff-changed-lines" >| {{maximumRequestsPerPeriod}}
| | Maximum number of requests per period to throttle. This option must be provided and a
positive number. <span class="diff-added-words"style="background-color: #dfd;">Notice,
in the XML DSL, from Camel 2.8 onwards this option is configured using an [Expression] instead
of an attribute.</span> | <br></td></tr>
            <tr><td class="diff-unchanged" >| {{timePeriodMillis}} | {{1000}}
| The time period in millis, in which the throttler will allow at most {{maximumRequestsPerPeriod}}
number of messages. | <br>| {{asyncDelayed}} | {{false}} | *Camel 2.4:* If enabled then
any messages which is delayed happens asynchronously using a scheduled thread pool. | <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>*Using the [Spring XML
Extensions]* <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h4.
Camel 2.7.x or older <br>{snippet:id=example|lang=xml|url=camel/tags/camel-2.7.0/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/throttler.xml}
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h4.
Camel 2.8 onwards <br>In Camel 2.8 onwards you must set the maximum period as an [Expression]
as shown below where we use a [Constant] expression: <br></td></tr>
            <tr><td class="diff-unchanged" >{snippet:id=example|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/throttler.xml}
<br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
Dynamically changing maximum requests per period <br>*Available os of Camel 2.8*  <br>Since
we use an [Expression] you can adjust this value at runtime, for example you can provide a
header with the value. At runtime Camel evaluates the expression and converts the result to
a {{java.lang.Long}} type. In the example below we use a header from the message to determine
the maximum requests per period. If the header is absent, then the [Throttler] uses the old
value. So that allows you to only provide a header if the value is to be changed: <br>{snippet:id=e2|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/throttler.xml}
<br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Asynchronous delaying <br>*Available
as of Camel 2.4* <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h3><a name="Throttler-Throttler"></a>Throttler</h3>

<p>The Throttler Pattern allows you to ensure that a specific endpoint does not get
overloaded, or that we don't exceed an agreed SLA with some external service.</p>

<h3><a name="Throttler-Options"></a>Options</h3>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>maximumRequestsPerPeriod</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Maximum number of requests per period to throttle. This option
must be provided and a positive number. Notice, in the XML DSL, from Camel 2.8 onwards this
option is configured using an <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>
instead of an attribute. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>timePeriodMillis</tt> </td>
<td class='confluenceTd'> <tt>1000</tt> </td>
<td class='confluenceTd'> The time period in millis, in which the throttler will allow
at most <tt>maximumRequestsPerPeriod</tt> number of messages. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>asyncDelayed</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> If enabled then any messages
which is delayed happens asynchronously using a scheduled thread pool. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>executorServiceRef</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'>  <b>Camel 2.4:</b> Refers to a custom <a href="/confluence/display/CAMEL/Threading+Model"
title="Threading Model">Thread Pool</a> to be used if <tt>asyncDelay</tt>
has been enabled. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>callerRunsWhenRejected</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> Is used if <tt>asyncDelayed</tt>
was enabled. This controls if the caller thread should execute the task if the thread pool
rejected the task. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Throttler-Examples"></a>Examples</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">"seda:a"</span>).throttle(3).timePeriodMillis(10000).to(<span
class="code-quote">"log:result"</span>, <span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>So the above example will throttle messages all messages received on <b>seda:a</b>
before being sent to <b>mock:result</b> ensuring that a maximum of 3 messages
are sent in any 10 second window. Note that typically you would often use the default time
period of a second. So to throttle requests at 100 requests per second between two endpoints
it would look more like this...</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"seda:a"</span>).throttle(100).to(<span class="code-quote">"seda:b"</span>);
</pre>
</div></div>

<p>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/ThrottlerTest.java?view=markup"
class="external-link" rel="nofollow">junit test case</a></p>

<p><b>Using the <a href="/confluence/display/CAMEL/Spring+XML+Extensions" title="Spring
XML Extensions">Spring XML Extensions</a></b></p>
<h4><a name="Throttler-Camel2.7.xorolder"></a>Camel 2.7.x or older</h4>
<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">"seda:a"</span>
/&gt;</span>
  <span class="code-tag">&lt;throttle maximumRequestsPerPeriod=<span class="code-quote">"3"</span>
timePeriodMillis=<span class="code-quote">"10000"</span>&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>
/&gt;</span>
  <span class="code-tag">&lt;/throttle&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<h4><a name="Throttler-Camel2.8onwards"></a>Camel 2.8 onwards</h4>
<p>In Camel 2.8 onwards you must set the maximum period as an <a href="/confluence/display/CAMEL/Expression"
title="Expression">Expression</a> as shown below where we use a <a href="/confluence/display/CAMEL/Constant"
title="Constant">Constant</a> expression:</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">"seda:a"</span>/&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- throttle 3 messages
per 10 sec --&gt;</span></span>
    <span class="code-tag">&lt;throttle timePeriodMillis=<span class="code-quote">"10000"</span>&gt;</span>
        <span class="code-tag">&lt;constant&gt;</span>3<span class="code-tag">&lt;/constant&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/throttle&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<h3><a name="Throttler-Dynamicallychangingmaximumrequestsperperiod"></a>Dynamically
changing maximum requests per period</h3>
<p><b>Available os of Camel 2.8</b> <br/>
Since we use an <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>
you can adjust this value at runtime, for example you can provide a header with the value.
At runtime Camel evaluates the expression and converts the result to a <tt>java.lang.Long</tt>
type. In the example below we use a header from the message to determine the maximum requests
per period. If the header is absent, then the <a href="/confluence/display/CAMEL/Throttler"
title="Throttler">Throttler</a> uses the old value. So that allows you to only provide
a header if the value is to be changed:</p>
<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException:
Index: 20, Size: 20</span> </div>

<h3><a name="Throttler-Asynchronousdelaying"></a>Asynchronous delaying</h3>
<p><b>Available as of Camel 2.4</b></p>

<p>You can let the <a href="/confluence/display/CAMEL/Throttler" title="Throttler">Throttler</a>
use non blocking asynchronous delaying, which means Camel will use a scheduler to schedule
a task to be executed in the future. The task will then continue routing. This allows the
caller thread to not block and be able to service other messages etc.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"seda:a"</span>).throttle(100).asyncDelayed().to(<span
class="code-quote">"seda:b"</span>);
</pre>
</div></div>

<h4><a name="Throttler-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="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Throttler">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=61229&revisedVersion=10&originalVersion=9">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Throttler?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message