You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

Loop

The Loop allows to process the a message a number of times and possibly process them in a different way. Useful mostly for testing.

Default mode

Notice by default the loop uses the same exchange throughout the looping. So the result from the previous iteration is 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.

Options

Unknown macro: {div}

Name

Default Value

Description

copy

false

Camel 2.8: Whether or not copy mode is used. If false then the same Exchange is being used throughout the looping. So the result from the previous iteration will be visible for the next iteration. Instead you can enable copy mode, and then each iteration is restarting with a fresh copy of the input Exchange.

Exchange properties

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.

Property

Description

CamelIterationCount

Camel 1.x: Total number of iterations to be run

CamelIterationIndex

Camel 1.x: Index of the current iteration (0 based)

CamelLoopSize

Camel 2.0: Total number of loops

CamelLoopIndex

Camel 2.0: Index of the current iteration (0 based)

that could be used by processors down the pipeline to process the Message in different ways.

Examples

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 loop(), or determined at runtime by evaluating an expression. The expression must evaluate to an int, otherwise a RuntimeCamelException is thrown.

Using the Fluent Builders

Pass loop count as an argument

Error formatting macro: snippet: java.lang.NullPointerException

Use expression to determine loop count

Error formatting macro: snippet: java.lang.NullPointerException

Use expression to determine loop count

Error formatting macro: snippet: java.lang.NullPointerException

Using the Spring XML Extensions

Pass loop count as an argument

Error formatting macro: snippet: java.lang.NullPointerException

Use expression to determine loop count

Error formatting macro: snippet: java.lang.NullPointerException

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.
The output of processing this route will be that, each "mock:loop" endpoint will receive "AB" as message.

Error formatting macro: snippet: java.lang.NullPointerException

However if we do not enable copy mode then "mock:loop" will receive "AB", "ABB", "ABBB" messages.

Error formatting macro: snippet: java.lang.NullPointerException

The equivalent example in XML DSL in copy mode is as follows:

Error formatting macro: snippet: java.lang.NullPointerException

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.

  • No labels