camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Loop
Date Mon, 30 Jan 2012 15:14: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/Loop">Loop</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~mazzag">Glen
Mazza</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        editorial cleanup<br />
    </div>
        <br/>
                         <h4>Changes (11)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-unchanged" >h2. Loop <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">The
Loop allows to process the a message a number of times and possibly process them in a different
way.  Useful mostly for testing. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
Loop allows for processing a message a number of times, possibly in a different way for each
iteration.  Useful mostly during testing. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{info:title=Default mode}
<br></td></tr>
            <tr><td class="diff-changed-lines" >Notice by default the loop uses
the same exchange throughout the looping. So the result from the previous iteration <span
class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">is</span>
<span class="diff-added-words"style="background-color: #dfd;">will be</span> used
for the next (eg [Pipes And Filters]). From *Camel 2.8* onwards you can enable copy mode instead.
See the options table for more details. <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" >{div:class=confluenceTableSmall} <br>||
Name || Default Value || Description || <br></td></tr>
            <tr><td class="diff-changed-lines" >| {{copy}} | {{false}} | *Camel
2.8:* Whether or not copy mode is used. If {{false}} then the same Exchange <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">is
being</span> <span class="diff-added-words"style="background-color: #dfd;">will
be</span> used <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">throughout
the looping.</span> <span class="diff-added-words"style="background-color: #dfd;">for
each iteration.</span> So the result from the previous iteration will be _visible_ for
the next iteration. Instead you can enable copy mode, and then each iteration <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">is
_restarting_</span> <span class="diff-added-words"style="background-color: #dfd;">_restarts_</span>
with a fresh copy of the input [Exchange]. | <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h3. Exchange properties
<br>For each iteration two properties are set on the {{Exchange}} that could be used
by processors down the pipeline to process the [Message] in different ways. <br>|| Property
|| Description || <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">|
{{CamelIterationCount}} | Camel 1.x: Total number of iterations to be run | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
{{CamelLoopSize}} | Total number of loops | <br></td></tr>
            <tr><td class="diff-changed-lines" >| <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">{{CamelIterationIndex}}</span>
<span class="diff-added-words"style="background-color: #dfd;">{{CamelLoopIndex}}</span>
| <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">Camel
1.x:</span> Index of the current iteration (0 based) | <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">|
{{CamelLoopSize}} | Camel 2.0: Total number of loops | <br>| {{CamelLoopIndex}} | Camel
2.0: Index of the current iteration (0 based) | <br>that could be used by processors
down the pipeline to process the [Message] in different ways. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Processors
can rely on the above information to process the [Message] in different ways. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h4. Examples <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >The following example shows how
to take a request from the *direct:x* <span class="diff-changed-words">endpoint<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">
</span>,</span> then send the message repetitively to *mock:result*.  The number
of times the message is sent is either passed as an argument to {{loop()}}, or determined
at runtime by evaluating an expression.  The expression *must* evaluate to an {{int}}, otherwise
a {{RuntimeCamelException}} is thrown. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>*Using the [Fluent Builders]*
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopCopyTest.java}
<br> <br></td></tr>
            <tr><td class="diff-changed-lines" >However if we do *not* enable
copy mode then &quot;mock:loop&quot; will receive &quot;AB&quot;, &quot;ABB&quot;,
<span class="diff-changed-words">&quot;ABBB&quot;<span class="diff-added-chars"style="background-color:
#dfd;">, etc.</span></span> messages. <br></td></tr>
            <tr><td class="diff-unchanged" >{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopNoCopyTest.java}
<br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Loop-Loop"></a>Loop</h2>
<p>The Loop allows for processing a message a number of times, possibly in a different
way for each iteration.  Useful mostly during testing.</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><b>Default mode</b><br
/>Notice by default the loop uses the same exchange throughout the looping. So the result
from the previous iteration will be used for the next (eg <a href="/confluence/display/CAMEL/Pipes+and+Filters"
title="Pipes and Filters">Pipes and Filters</a>). From <b>Camel 2.8</b>
onwards you can enable copy mode instead. See the options table for more details.</td></tr></table></div>

<h3><a name="Loop-Options"></a>Options</h3>
<div class="confluenceTableSmall"></div>
<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>copy</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8:</b> Whether or not copy mode is
used. If <tt>false</tt> then the same Exchange will be used for each iteration.
So the result from the previous iteration will be <em>visible</em> for the next
iteration. Instead you can enable copy mode, and then each iteration <em>restarts</em>
with a fresh copy of the input <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>.
</td>
</tr>
</tbody></table>
</div>


