Stop execution of a mule4 flow without a Script
While developing our applications/APIs using mulesoft i.e. mulesoft's anypoint studio, we may confront a situation in which we may have to stop the execution of the mule flow at a particular point in time. In order to do this we may have to take help of an external script such as a Groovy Script, to do it programmatically.
In this article, I am going to explain how we can achieve this without making use of any external script. Observe the below image. Here, I want to stop the mule flow execution after passing through "Logger2". I have added a "Raise error" connector, so, when the control passes through "Logger2" and reaches "Raise error" an exception will be thrown and the control will be transferred to the error handling section of the flow. Since, in mule4 whenever there is an error be it of any type, the flow execution stops at the point where the error has occurred and the control is transferred to the error handling section of the flow or to the flow reference of the main flow from where the sub-flow is being referred in case of error in a sub-flow.
I have specified the error type as "ANY" in the "Raise error" Connector, Please find the screenshot of properties of "Raise error" connector below.
In the error-handling section of the flow, I have given an empty "On Error Continue" scope. And in the properties of the "On Error Continue" we can un-check the field "Log Exception" so that the exception which has been thrown by the "Raise error" connector is not displayed in the logs and the stoppage of our flow execution is smooth.
So, when we trigger the flow the Logger1 is executed, Logger2 is executed but before Logger3 is executed the exception is raised and control is transferred to empty "On Error Continue" scope and the execution halts without logging any exception.
This way we can stop the execution of any mule4 flow without making use of any external script.
Please find the Configuration XML code below:
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd"> <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="d2c12e71-9f33-44f5-8515-616b1733d52a" > <http:listener-connection host="0.0.0.0" port="8081" /> </http:listener-config> <flow name="stop-flow-wsFlow" doc:id="c668e851-46ec-459d-9252-109ec488c47e" > <http:listener doc:name="Listener" doc:id="e8e5a81d-97f7-4cda-8984-483ead08d376" config-ref="HTTP_Listener_config" path="/stopflow"/> <logger level="INFO" doc:name="Logger1" doc:id="b1e57888-acf6-41bb-af46-abc54acb8c5b" message='#["Logger1"]'/> <logger level="INFO" doc:name="Logger2" doc:id="02ba5ce3-69eb-4a8e-ae7a-063cd372ffa9" message='#["Logger2"]'/> <raise-error doc:name="Raise error" doc:id="872e5b03-64d4-499f-a77c-b69d12bfaf0c" type="ANY"/> <logger level="INFO" doc:name="Logger3" doc:id="b54480ea-b89b-4b66-b2bd-152617860c4f" message='#["Logger3"]'/> <set-payload value='#["Hello World"]' doc:name="Set Payload" doc:id="04738f06-77d0-4953-a976-c14589718692" /> <error-handler> <on-error-continue enableNotifications="true" logException="false" doc:name="On Error Continue" doc:id="87ac9f65-3456-4c00-b76c-c461acf0628c" /> </error-handler> </flow> </mule>
Think out of the box