DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Related

  • XML Processing Made Easy with Ballerina
  • Conversational Applications With Large Language Models Understanding the Sequence of User Inputs, Prompts, and Responses
  • Reversing an Array: An Exploration of Array Manipulation
  • Error Handling Inside Kumologica Subflow

Trending

  • Comprehensive Guide on Integrating OpenAI ChatGPT API With React JS
  • The ABCs of Unity's Coroutines: From Basics to Implementation
  • Mastering Cloud Migration: Best Practices to Make it a Success
  • The Scrum Trap
  1. DZone
  2. Data Engineering
  3. Databases
  4. Iterative Processing Using the For Each Scope in Mule

Iterative Processing Using the For Each Scope in Mule

Learn how to implement the For Each scope in Mule applications to simplify the splitting and aggregation of message collections.

Ankit Lawaniya user avatar by
Ankit Lawaniya
·
Sep. 06, 17 · Tutorial
Like (12)
Save
Tweet
Share
47.4K Views

Join the DZone community and get the full member experience.

Join For Free

The For Each scope is one of the scopes available in Mule, which splits a collection into individual elements and processes them iteratively through the processors embedded in the scope, then returns the original message to the flow. This scope basically simplifies the splitting and aggregation of message collections.

How It Works

The For Each scope does not change the type of the message collection and does not change the contents of the of the original message collection, if the individual collection elements are immutable. That is, after For Each splits a message collection and processes the individual elements, it does not re-aggregate those individual elements into a MuleMessageCollection; rather, it returns the original message (this means “Java in, Java out,” rather than “Java in, MuleMessageCollection out”).

The For Each scope is versatile; it can iteratively process elements from any type of collection, including maps, lists, arrays, and MuleMessageCollection.

IMP Points of For Each:

  1. You can insert any message processor, except inbound connector, in the For Each scope. If we drag a two-way connector into a For Each scope, Mule automatically converts it to an outbound-only connector.

  2. When the For Each scope receives a message payload that is not a collection type like maps, lists, arrays, and MuleMessageCollection, it throws an IllegalArgumentException.

  3. The rootMessage variable (flow variable), which is associated with the message by default, contains a reference of the complete, unsplit message collection.

  4. The counter variable (flow variable) is associated with the message by default. For Each uses a counter variable to record the number of the elements it has processed.

In this example, Mule uses a For Each message processor to extract and iteratively process records from a database.

Flow:

Image title

Code:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.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/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">

<db:generic-config name="Generic_Database_Configuration"
url="jdbc:hsqldb:hsql://localhost:9001" driverClassName="org.hsqldb.jdbcDriver"
doc:name="Generic Database Configuration" />
<http:listener-config name="HTTP_Listener_Configuration"
host="localhost" port="8081" basePath="/foreach" doc:name="HTTP Listener Configuration" />
<flow name="forEachFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/foreach" doc:name="HTTP" />
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload]]></dw:set-payload>
        </dw:transform-message> 
  <foreach doc:name="For Each Loop">
  <logger level="INFO" message="#[payload]" doc:name="Logger" />
   <enricher doc:name="Message Enricher">
<db:select config-ref="Generic_Database_Configuration"
doc:name="Select * from Employee">
<db:dynamic-query><![CDATA[select name from employee where id='#[payload.id]']]></db:dynamic-query>
</db:select>
<enrich target="#[flowVars.name]" source="#[payload[0].name]"/>
 </enricher>
 <logger level="INFO" message="#[flowVars.name]" doc:name="Logger" />
 <expression-component doc:name="Expression"><![CDATA[ 
           payload.name= flowVars.name;
         ]]></expression-component> 
 </foreach>

<object-to-string-transformer />
<set-payload value="#[payload]"  doc:name="Set Payload"/>
<logger level="INFO" message="#[payload]" doc:name="Logger" />
</flow>
</mule>


Request:

[{
"id":1,
"role":"Application Developer"
},
{
"id":3,
"role":"Application Manager"
}
]

Image title

Response:

Image title

Hope this helps.

Thanks.

Keep learning.

Element Processing Database Connector (mathematics) Flow (web browser) Extract Convert (command)

Opinions expressed by DZone contributors are their own.

Related

  • XML Processing Made Easy with Ballerina
  • Conversational Applications With Large Language Models Understanding the Sequence of User Inputs, Prompts, and Responses
  • Reversing an Array: An Exploration of Array Manipulation
  • Error Handling Inside Kumologica Subflow

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: