Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.Questo contenuto non è disponibile nella lingua selezionata.
8.14. Loop
Loop Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
The loop pattern enables you to process a message multiple times. It is used mainly 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.
Exchange properties Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
On each loop iteration, two exchange properties are set, which can optionally be read by any processors included in the loop.
Property | Description |
---|---|
CamelLoopSize
|
Apache Camel 2.0: Total number of loops |
CamelLoopIndex
|
Apache Camel 2.0: Index of the current iteration (0 based) |
Java DSL examples Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
The following examples show how to take a request from the
direct:x
endpoint and then send the message repeatedly to mock:result
. The number of loop iterations is specified either as an argument to loop()
or by evaluating an expression at run time, where the expression must evaluate to an int
(or else a RuntimeCamelException
is thrown).
The following example passes the loop count as a constant:
from("direct:a").loop(8).to("mock:result");
from("direct:a").loop(8).to("mock:result");
The following example evaluates a simple expression to determine the loop count:
from("direct:b").loop(header("loop")).to("mock:result");
from("direct:b").loop(header("loop")).to("mock:result");
The following example evaluates an XPath expression to determine the loop count:
from("direct:c").loop().xpath("/hello/@times").to("mock:result");
from("direct:c").loop().xpath("/hello/@times").to("mock:result");
XML configuration example Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
You can configure the same routes in Spring XML.
The following example passes the loop count as a constant:
The following example evaluates a simple expression to determine the loop count:
Using copy mode Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
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.
However if we do not enable copy mode then
mock:loop
will receive AB, ABB, ABBB messages.
The equivalent example in XML DSL in copy mode is as follows:
Options Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
The
loop
DSL command supports the following options:
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.
|