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

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Why "Polyglot Programming" or "Do It Yourself Programming Languages" or "Language Oriented Programming" sucks?
  • Step-by-Step Guide to Use Anypoint MQ: Part 1
  • Step By Step Guide To Using Mule ESB

Trending

  • Mastering Advanced Aggregations in Spark SQL
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • What Is Plagiarism? How to Avoid It and Cite Sources
  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
31.8K 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?
  • Step-by-Step Guide to Use Anypoint MQ: Part 1
  • Step By Step Guide To Using Mule ESB

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • 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: