For Each Example in Mule 4
The For Each scope splits a payload into elements and processes them one by one through the components that you place in the scope.
General views about For Each scope:
1) By default, For Each tries to split the payload. If the payload is a simple Java collection, the For Each scope can split it without any configuration. The payload inside the For Each scope is each of the split elements. Attributes within the original message are ignored because they are related to the entire message.
2) For Each does not modify the current payload. The output payload is the same as the input.
3) For non-Java collections, such as XML or JSON, use a DataWeave expression to split data. Use the Collection field for this purpose.
For Each scope in mule 4 is used to iterate over a collection.
Flow:
Database connector is used to fetch records from database.
Db connector configuration:
Transform message settings:
Foreach scope configuration:
Method: GET
Output:
XML project code:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<db:config name="Database_Config" doc:name="Database Config" doc:id="ea80f53a-1041-48b2-aa07-8d6741cf43ea" >
<db:my-sql-connection host="localhost" port="3306" user="root" password="”*******" database="mule4" />
</db:config>
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="6055fd67-52f6-4cf0-91b1-6818bf2980e8" >
<http:listener-connection host="0.0.0.0" port="8085" />
</http:listener-config>
<flow name="test_foreachFlow" doc:id="b081e93c-8b0f-4a8a-9bc8-01507eccad4d" >
<http:listener doc:name="Listener" doc:id="c9bb4fdf-629f-4426-aec0-9b705b6b4c44" config-ref="HTTP_Listener_config" path="/foreach"/>
<db:select doc:name="Select" doc:id="08464734-060c-45c6-a958-e1886079882b" config-ref="Database_Config">
<db:sql>select * from mule4.info</db:sql>
</db:select>
<ee:transform doc:name="Transform Message" doc:id="e62e80cf-74d2-48e2-808c-439378036cf5" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
payload map(index,value)->
{
ID: index.ID,
NAME: index.NAME
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<foreach doc:name="For Each" doc:id="1b160052-cf2b-4cbc-981d-1faa0325c1a3" >
<logger level="INFO" doc:name="Logger" doc:id="ee4e0821-d2da-4e2e-9ce7-385218616824" message="#[payload]" />
</foreach>
</flow>
</mule>
No comments:
Post a Comment