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 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
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Create a While Loop in Mule ESB Without Using Java or Groovy

Create a While Loop in Mule ESB Without Using Java or Groovy

You don't need to look outside Mule ESB to make a while loop. Instead, a private flow can do the job just as well.

Satheesh Kumar user avatar by
Satheesh Kumar
·
Jul. 28, 16 · Tutorial
Like (12)
Save
Tweet
Share
29.11K Views

Join the DZone community and get the full member experience.

Join For Free

Using Anypoint Studio's inbuilt component is an easy way to perform foreach, but when it comes to a situation where a while loop has to be used, Mulesoft does not provide a while loop component like other integration tools do. To overcome this, we put a lot of effort into creating a Groovy or a JavaScript solution, but there is an easy way to overcome this and create a while loop using a simple flow ref component.

Image title


In Mule flows, a subflow cannot call itself. But a private flow can all itself in a recursive loop. We are going to implement the while loop using this logic.

Scenario

In this scenario, I am inputting the XML below. I need to make outbound calls the number of times it's mentioned in the XML call frequency tag. Here, the callfrequency is 10, so we need to call the outbound application 10 times.


<StudentData>
<callfrequency>10</callfrequency>
<EndDate>27-07-2025</EndDate>
</StudentData>


To achieve this, I have a main flow that accepts inbound requests and stores the call frequency value in a variable, and a while loop is called using a flow ref.

Main Flow

Image title


The XML of the main flow is given below:


<flow name="main_flow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="Inbound HTTP call"/>
    <set-variable variableName="counter" value="#[xpath3('//callfrequency')]" doc:name="Storing the call frequency"/>
    <flow-ref name="while_loop" doc:name="Call while Loop"/>
</flow>


Now in the flow where we perform the looping operation, the counter variable is deducted as shown below, and the flow is made to call itself again and again until the choice condition fails. Once the condition fails, the loop ends and returns back to the main flow.

While Loop Flow


The XML of the while loop flow is given below:


<flow name="while_loop">
    <http:request config-ref="HTTP_Request_Configuration" path="testoutbound" method="PATCH" doc:name="outboundCall"/>
    <choice doc:name="Counter Value check">
        <when expression="#[flowVars.counter &gt;1]">
            <set-variable variableName="counter" value="#[flowVars.counter - 1 ]" doc:name="Counter deduction"/>
            <flow-ref name="while_loop" doc:name="while_loop"/>
        </when>
        <otherwise>
            <logger message="The loop breaks" level="INFO" doc:name="The loop breaks"/>
        </otherwise>
    </choice>
</flow>


Hence the scenario is achieved using only Mule components.

The full config.xml is given below:


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

<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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:spring="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8085" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" host="abc.com" doc:name="HTTP Request Configuration" port="8081"/>
    <flow name="main_flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="Inbound HTTP call"/>
        <set-variable variableName="counter" value="#[xpath3('//callfrequency')]" doc:name="Storing the call frequency"/>
        <flow-ref name="while_loop" doc:name="Call while Loop"/>
    </flow>
    <flow name="while_loop">
        <http:request config-ref="HTTP_Request_Configuration" path="testoutbound" method="PATCH" doc:name="outboundCall"/>
        <choice doc:name="Counter Value check">
            <when expression="#[flowVars.counter &gt;1]">
                <set-variable variableName="counter" value="#[flowVars.counter - 1 ]" doc:name="Counter deduction"/>
                <flow-ref name="while_loop" doc:name="while_loop"/>
            </when>
            <otherwise>
                <logger message="The loop breaks" level="INFO" doc:name="The loop breaks"/>
            </otherwise>
        </choice>
    </flow>
</mule>
Enterprise service bus Flow (web browser) Groovy (programming language) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?
  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey
  • How Elasticsearch Works
  • Monolithic First

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
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: