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

  • Automating Power Automate: How to Ensure Cloud Flows Are Active After Every Pipeline Deployment
  • How Power Automate Helps Analysts Send Alert Emails Faster and How AI Builder Takes It to the Next Level
  • 2 Hidden Bottlenecks in Large-Scale Azure Migrations
  • How to Create a Custom React Component in Vaadin Flow

Trending

  • From "Vibe Coding" to Production: Setting Up an Evals Loop for Claude Agents
  • Testing Is Not About Finding Bugs
  • Amazon CodeWhisperer to Q Developer to Kiro: The Rise of Agentic Coding
  • Architecting Proactive IT: NinjaOne Remote Monitoring and Management

Stop and Start Mule Flow Dynamically at Runtime

Learn to stop a Mule flow at runtime to avoid unnecessary message processing and error handling when the external endpoint is unavailable.

By 
Khushboo Tripathi user avatar
Khushboo Tripathi
·
Jul. 08, 17 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
14.9K Views

Join the DZone community and get the full member experience.

Join For Free

There is sometimes a requirement to stop the Mule flow dynamically when an external endpoint is not accessible to hold the processing of a message until the endpoint is available. This is to avoid the unnecessary processing of the message and error handling.

Example: a Mule process tries to connect to the external database and the database is not accessible. After the Mule process fails to connect with the database for a predefined interval, then the Mule process logs the error and stops processing further messages.

This has been achieved by using a Groovy script to start and stop Mule flow dynamically.

Sample Mule Flow: the Mule flow receives the HTTP request and inserts the request parameter to the database. If there is a database connectivity error then it executes the sub-flow to stop the Mule flow.

Image title

In this example, the Mule process tries to insert the data into the database, and if there is any error, a Choice exception will catch the exception as mentioned in the Mule expression. If there is any database connection error this will go through the ‘Catch Database Exception’ and will call the sub-flow ‘demoStopFlow.’

Stopping the Process at Runtime Using the Groovy Script Without User Intervention

suspend flow

This flow will check the flag condition. If it is true, then it executes the Groovy script to stop the process, or otherwise execute the default condition.

Below is the Groovy script, which will stop the specified process:

  <scripting:component doc:name="Groovy">
                    <scripting:script engine="Groovy">
                    <![CDATA[if(muleContext.registry.lookupFlowConstruct(flowVars.demoStartFlow).isStarted()                       && demo.test.isdb.stop=='true')
                        {
                                           muleContext.registry.lookupFlowConstruct(flowVars.demoStartFlow).stop();
flowVars.pubSuspendstatus=flowVars.demoStartFlow+' Suspended now ';
          }
                       else
                        {
         flowVars.pubSuspendstatus='Process already suspended';
             }            
               ]]></scripting:script>

If the process is in running status, it will stop the mentioned Mule process, or otherwise execute the else block and set the process status value.

Start the Process Without Any Deployment

Once the endpoint or target is up, then the support team can start the process without any deployment. The support team has to send a dummy message to the queue. This will check if the specified process is running or not. If it is stopped, then it will start the specified process.

Suspend flow

Here is the Groovy Script to start the flow:

<scripting:component doc:name="Groovy">
 <scripting:script engine="Groovy"> <![CDATA[
if(muleContext.registry.lookupFlowConstruct('demoFlow').isStopped()) {
muleContext.registry.lookupFlowConstruct('demoFlow').start();
flowVars.pubSuspendstatus='Process started now.';
} else {
flowVars.pubSuspendstatus='Processs already started';
}
          ]]>
</scripting:script>
</scripting:component>

This script first checks the condition. If it is stopped, this will start the mentioned flow, or otherwise execute the else condition and set the process status.

You can find the code on Github.

Flow (web browser)

Opinions expressed by DZone contributors are their own.

Related

  • Automating Power Automate: How to Ensure Cloud Flows Are Active After Every Pipeline Deployment
  • How Power Automate Helps Analysts Send Alert Emails Faster and How AI Builder Takes It to the Next Level
  • 2 Hidden Bottlenecks in Large-Scale Azure Migrations
  • How to Create a Custom React Component in Vaadin Flow

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