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.
Join the DZone community and get the full member experience.
Join For FreeThere 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.
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
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.
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.
Opinions expressed by DZone contributors are their own.
Comments