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 Description Display Name
Default value =
For Each
- Customize to display a unique name for the scope in your application.XML Example:
doc:name="For Each"
Collection
(Optional) No default value. - 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.
XML Example:
collection="#[payload.topic]"
Counter Variable Name
(Optional) Default value =
counter
- 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 labelcounter
for another variable, this field is blank and you need to enter a different label for the Counter Variable Name, such asindex
.XML Example:
counterVariableName="counter"
Batch Size
(Optional) Default value =
1
- 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 iteratively processes 4 batches of 50 elements, each as a separate Mule message.XML Example:
batchSize="50"
Root Message Variable Name
(Optional) Default value =
rootMessage
- 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 labelrootMessage
for another variable, this field is blank and you need to enter a different label for the Root Message Variable Name.XML Example:
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, 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 iteratively processes 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, 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>