The Loop allows for processing a message a number of times, possibly in a different way for each iteration. Useful mostly during testing.
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 Filters). From Camel 2.8 onwards you can enable copy mode instead. See the options table for more details.
For each iteration two properties are set on the
The following example shows how to take a request from the direct:x endpoint, then send the message repetitively to mock:result. The number of times the message is sent is either passed as an argument to
Using the Fluent Builders
Pass loop count as an argumentUse expression to determine loop count Use expression to determine loop count Using the Spring XML Extensions
Pass loop count as an argumentUse expression to determine loop count For further examples of this pattern in use you could look at one of the junit test case
Using copy mode
Available as of Camel 2.8
Now suppose we send a message to "direct:start" endpoint containing the letter A.
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.
And the same example in XML:
Notice in XML that the while loop is turned on using the doWhile attribute.
Using This Pattern
If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.