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
Please enter at least three characters to search
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

Related

  • How to Use Mulesoft VM Connector
  • Spark Job Optimization
  • KubeVirt Implementation: Who Needs It and Why?
  • Ansible and the Pre-Container Arts

Trending

  • Building Resilient Identity Systems: Lessons from Securing Billions of Authentication Requests
  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. FizzBuzz on Mule 4 With a While Loop Using VM Queue

FizzBuzz on Mule 4 With a While Loop Using VM Queue

Here’s a FizzBuzz flow using Mule. Also, see an elegant way to implement a loop in mule using VM queues instead of the often used flow reference.

By 
Ken Ng user avatar
Ken Ng
·
Jul. 31, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
13.5K Views

Join the DZone community and get the full member experience.

Join For Free

A friend shared this youtube video on the Art of Code where FizzBuzz was demonstrated on SonicPi and also at end of the video (I won’t spoil it for you). After watching it, I was highly inspired to also implement it on Mule, because why not? I even searched the web to see if anyone had already done a FizzBuzz loop on Mule. The fact that I then did it last night kinda tells you that the answer was no.

It turns out that I also learned a thing or two implementing FizzBuzz on Mule 4. FizzBuzz is one of the ways loops are introduced when learning a programming language. Even the recent Golang course I took also introduces loops using FizzBuzz. For the uninitiated, FizzBuzz is derived from a children’s game, the problem statement for a FizzBuzz program is pretty straightforward. This is the same one you can find at HackerRank.

Java
 




x


 
1
Write a short program that prints each number from 1 to 100 on a new line. 
2
For each multiple of 3, print "Fizz" instead of the number. 
3
For each multiple of 5, print "Buzz" instead of the number. 
4
For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number.



As you can see, you need a for loop, and a nested if/else statement. Implementing a nested if/else statement is as simple as just nesting choice routers in Mule. Implementing a loop as a flow in Mule, however, can be slightly trickier. Of course, I could just use a script component to execute the FizzBuzz loop but that would be cheating. 

 Let’s explore loops on Mule in the next section.

Implementing Loops on Mule 4

Using the Flow Reference Component

In my first attempt to implement FizzBuzz flow on Mule 4, I instinctively used the Flow Reference component to loop the flow back to the FizzBuzz subflow routine when the index count is still smaller than the target count. The screenshot of the flow below is quite self-explanatory and the flow does work as design.

When running the flow, Mule 4 will warn you of a potential infinite recursive loop and it will stop processing after it detects too many nested child contexts.

As you can see, after a depth of 45 nested child contexts, the flow stops running and will throw the exception you see in the screenshot above. Clearly we need another way to run legitimate while loops.

Loops on Mule Using VM Queues

This is what I love about MuleSoft’s support site. You can almost always find the answer. I know I’m biased since I’m a Muley. But facts simply are just are facts. Quick research brought me to this article that describes the exception of the nested context (link). Here, the author recommends using VM queues to trigger the loop.

In my second iteration, I revamped the flow so that the payload itself stores the state of the processing. This makes the flow itself stateless, unlike the earlier iteration where I used flow variables to store the state.

What I really like is the elegance is using VM queues to loop in Mule. In this flow, Mule doesn’t need to spawn any child context. Instead, it just processes any messages that are published to the queue. The loop is triggered by passing the payload back into the input queue as long as the exit condition is not true.

All the FizzBuzz evaluations are done with DataWeave.

Dataweave is also used to update the state of the payload.

The full code can also be found here on my GitLab repo.

Conclusion

This started off a bit of a funny thing that I wanted to do just fill the void of a FizzBuzz implementation using Mule 4. But I ended up also learning how to use VM queues as a way to implement a loop in Mule.

Virtual Machine Flow (web browser)

Published at DZone with permission of Ken Ng. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Use Mulesoft VM Connector
  • Spark Job Optimization
  • KubeVirt Implementation: Who Needs It and Why?
  • Ansible and the Pre-Container Arts

Partner Resources

×

Comments
Oops! Something Went Wrong

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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!