camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Delayer
Date Wed, 19 Oct 2011 09:35: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/Delayer">Delayer</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~fbolton">Fintan
Bolton</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Added a cross-reference to the delay interceptor page<br />
    </div>
        <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" > <br>{include:Using This Pattern}
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">
<br>h3. See Also <br> <br>* [Delay Interceptor] <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h3><a name="Delayer-Delayer"></a>Delayer</h3>

<p>The Delayer Pattern allows you to delay the delivery of messages to some destination.</p>

<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>The <a href="/confluence/display/CAMEL/Delayer"
title="Delayer">Delayer</a> in Camel 1.x works a bit differently than Camel 2.0 onwards.

<p>In Camel 1.x the expression is used to calculate an absolute time in millis.<br/>
So if you want to wait 3 sec from now and want to use the expression for that you have to
set the absolute time as <tt>currentTimeInMillis() + 3000</tt>.</p>

<p>In Camel 2.0 the expression is a value in millis to wait from the current time, so
the expression should just be <tt>3000</tt>.<br/>
However in both Camel 1.x and 2.0 you can use a long value for a fixed value to indicate the
delay in millis.<br/>
See the Spring DSL samples for Delayer in Camel 1.x vs. Camel 2.0.</p></td></tr></table></div>

<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/forbidden.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Using Delayer
in Java DSL</b><br />See this ticket: <a href="https://issues.apache.org/jira/browse/CAMEL-2654"
class="external-link" rel="nofollow">https://issues.apache.org/jira/browse/CAMEL-2654</a></td></tr></table></div>

<h3><a name="Delayer-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>asyncDelayed</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> If enabled then delayed messages
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>

<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:b"</span>).delay(1000).to(<span
class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>So the above example will delay all messages received on <b>seda:b</b>
1 second before sending them to <b>mock:result</b>. </p>

<p>You can of course use many different <a href="/confluence/display/CAMEL/Expression"
title="Expression">Expression</a> languages such as <a href="/confluence/display/CAMEL/XPath"
title="XPath">XPath</a>, <a href="/confluence/display/CAMEL/XQuery" title="XQuery">XQuery</a>,
<a href="/confluence/display/CAMEL/SQL" title="SQL">SQL</a> or various <a href="/confluence/display/CAMEL/Scripting+Languages"
title="Scripting Languages">Scripting Languages</a>.  You can just delay things a
fixed amount of time from the point at which the delayer receives the message. For example
to delay things 2 seconds</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
delayer(2000)
</pre>
</div></div>

<p>The above assume that the delivery order is maintained and that the messages are
delivered in delay order. If you want to reorder the messages based on delivery time, you
can use the <a href="/confluence/display/CAMEL/Resequencer" title="Resequencer">Resequencer</a>
with this pattern. For example</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:someQueue"</span>).resequencer(header(<span
class="code-quote">"MyDeliveryTime"</span>)).delay(<span class="code-quote">"MyRedeliveryTime"</span>).to(<span
class="code-quote">"activemq:aDelayedQueue"</span>);
</pre>
</div></div>

<h4><a name="Delayer-Camel2.0SpringDSL"></a>Camel 2.0 - Spring DSL</h4>
<p>The sample below demonstrates the delay in Spring DSL:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext xmlns=<span
class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <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;delay&gt;</span>
            <span class="code-tag">&lt;header&gt;</span>MyDelay<span
class="code-tag">&lt;/header&gt;</span>
        <span class="code-tag">&lt;/delay&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;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"seda:b"</span>/&gt;</span>
        <span class="code-tag">&lt;delay&gt;</span>
            <span class="code-tag">&lt;constant&gt;</span>1000<span
class="code-tag">&lt;/constant&gt;</span>
        <span class="code-tag">&lt;/delay&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>
</pre>
</div></div>

<h4><a name="Delayer-Camel1.xSpringDSL"></a>Camel 1.x - Spring DSL</h4>
<p>The delayer is using slightly different names in Camel 1.x:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;delayer&gt;</span>
  <span class="code-tag">&lt;delayTime&gt;</span>3000<span class="code-tag">&lt;/delayTime&gt;</span>
  <span class="code-tag">&lt;/expression&gt;</span>
<span class="code-tag">&lt;/delayer&gt;</span>
</pre>
</div></div>

<p>The empty tag <tt><b>&lt;/expression&gt;</b></tt>
is needed to fulfill the XSD validation as its an optional element and we use JAXB annotations
to generated the XSD in Camel and some combinations is hard to auto generate with optional
elements.</p>

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

<h3><a name="Delayer-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/Delayer" title="Delayer">Delayer</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>

<h4><a name="Delayer-FromJavaDSL"></a>From Java DSL</h4>
<p>You use the <tt>asyncDelayed()</tt> to enable the async behavior.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:queue:foo"</span>).delay(1000).asyncDelayed().to(<span
class="code-quote">"activemq:aDelayedQueue"</span>);
</pre>
</div></div>

<h4><a name="Delayer-FromSpringXML"></a>From Spring XML</h4>
<p>You use the <tt>asyncDelayed="true"</tt> attribute to enable the async
behavior.</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">"activemq:queue:foo"</span>/&gt;</span>
   <span class="code-tag">&lt;delay asyncDelayed=<span class="code-quote">"true"</span>&gt;</span>
       <span class="code-tag">&lt;constant&gt;</span>1000<span class="code-tag">&lt;/constant&gt;</span>
   <span class="code-tag">&lt;/delay&gt;</span>
   <span class="code-tag">&lt;to uri=<span class="code-quote">"activemq:aDealyedQueue"</span>/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<h3><a name="Delayer-Creatingacustomdelay"></a>Creating a custom delay</h3>

<p>You can use an expression to determine when to send a message using something like
this</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:foo"</span>).
  delay().method(<span class="code-quote">"someBean"</span>, <span class="code-quote">"computeDelay"</span>).
  to(<span class="code-quote">"activemq:bar"</span>);
</pre>
</div></div>

<p>then the bean would look like this...</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class SomeBean {
  <span class="code-keyword">public</span> <span class="code-object">long</span>
computeDelay() { 
     <span class="code-object">long</span> delay = 0;
     <span class="code-comment">// use java code to compute a delay value in millis
</span>     <span class="code-keyword">return</span> delay;
 }
}
</pre>
</div></div>

<h4><a name="Delayer-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>

<h3><a name="Delayer-SeeAlso"></a>See Also</h3>

<ul>
	<li><a href="/confluence/display/CAMEL/Delay+Interceptor" title="Delay Interceptor">Delay
Interceptor</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/Delayer">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=60899&revisedVersion=16&originalVersion=15">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Delayer?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message