Foreach
The foreach message processor allows iterating over a collection of elements present in the current message (either in the payload or properties) and then continue processing the original message. If the message payload is a java collection, a map, an array or a MuleMessageCollection, no extra attributes are needed to generate each message. Otherwise an expression can be used to get a collection from the message payload or properties.
The message processor is configured as follows:
The example below shows how the xml payload can be modified inside foreach using a script component:
It can also be used to process the collection in batches of the specified size:
Foreach also puts in scope the variable 'counter' that holds the number of message being processed and the variable 'rootMessage' that contains the original message. Both variables may be renamed through the attributes 'counterVariableName' and 'rootMessageVariableName'.