camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Route Throttling Example
Date Tue, 03 Nov 2009 09:19:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;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/Route+Throttling+Example">Route
Throttling Example</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">
         <h2><a name="RouteThrottlingExample-RouteThrottlingExample"></a>Route
Throttling Example</h2>
<p><b>Available as of Camel 2.1</b></p>

<h3><a name="RouteThrottlingExample-About"></a>About</h3>
<p>This example shows how to use the new feature <a href="/confluence/display/CAMEL/RoutePolicy"
title="RoutePolicy">RoutePolicy</a> to dynamically at runtime to throttle routes
based on metrics gathered by the current number of inflight exchanges. </p>

<p>What it means is that Camel will dynamic throttle the routes based on the flow of
messages being processed at runtime.</p>

<p>The example have 3 routes where as two of the routes are input routes, and the last
is the processing route<br/>
1. route1: from("jms") - input from a JMS queue<br/>
2. route2: from("file") - input from a file folder<br/>
3. route3: from("seda") - processing of the input messages, this one simulates CPU work by
delaying the messages 100 mills.</p>

<h3><a name="RouteThrottlingExample-Howitworks"></a>How it works</h3>
<p>When the example runs we have a dependency from the routes as follows:</p>
<ul>
	<li>route1 -&gt; route3</li>
	<li>route2 -&gt; route3</li>
</ul>


<p>What the example demonstrates is that Camel is capable of dynamic throttling route1
and route2 based on the total flow of messages. At runtime Camel gathers the current inflight
exchanges in a registry which is used for metrics.</p>

<p>So when the flow is going <em>too fast</em> then the <a href="/confluence/display/CAMEL/RoutePolicy"
title="RoutePolicy">RoutePolicy</a> kicks in and <tt>suspends</tt> route1
and/or route2. The current inflight exchanges will continue to be processed and when we are
below a <em>threshold</em> then the <a href="/confluence/display/CAMEL/RoutePolicy"
title="RoutePolicy">RoutePolicy</a> kicks in again and <tt>resumes</tt>
route1 and/or route2.</p>

<p>Camel provides the throttling policy in the <tt>org.apache.camel.impl.ThrottlingInflightRoutePolicy</tt>.</p>

<h3><a name="RouteThrottlingExample-Howtorun"></a>How to run</h3>
<p>The example has 3 maven goals to run the example</p>

<p><tt>mvn compile exec:java -PCamelServer</tt> - starts the Camel Server
which contains the 3 routes and where you should check its log output for how it goes.</p>

<p><tt>mvn compile exec:java -PCamelClient</tt> - is a client that sends
10000 JMS messages to the JMS broker which is consumed by route1. The Server must be started
beforehand.</p>

<p><tt>mvn compile exec:java -PCamelFileClient</tt> - is a client that creates
5000 files that are consumed by route2. The server may be started beforehand, but its not
required.</p>

<p>So at first you start the server. Then at any time you can run a client at will.
For example you can run the JMS client and let it run to completion at the server. You can
see at the server console logging that it reports the progress. And at sometime it will reach
10000 messages processed. You can then start the client again if you like.</p>

<p>You can also start the other client to create the files which then let the example
be a bit more complicated as we have concurrent processing of JMS messages and files at the
same time. And where as both of these should be dynamic throttled so we wont go <em>too
fast</em>.</p>

<h3><a name="RouteThrottlingExample-Howtochange"></a>How to change</h3>
<p>You can check the file <tt>src/main/resources/META-INF/spring/camel-server.xml</tt>
file where you can see the configuration of the dynamic throttler. By default its configured
as:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;bean id=<span class="code-quote">"myPolicy"</span>
class=<span class="code-quote">"org.apache.camel.impl.ThrottlingInflightRoutePolicy"</span>&gt;</span>
    &lt;!-- define the scope to be context scoped so we measure against total inflight
exchanges
         that means for both route1, route2 and route3 all together --&gt;
    <span class="code-tag">&lt;property name=<span class="code-quote">"scope"</span>
value=<span class="code-quote">"Context"</span>/&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- when we hit
&gt;</span> 30 inflight exchanges then kick in and suspend the routes --&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"maxInflightExchanges"</span>
value=<span class="code-quote">"30"</span>/&gt;</span>
    &lt;!-- when we hit lower than 10% of the max = 3 then kick in and resume the routes
         the default percentage is 70% but in this demo we want a low value --&gt;
    <span class="code-tag">&lt;property name=<span class="code-quote">"resumePercentOfMax"</span>
value=<span class="code-quote">"10"</span>/&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- output throttling
activity at WARN level --&gt;</span></span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"loggingLevel"</span>
value=<span class="code-quote">"WARN"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>You can then change this and restart the server to see the changes in effect.</p>

<h3><a name="RouteThrottlingExample-JMXmanagement"></a>JMX management</h3>
<p>At runtime you can manage the <tt>ThrottlingInflightRoutePolicy</tt>
at runtime as its listed under services in the JConsole.<br/>
For example you can change the option <tt>maxInflightExchanges</tt> while its
running to find a more suitable value.</p>

<p>The screenshot below illustrates it from a JConsole.<br/>
<img src="/confluence/download/attachments/5604305/throttling services.png" align="absmiddle"
border="0" /></p>

<p>See more at <a href="/confluence/display/CAMEL/Camel+JMX" title="Camel JMX">using
JMX with Camel</a>.</p>

<h3><a name="RouteThrottlingExample-Sampleoutputfromrunningexample"></a>Sample
output from running example</h3>

<p>When running the example you should see a lot of activity logged to the console.
<br/>
Below is a snippet when the example runs to an end when the 10000 messages have been processed.
What you should notice is that the throttling ensures that the JMS consumer is not taking
in more messages than we can process. That means that ++<ins>JMS</ins>++ and ++<ins>SEDA</ins>++
are completing at nearly the same time.  </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
[ultMessageListenerContainer-42] +++JMS +++                     INFO  Received: 9800 messages
so far. Last group took: 673 millis which is: 148.588 messages per second. average: 159.734
[currentConsumers=25&amp;size=25000] ThrottlingInflightRoutePolicy  WARN  Throttling consumer:
25 &gt; 20 inflight exchange by suspending consumer.
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 9800
messages so far. Last group took: 733 millis which is: 136.426 messages per second. average:
159.789
[currentConsumers=25&amp;size=25000] ThrottlingInflightRoutePolicy  WARN  Throttling consumer:
2 &lt;= 2 inflight exchange by resuming consumer.
[currentConsumers=25&amp;size=25000] ThrottlingInflightRoutePolicy  WARN  Throttling consumer:
25 &gt; 20 inflight exchange by suspending consumer.
[currentConsumers=25&amp;size=25000] ThrottlingInflightRoutePolicy  WARN  Throttling consumer:
2 &lt;= 2 inflight exchange by resuming consumer.
[currentConsumers=25&amp;size=25000] ThrottlingInflightRoutePolicy  WARN  Throttling consumer:
25 &gt; 20 inflight exchange by suspending consumer.
[currentConsumers=25&amp;size=25000] ThrottlingInflightRoutePolicy  WARN  Throttling consumer:
2 &lt;= 2 inflight exchange by resuming consumer.
[ultMessageListenerContainer-55] +++JMS +++                     INFO  Received: 9900 messages
so far. Last group took: 758 millis which is: 131.926 messages per second. average: 159.395
[currentConsumers=25&amp;size=25000] ThrottlingInflightRoutePolicy  WARN  Throttling consumer:
25 &gt; 20 inflight exchange by suspending consumer.
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 9900
messages so far. Last group took: 598 millis which is: 167.224 messages per second. average:
159.86
[currentConsumers=25&amp;size=25000] ThrottlingInflightRoutePolicy  WARN  Throttling consumer:
2 &lt;= 2 inflight exchange by resuming consumer.
[currentConsumers=25&amp;size=25000] ThrottlingInflightRoutePolicy  WARN  Throttling consumer:
24 &gt; 20 inflight exchange by suspending consumer.
[currentConsumers=25&amp;size=25000] ThrottlingInflightRoutePolicy  WARN  Throttling consumer:
2 &lt;= 2 inflight exchange by resuming consumer.
[currentConsumers=25&amp;size=25000] ThrottlingInflightRoutePolicy  WARN  Throttling consumer:
25 &gt; 20 inflight exchange by suspending consumer.
[currentConsumers=25&amp;size=25000] ThrottlingInflightRoutePolicy  WARN  Throttling consumer:
2 &lt;= 2 inflight exchange by resuming consumer.
[ultMessageListenerContainer-76] +++JMS +++                     INFO  Received: 10000 messages
so far. Last group took: 732 millis which is: 136.612 messages per second. average: 159.129
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 10000
messages so far. Last group took: 732 millis which is: 136.612 messages per second. average:
159.589
</pre>
</div></div>

<p>However if you run the example <b>without</b> the throttling you will
notice that the JMS consumer runs faster than we can process the messages. Towards the end
we have more than 2000 messages pending on the internal SEDA queue, when the JMS consumer
finishes.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
[ultMessageListenerContainer-41] +++JMS +++                     INFO  Received: 9800 messages
so far. Last group took: 304 millis which is: 328.947 messages per second. average: 225.272
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 7800
messages so far. Last group took: 645 millis which is: 155.039 messages per second. average:
178.461
[ultMessageListenerContainer-65] +++JMS +++                     INFO  Received: 9900 messages
so far. Last group took: 543 millis which is: 184.162 messages per second. average: 224.765
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 7900
messages so far. Last group took: 438 millis which is: 228.311 messages per second. average:
178.956
[ultMessageListenerContainer-65] +++JMS +++                     INFO  Received: 10000 messages
so far. Last group took: 395 millis which is: 253.165 messages per second. average: 225.017
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 8000
messages so far. Last group took: 408 millis which is: 245.098 messages per second. average:
179.561
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 8100
messages so far. Last group took: 410 millis which is: 243.902 messages per second. average:
180.148
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 8200
messages so far. Last group took: 405 millis which is: 246.914 messages per second. average:
180.744
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 8300
messages so far. Last group took: 404 millis which is: 247.525 messages per second. average:
181.334
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 8400
messages so far. Last group took: 404 millis which is: 247.525 messages per second. average:
181.913
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 8500
messages so far. Last group took: 666 millis which is: 150.15 messages per second. average:
181.461
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 8600
messages so far. Last group took: 405 millis which is: 246.914 messages per second. average:
182.022
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 8700
messages so far. Last group took: 404 millis which is: 247.525 messages per second. average:
182.577
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 8800
messages so far. Last group took: 407 millis which is: 245.7 messages per second. average:
183.112
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 8900
messages so far. Last group took: 404 millis which is: 247.525 messages per second. average:
183.649
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 9000
messages so far. Last group took: 404 millis which is: 247.525 messages per second. average:
184.177
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 9100
messages so far. Last group took: 405 millis which is: 246.914 messages per second. average:
184.693
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 9200
messages so far. Last group took: 405 millis which is: 246.914 messages per second. average:
185.2
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 9300
messages so far. Last group took: 404 millis which is: 247.525 messages per second. average:
185.703
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 9400
messages so far. Last group took: 405 millis which is: 246.914 messages per second. average:
186.194
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 9500
messages so far. Last group took: 405 millis which is: 246.914 messages per second. average:
186.677
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 9600
messages so far. Last group took: 408 millis which is: 245.098 messages per second. average:
187.142
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 9700
messages so far. Last group took: 413 millis which is: 242.131 messages per second. average:
187.581
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 9800
messages so far. Last group took: 410 millis which is: 243.902 messages per second. average:
188.024
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 9900
messages so far. Last group took: 579 millis which is: 172.712 messages per second. average:
187.856
[currentConsumers=25&amp;size=25000] +++SEDA+++                     INFO  Received: 10000
messages so far. Last group took: 404 millis which is: 247.525 messages per second. average:
188.31
</pre>
</div></div>

<p>So by using the <tt>ThrottlingInflightRoutePolicy</tt> we can throttle
the intake of messages to be on a pair with the speed we can process messages. And since its
pluggable you can implement your own custom logic to throttle according to your needs.</p>

<h3><a name="RouteThrottlingExample-SeeAlso"></a>See Also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a></li>
	<li><a href="/confluence/display/CAMEL/RoutePolicy" title="RoutePolicy">RoutePolicy</a></li>
</ul>

     </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/Route+Throttling+Example">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=5604305&revisedVersion=5&originalVersion=4">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Route+Throttling+Example?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message