Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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.

· Integration Zone
Free Resource

Today’s data climate is fast-paced and it’s not slowing down. Here’s why your current integration solution is not enough. Brought to you in partnership with Liaison Technologies.

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>

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and iPaaS+ could cost you down the road. Brought to you in partnership with Liaison Technologies.

Topics:
integration service ,mule 3.7 ,mule esb ,while statement r

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}