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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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

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.

Khushboo Tripathi user avatar by
Khushboo Tripathi
·
Jul. 08, 17 · Tutorial
Like (7)
Save
Tweet
Share
12.91K 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.

Popular on DZone

  • How to Develop a Portrait Retouching Function
  • Public Cloud-to-Cloud Repatriation Trend
  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • Visual Network Mapping Your K8s Clusters To Assess Performance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: