camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > RoutePolicy
Date Thu, 08 Dec 2011 09:16: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/RoutePolicy">RoutePolicy</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (1)</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" >There are these callbacks invoked
<br>- {{onInit}} *Camel 2.3* <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">-
{{onRemove}} *Camel 2.9* <br>- {{onStart}} *Camel 2.9* <br>- {{onStop}} *Camel
2.9* <br>- {{onSuspend}} *Camel 2.9* <br>- {{onResume}} *Camel 2.9* <br></td></tr>
            <tr><td class="diff-unchanged" >- {{onExchangeBegin}} <br>-
{{onExchangeDone}} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="RoutePolicy-RoutePolicy"></a>RoutePolicy</h2>
<p><b>Available as of Camel 2.1</b></p>

<p>A route policy <tt>org.apache.camel.spi.RoutePolicy</tt> is used to control
route(s) at runtime. For example you can use it to determine whether a route should be running
or not. However the policies can support any kind of use cases.</p>

<h3><a name="RoutePolicy-Howitworks"></a>How it works</h3>
<p>You associate a route with a given <tt>RoutePolicy</tt> and then during
runtime Camel will invoke callbacks on this policy where you can implement your custom logic.</p>

<p>Camel provides a support class that is a good base class to extend <tt>org.apache.camel.impl.RoutePolicySupport</tt>.
</p>

<p>There are these callbacks invoked</p>
<ul class="alternate" type="square">
	<li><tt>onInit</tt> <b>Camel 2.3</b></li>
	<li><tt>onRemove</tt> <b>Camel 2.9</b></li>
	<li><tt>onStart</tt> <b>Camel 2.9</b></li>
	<li><tt>onStop</tt> <b>Camel 2.9</b></li>
	<li><tt>onSuspend</tt> <b>Camel 2.9</b></li>
	<li><tt>onResume</tt> <b>Camel 2.9</b></li>
	<li><tt>onExchangeBegin</tt></li>
	<li><tt>onExchangeDone</tt></li>
</ul>


<p>See the javadoc of the <tt>org.apache.camel.spi.RoutePolicy</tt> for
more details.<br/>
And also the implementation of the <tt>org.apache.camel.impl.ThrottlingInflightRoutePolicy</tt>
for a concrete example.</p>

<p>Camel provides the following policies out of the box:</p>
<ul class="alternate" type="square">
	<li><tt>org.apache.camel.impl.ThrottlingInflightRoutePolicy</tt> - a throttling
based policy that automatic suspends/resumes route(s) based on metrics from the current in
flight exchanges. You can use this to dynamic throttle e.g. a <a href="/confluence/display/CAMEL/JMS"
title="JMS">JMS</a> consumer to avoid it consuming too fast.</li>
</ul>


<p>As of <b>Camel 2.5</b>, Camel also provides an ability to schedule routes
to be activated, de-activated, suspended and/or resumed at certain times during the day using
a <a href="/confluence/display/CAMEL/ScheduledRoutePolicy" title="ScheduledRoutePolicy">ScheduledRoutePolicy</a>
(offered via the <a href="http://camel.apache.org/quartz.html" class="external-link" rel="nofollow">camel-quartz</a>
component). </p>


<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>SuspendableService</b><br
/>If you want to dynamic suspend/resume routes as the <tt>org.apache.camel.impl.ThrottlingRoutePolicy</tt>
does then its advised to use <tt>org.apache.camel.SuspendableService</tt> as it
allows for fine grained <tt>suspend</tt> and <tt>resume</tt> operations.
And use the <tt>org.apache.camel.util.ServiceHelper</tt> to aid when invoking
these operations as it support fallback for regular <tt>org.apache.camel.Service</tt>
instances.</td></tr></table></div>

<h4><a name="RoutePolicy-ThrottlingInflightRoutePolicy"></a>ThrottlingInflightRoutePolicy</h4>
<p>The ThrottlingInflightRoutePolicy is triggered when an <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> is complete, which means that it requires at least
one <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
to be complete before it <em>works</em>. </p>

<p>The throttling inflight route policy has the following options:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> scope </td>
<td class='confluenceTd'> Route </td>
<td class='confluenceTd'> A scope for either <tt>Route</tt> or <tt>Context</tt>
which defines if the current number of inflight exchanges is context based or for that particular
route. </td>
</tr>
<tr>
<td class='confluenceTd'> maxInflightExchanges </td>
<td class='confluenceTd'> 1000 </td>
<td class='confluenceTd'> The maximum threshold when the throttling will start to suspend
the route if the current number of inlfight exchanges is higher than this value. </td>
</tr>
<tr>
<td class='confluenceTd'> resumePercentOfMax </td>
<td class='confluenceTd'> 70 </td>
<td class='confluenceTd'> A percentage 0..100 which defines when the throttling should
resume again in case it has been suspended. </td>
</tr>
<tr>
<td class='confluenceTd'> loggingLevel </td>
<td class='confluenceTd'> INFO </td>
<td class='confluenceTd'> The logging level used for logging the throttling activity.
</td>
</tr>
<tr>
<td class='confluenceTd'> logger </td>
<td class='confluenceTd'> ThrottlingInflightRoutePolicy </td>
<td class='confluenceTd'> The logger category. </td>
</tr>
</tbody></table>
</div>


<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>ThrottlingInflightRoutePolicy
compared to the <a href="/confluence/display/CAMEL/Throttler" title="Throttler">Throttler</a>
EIP</b><br />The <tt>ThrottlingInflightRoutePolicy</tt> compared to
<a href="/confluence/display/CAMEL/Throttler" title="Throttler">Throttler</a>
is that it does <b>not</b> block during throttling. It does throttling that is
approximate based, meaning that its more coarse grained and not explicit precise as the <a
href="/confluence/display/CAMEL/Throttler" title="Throttler">Throttler</a>. The <a
href="/confluence/display/CAMEL/Throttler" title="Throttler">Throttler</a> can be
much more accurate and only allow a specific number of messages being passed per a given time
unit. Also the <tt>ThrottlingInflightRoutePolicy</tt> is based its metrics on
number of inflight exchanges where as <a href="/confluence/display/CAMEL/Throttler" title="Throttler">Throttler</a>
is based on number of messages per time unit.</td></tr></table></div>

<h4><a name="RoutePolicy-ScheduledRoutePolicy%28SimpleandCronbased%29usingcamelQuartz"></a>ScheduledRoutePolicy
(Simple and Cron based) using camel Quartz</h4>

<p>For more details check out the following links</p>

<h3><a name="RoutePolicy-Configuringpolicy"></a>Configuring policy</h3>

<p>You configure the route policy as follows from Java DSL, using the <tt>routePolicy</tt>
method:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  RoutePolicy myPolicy = <span class="code-keyword">new</span> MyRoutePolicy();
  from(<span class="code-quote">"seda:foo"</span>).routePolicy(myPolicy).to(<span
class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>In Spring XML its a bit different as follows using the <tt>routePolicyRef</tt>
attribute:</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">"com.mycompany.MyRoutePolicy"</span>/&gt;</span>
   
   <span class="code-tag">&lt;route routePolicyRef=<span class="code-quote">"myPolicy"</span>&gt;</span>
       <span class="code-tag">&lt;from uri=<span class="code-quote">"seda:foo"</span>/&gt;</span>
       <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
   <span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<h3><a name="RoutePolicy-Configuringpolicysets"></a>Configuring policy sets</h3>
<p><b>Available as of Camel 2.7</b></p>

<p>RoutePolicy has been further improved to allow addition of policy sets or a collection
of policies that are concurrently applied on a route. The addition of policies is done as
follows.</p>

<p>In the example below, the route testRoute has a startPolicy and throttlePolicy applied
concurrently. Both policies are applied as necessary on the route.</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">"date"</span>
class=<span class="code-quote">"org.apache.camel.routepolicy.quartz.SimpleDate"</span>/&gt;</span>

    <span class="code-tag">&lt;bean id=<span class="code-quote">"startPolicy"</span>
class=<span class="code-quote">"org.apache.camel.routepolicy.quartz.SimpleScheduledRoutePolicy"</span>&gt;</span>
    	<span class="code-tag">&lt;property name=<span class="code-quote">"routeStartDate"</span>
ref=<span class="code-quote">"date"</span>/&gt;</span>
    	<span class="code-tag">&lt;property name=<span class="code-quote">"routeStartRepeatCount"</span>
value=<span class="code-quote">"1"</span>/&gt;</span>
    	<span class="code-tag">&lt;property name=<span class="code-quote">"routeStartRepeatInterval"</span>
value=<span class="code-quote">"3000"</span>/&gt;</span>    	
    <span class="code-tag">&lt;/bean&gt;</span>
    
    <span class="code-tag">&lt;bean id=<span class="code-quote">"throttlePolicy"</span>
class=<span class="code-quote">"org.apache.camel.impl.ThrottlingInflightRoutePolicy"</span>&gt;</span>
    	<span class="code-tag">&lt;property name=<span class="code-quote">"maxInflightExchanges"</span>
value=<span class="code-quote">"10"</span>/&gt;</span>    	
	<span class="code-tag">&lt;/bean&gt;</span>
	 	
    <span class="code-tag">&lt;camelContext id=<span class="code-quote">"testRouteContext"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
        <span class="code-tag">&lt;route id=<span class="code-quote">"testRoute"</span>
autoStartup=<span class="code-quote">"false"</span> routePolicyRef=<span class="code-quote">"startPolicy,
throttlePolicy"</span>&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"seda:foo?concurrentConsumers=20"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>
    <span class="code-tag">&lt;/camelContext&gt;</span>
   <span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<h3><a name="RoutePolicy-SeeAlso"></a>See Also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Route+Throttling+Example" title="Route Throttling
Example">Route Throttling Example</a> for an example using this in practice with
the ThrottlingInflightRoutePolicy</li>
	<li><a href="/confluence/display/CAMEL/ScheduledRoutePolicy" title="ScheduledRoutePolicy">ScheduledRoutePolicy</a>
for information on policy based scheduling capability for camel routes</li>
	<li><a href="/confluence/display/CAMEL/Architecture" title="Architecture">Architecture</a></li>
</ul>

    </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/RoutePolicy">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=5604260&revisedVersion=17&originalVersion=16">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/RoutePolicy?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message