...
Info | ||
---|---|---|
| ||
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 Pipes And and Filters). From Camel 2.8 onwards you can enable copy mode instead. See the options table for more details. |
...
Div | ||
---|---|---|
| ||
Name | Default Value | Description |
---|---|---|
|
| Camel 2.8: Whether or not copy mode is used. If |
doWhile | Camel 2.17: Enables the while loop that loops until the predicate evaluates to false or null. |
Exchange properties
For each iteration two properties are set on the Exchange
. Processors can rely on these properties to process the Message in different ways.
Property | Description |
---|---|
| Total number of loops. This is not available if running the loop in while loop mode. |
| Index of the current iteration (0 based) |
...
Pass loop count as an argument
Wiki Markup |
---|
{snippet:id=ex1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTest.java} |
Wiki Markup |
---|
{snippet:id=ex2|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTest.java} |
Wiki Markup |
---|
{snippet:id=ex3|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTest.java} |
Pass loop count as an argument
Wiki Markup |
---|
{snippet:id=ex1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/loop.xml} |
Wiki Markup |
---|
{snippet:id=ex2|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/loop.xml} |
...
Now suppose we send a message to "direct:start" endpoint containing the letter A.
The output of processing this route will be that, each "mock:loop" endpoint will receive "AB" as message.
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopCopyTest.java} |
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopNoCopyTest.java} |
Wiki Markup |
---|
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringLoopCopyTest.xml} |
Using while mode
Available as of Camel 2.17
The loop can act like a while loop that loops until the expression evaluates to false or null.
For example the route below loops while the length of the message body is 5 or less characters. Notice that the DSL uses loopDoWhile.
Code Block |
---|
from("direct:start") .loopDoWhile(simple("${body.length} <= 5")) .to("mock:loop") .transform(body().append("A")) .end() .to("mock:result"); |
And the same example in XML:
Code Block | ||||
---|---|---|---|---|
| ||||
<route> <from uri="direct:start"/> <loop doWhile="true"> <simple>${body.length} <= 5</simple> <to uri="mock:loop"/> <transform> <simple>A${body}</simple> </transform> </loop> <to uri="mock:result"/> </route> |
Notice in XML that the while loop is turned on using the doWhile attribute.
Include Page | ||||
---|---|---|---|---|
|