Mulesoft Asynchronous Flow Example
In MuleSoft, asynchronous flows are used to handle tasks asynchronously, allowing your application to continue processing without waiting for a potentially time-consuming operation to complete. Asynchronous flows are particularly useful for scenarios like making external API calls, processing large data sets, or performing tasks that might take a while to finish.
Here’s an example of how to create an asynchronous flow in MuleSoft:
Create a New Mule Project:
- Open Anypoint Studio and create a new Mule project or open an existing one.
Create a Flow:
- Create a new flow in your Mule project. You can do this by right-clicking on your project in the Package Explorer and selecting “New > Flow.”
Configure an HTTP Listener:
- Within your flow, configure an HTTP listener to accept incoming requests. You can use the “HTTP Listener” connector for this purpose.
- Set the “Path” attribute to define the URL path at which your service will be available.
Add an Asynchronous Processor:
- Add an asynchronous processor to your flow. You can use the
<async>
scope for this. - Inside the
<async>
scope, define the logic that you want to execute asynchronously.
- Add an asynchronous processor to your flow. You can use the
Here’s an example XML configuration for an asynchronous flow:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
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 Listener to accept incoming requests –>
<http:listener-config name=“HTTP_Listener_Config” host=“0.0.0.0” port=“8081” basePath=“/async-flow” doc:name=“HTTP Listener Config”/>
<!– Main flow –>
<flow name=“asyncFlow”>
<http:listener config-ref=“HTTP_Listener_Config” path=“/process” allowedMethods=“POST” doc:name=“HTTP”/>
<!– Asynchronous scope –>
<async doc:name=“Async”>
<!– Logic to be executed asynchronously –>
<set-payload value=“Asynchronous processing started.” />
<logger message=“Asynchronous task started.” level=“INFO” doc:name=“Logger”/>
<!– Simulate a delay to show asynchronous behavior –>
<set-property propertyName=“MULE_THREAD_SLEEP” value=“5000” />
<logger message=“Asynchronous task completed.” level=“INFO” doc:name=“Logger”/>
</async>
<!– Continue processing after the asynchronous scope –>
<logger message=“Processing completed.” level=“INFO” doc:name=“Logger”/>
</flow>
</mule>
In this example, the /async-flow/process
endpoint accepts incoming POST requests. Inside the <async>
scope, we simulate asynchronous processing by introducing a delay using the <set-property>
element.
Mulesoft Training Demo Day 1 Video:
Conclusion:
Unogeeks is the No.1 Training Institute for Mulesoft Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Mulesoft Training here – Mulesoft Blogs
You can check out our Best in Class Mulesoft Training details here – Mulesoft Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks