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 Fri, 07 Sep 2012 20:32: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/~muellerc">Christian
Mueller</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Removed the deprecated Camel 1.x documentations<br />
    </div>
        <br/>
                         <h4>Changes (8)</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" >The Delayer Pattern allows you to
delay the delivery of messages to some destination. <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">{info:Delayer
in Camel 1.x and 2.x} <br>The [Delayer] in Camel 1.x works a bit differently than Camel
2.0 onwards. <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">
</span> <span class="diff-added-words"style="background-color: #dfd;">{info:Delayer}</span>
<br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">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 {{currentTimeInMillis() + 3000}}. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">In
Camel 2.0 the</span> <span class="diff-added-words"style="background-color: #dfd;">The</span>
expression is a value in millis to wait from the current time, so the expression should just
be {{3000}}. <br></td></tr>
            <tr><td class="diff-changed-lines" >However <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">in
both Camel 1.x and 2.0</span> you can use a long value for a fixed value to indicate
the delay in millis. <br></td></tr>
            <tr><td class="diff-changed-lines" >See the Spring DSL samples for
<span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">Delayer
in Camel 1.x vs. Camel 2.0.</span> <span class="diff-added-words"style="background-color:
#dfd;">Delayer.</span> <br></td></tr>
            <tr><td class="diff-unchanged" >{info} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >h4. <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">Camel
2.0 -</span> Spring DSL <br></td></tr>
            <tr><td class="diff-unchanged" >The sample below demonstrates the
delay in Spring DSL: <br>{snippet:id=example|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/delayer.xml}
<br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">h4.
Camel 1.x - Spring DSL <br>The delayer is using slightly different names in Camel 1.x:
<br>{code:xml} <br>&lt;delayer&gt; <br>  &lt;delayTime&gt;3000&lt;/delayTime&gt;
<br>  &lt;/expression&gt; <br>&lt;/delayer&gt; <br>{code}
<br> <br>The empty tag {{*&lt;/expression&gt;*}} 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. <br>
<br></td></tr>
            <tr><td class="diff-unchanged" >For further examples of this pattern
in use you could look at the [junit test case|http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DelayerTest.java?view=markup]
<br> <br></td></tr>
            <tr><td class="diff-snipped" >...<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 expression is
a value in millis to wait from the current time, so the expression should just be <tt>3000</tt>.<br/>
However you can use a long value for a fixed value to indicate the delay in millis.<br/>
See the Spring DSL samples for Delayer.</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-SpringDSL"></a>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;bean id=<span class="code-quote">"myDelayBean"</span>
class=<span class="code-quote">"org.apache.camel.processor.MyDelayCalcBean"</span>/&gt;</span>

<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;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"seda:c"</span>/&gt;</span>
        <span class="code-tag">&lt;delay&gt;</span>
            <span class="code-tag">&lt;method ref=<span class="code-quote">"myDelayBean"</span>
method=<span class="code-quote">"delayMe"</span>/&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>

<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=17&originalVersion=16">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