Over a million developers have joined DZone.

Stop and Start Mule Flow Dynamically at Runtime

DZone 's Guide to

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.

· Integration Zone ·
Free Resource

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')
flowVars.pubSuspendstatus=flowVars.demoStartFlow+' Suspended now ';
         flowVars.pubSuspendstatus='Process already suspended';

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()) {
flowVars.pubSuspendstatus='Process started now.';
} else {
flowVars.pubSuspendstatus='Processs already started';

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.

mule esb

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}