<h3><a name="Loop-Exchangeproperties"></a>Exchange properties</h3>
<p>For each iteration two properties are set on the <tt>Exchange</tt> that
could be used by processors down the pipeline to process the <a href="/confluence/display/CAMEL/Message"
title="Message">Message</a> in different ways.</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Property </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelLoopSize</tt> </td>
<td class='confluenceTd'> Total number of loops </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelLoopIndex</tt> </td>
<td class='confluenceTd'> Index of the current iteration (0 based) </td>
</tr>
</tbody></table>
</div>


<p>Processors can rely on the above information to process the <a href="/confluence/display/CAMEL/Message"
title="Message">Message</a> in different ways.</p>

<h4><a name="Loop-Examples"></a>Examples</h4>

<p>The following example shows how to take a request from the <b>direct:x</b>
endpoint, then send the message repetitively to <b>mock:result</b>.  The number
of times the message is sent is either passed as an argument to <tt>loop()</tt>,
or determined at runtime by evaluating an expression.  The expression <b>must</b>
evaluate to an <tt>int</tt>, otherwise a <tt>RuntimeCamelException</tt>
is thrown.</p>

<p><b>Using the <a href="/confluence/display/CAMEL/Fluent+Builders" title="Fluent
Builders">Fluent Builders</a></b></p>

<p>Pass loop count as an argument</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:a"</span>).loop(8).to(<span
class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>Use expression to determine loop count</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:b"</span>).loop(header(<span
class="code-quote">"loop"</span>)).to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>Use expression to determine loop count</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:c"</span>).loop().xpath(<span
class="code-quote">"/hello/@times"</span>).to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p><b>Using the <a href="/confluence/display/CAMEL/Spring+XML+Extensions" title="Spring
XML Extensions">Spring XML Extensions</a></b></p>

<p>Pass loop count as an argument</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">"direct:a"</span>/&gt;</span>
  <span class="code-tag">&lt;loop&gt;</span>
    <span class="code-tag">&lt;constant&gt;</span>8<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;/loop&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<p>Use expression to determine loop count</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">"direct:b"</span>/&gt;</span>
  <span class="code-tag">&lt;loop&gt;</span>
    <span class="code-tag">&lt;header&gt;</span>loop<span class="code-tag">&lt;/header&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
  <span class="code-tag">&lt;/loop&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<p>For further examples of this pattern in use you could look at one of the <a href="http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTest.java?view=markup"
class="external-link" rel="nofollow">junit test case</a></p>

<h4><a name="Loop-Usingcopymode"></a>Using copy mode</h4>
<p><b>Available as of Camel 2.8</b></p>

<p>Now suppose we send a message to "direct:start" endpoint containing the letter A.<br/>
The output of processing this route will be that, each "mock:loop" endpoint will receive "AB"
as message.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// instruct loop to use copy mode, which mean it will
use a copy of the input exchange
</span>    <span class="code-comment">// <span class="code-keyword">for</span>
each loop iteration, instead of keep using the same exchange all over
</span>    .loop(3).copy()
        .transform(body().append(<span class="code-quote">"B"</span>))
        .to(<span class="code-quote">"mock:loop"</span>)
    .end()
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>However if we do <b>not</b> enable copy mode then "mock:loop" will receive
"AB", "ABB", "ABBB", etc. messages.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// by <span class="code-keyword">default</span>
loop will keep using the same exchange so on the 2nd and 3rd iteration its
</span>    <span class="code-comment">// the same exchange that was previous used
that are being looped all over
</span>    .loop(3)
        .transform(body().append(<span class="code-quote">"B"</span>))
        .to(<span class="code-quote">"mock:loop"</span>)
    .end()
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>The equivalent example in XML DSL in copy mode is as follows:</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">"direct:start"</span>/&gt;</span>
  <span class="code-tag"><span class="code-comment">&lt;!-- enable copy mode
for loop eip --&gt;</span></span>
  <span class="code-tag">&lt;loop copy=<span class="code-quote">"true"</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;transform&gt;</span>
      <span class="code-tag">&lt;simple&gt;</span>${body}B<span class="code-tag">&lt;/simple&gt;</span>
    <span class="code-tag">&lt;/transform&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:loop"</span>/&gt;</span>
  <span class="code-tag">&lt;/loop&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>


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

Mime
View raw message