Studio Visual Editor
-
Add a Foreach scope in your flow at the point where you want to initiate the Foreach scope processing.
-
Insert one or more message processors inside the Foreach scope area to define how Mule should process each element within the message collection. The Foreach scope can contain any number of message processors as well as references to child flows.
The only type of message processor you cannot drag into a Foreach scope is an inbound connector.
If you drag a two-way connector into a Foreach scope, Mule automatically converts it to an outbound-only connector.
Field Default Value Description XML Display Name
For Each
Customize to display a unique name for the scope in your application.
doc:name="For Each"
Collection
(Optional) Enter an expression that tells Foreach where to find the data it must split and process. For example, enter an expression that instructs Foreach to split and process a collection from the header section – rather than the payload. Unless this field specifies otherwise, Foreach assumes that the message payload is the collection.
collection="#[payload.topic]"
Counter Variable Name
counter
(Optional) Enter a name in this field to label the variable that Foreach uses to record the number of the elements it has processed. If your collection already uses the label counter for another variable, this field will be blank and you will need to enter a different label for the Counter Variable Name, such as
index
.counterVariableName="counter"
Batch Size
1
(Optional) Enter an integer to indicate the number of elements in each batch that Foreach processes. Potentially, these batches promote quicker processing. If greater than one, each batch is treated as a separate Mule message. For example, if a collection has 200 elements and you set the batch size to 50, Foreach will iteratively process 4 batches of 50 elements, each as a separate Mule message.
batchSize="50"
Root Message Variable Name
rootMessage
(Optional) Enter a name in this field to label the variable that Foreach uses to reference the complete, unsplit message collection. If your collection already uses the label rootMessage for another variable, this field will be blank and you will need to enter a different label for the Root Message Variable Name.
rootMessageVariableName="rootMessage"
XML Editor or Standalone
-
Add a foreach element to your flow at the point where you want to initiate a foreach processing block. Refer to the code sample below.
Element Description foreach
Use to create a block of message processors that iteratively process elements within a collection.
-
Configure the scope according to the table below
Element Attribute Default Value Description doc:name
For Each
Customize to display a unique name for the async scope in your application.
Note: Attribute not required in Mule Standalone configuration.
collection
Payload
(Optional) Enter an expression that tells Foreach where to find the data it must split and process. For example, enter an expression that instructs Foreach to split and process a collection from the header section – rather than the payload. Unless this attribute specifies otherwise, Foreach assumes that the message payload is the collection.
counterVariableName
counter
(Optional) Specify to label the variable that Foreach uses to record the number of the elements it has processed. If your collection already uses the label
counter
for another variable, you will need to select a unique name.batchSize
1
(Optional) Specify an integer to indicate the number of elements in each batch that Foreach processes. Potentially, these batches promote quicker processing. For example, if a collection has 200 elements and you set the batch size to 50, Foreach will iteratively process 4 batches of 50 elements.
rootMessageVariableName
rootMessage
(Optional) Specify to label the variable that Foreach uses to reference the complete, unsplit message collection. If your collection already uses the label
rootMessage
for another variable, you will need to select a unique name. -
Add nested elements beneath your
foreach
element to define how Mule should process each element within the message collection. The Foreach scope can contain any number of message processors as well as references to child flows.
<foreach collection="#[payload.name]" doc:name="For Each" counterVariableName="counter" rootMessageVariableName="rootMessage" batchSize="5"> <some-nested-element/> <some-other-nested-element/> </foreach>