2. Async Scope With Mule ESB
Async scope is branch processing block that executes simultaneously with the parent
message flow. It means that you have some activity in the flow that can be process
without stopping the flow.There are some time consuming operation (printing a file
or sending SMTP email) which doesn’t have dependency in further flow. In such
scenarios you can use Async scope.
There are two flows parent and child flow in your mule application. Parent flow is
calling the child flow to perform some task but parent flow doesn’t have dependency
from result of child flow. In such scenarios if you don’t use the Async scope then
parent flow stop its execution till child flow complete it’s execution.
In such scenarios, we should use Async scope, so parent and child flow both will
execute in the parallel in different threads.
3. Async Scope With Mule ESB
Scope is basically known as a wrappers. Mule provide Async scope and it is available in
Mule palette.
4. Async Scope With Mule ESB
1.) Drag and Drop HTTP Listener in canvas and configure it.
5. Async Scope With Mule ESB
2.) Drag and Drop child flow below parent flow. Place the groovy component and add
script to wait flow for 30 seconds.
6. Async Scope With Mule ESB
3.) Place Set Payload component afterGroovy in child flow.
7. Async Scope With Mule ESB
4.) Place the Async scope in parent flow and then flow reference component inAsync
scope. Call the child flow.
8. Async Scope With Mule ESB
5.) Place the Set Payload component in the parent flow.
9. Conclusion
Once you will POST the message, the parent flow call the child flow asynchronously.
Child flow will wait for 30 seconds but parent flow will not stop it execution. It will
continue its processing. Both the flow continue the processing in parallel.
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
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://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0"port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="async-scopeFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/async" allowedMethods="POST" doc:name="HTTP"/>
<async doc:name="Async">
<flow-ref name="async-scopeFlow1" doc:name="async-scopeFlow1"/>
</async>
<set-payload value="#['Parent Flow Completed']" doc:name="Set Payload"/>
</flow>
<flow name="async-scopeFlow1">
<scripting:component doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[sleep(30000)]]></scripting:script>
</scripting:component>
<set-payload value="#['Child flow executed.']" doc:name="Set Payload"/>
</flow>
</mule>