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.Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
8.14. Loop
Loop Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
					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 Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
					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 Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
					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 Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
					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 Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
					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 Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
					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.
						 | 
					
Do While Loop Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
					You can perform the loop until a condition is met using a 
do while loop. The condition will either be true or false.
				
					In DSL, the command is 
LoopDoWhile. The following example will perform the loop until the message body length is 5 characters or less:
				
					In XML, the command is 
loop doWhile. The following example also performs the loop until the message body length is 5 characters or less: