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
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Why "Polyglot Programming" or "Do It Yourself Programming Languages" or "Language Oriented Programming" sucks?
  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j

Trending

  • Why Your DLP Policies Fall Short the Moment AI Agents Enter the Picture
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • GenAI Implementation Isn't Magic — It’s a Lifecycle
  • Stop Running Two Data Systems for One Agent Query
  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.

By 
Satheesh Kumar user avatar
Satheesh Kumar
·
Jul. 28, 16 · Tutorial
Likes (12)
Comment
Save
Tweet
Share
32.2K 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.

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Why "Polyglot Programming" or "Do It Yourself Programming Languages" or "Language Oriented Programming" sucks?
  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook