Understanding Anypoint Flows and Sub-Flows With MuleSoft
Sub-flows are helpful whether you want to process messages synchronously or asynchronously. They are useful when it comes to code re-use, breaking down flows, and more.
Join the DZone community and get the full member experience.
Join For FreeA Mule application basically contains one or more flows. Mule flows start processing a message when it is received by inbound endpoints. This flow either contains all the processing stages or routes the message to other flows or sub-flows to perform a specific task. A Mule flow can be either a synchronous or asynchronous flow. The sub-flow is always synchronous.
Advantages of Mule Flow
Asynchronously triggered flow can perform time-consuming tasks, such as writing bulk data to a database or emailing a message without stalling the execution of triggering flow. Triggering flows and asynchronously triggered flows can both be executed in parallel.
You can break up complex flows into various flows or sub-flows that are easy to read.
Processing actions in a flows or sub-flows can be called and used by multiple flows in an application. You can achieve code reusability.
Sub-Flows
Sub-flows process messages synchronously and inherit the processing as well as exception strategy from triggering flows. During sub-flow running, processing on triggering flows stalls until the sub-flow completes the processing and hands over the message to the triggering flow.
Sub-flows are best-suited for code reuse, so you can write the block of code once within a sub-flow and it can be triggered by any flow within the same application.
You are breaking a flow into various sub-flows that make the GUI view intuitive and XML easy to read.
Sub-flows inherit processing as well as exception strategies from the triggering flow, so you don't have to implement processing and exception strategies for sub-flows.
<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
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: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/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">
<flow name="apdev-examplesFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/hello" doc:name="HTTP"/>
<flow-ref name="subflow1" doc:name="subflow1"/>
<flow-ref name="subflow2" doc:name="subflow2"/>
<logger level="INFO" doc:name="Logger" message="Name: #[message.outboundProperties.qpname] Type: #[flowVars.qptype] Color: #[sessionVars.color]"/>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<set-payload value="Error while processing the message" doc:name="Set Payload"/>
</catch-exception-strategy>
</flow>
<sub-flow name="subflow1">
<set-payload value="#['Hello World'.toUpperCase()]" doc:name="Set Payload"/>
<set-property propertyName="qpname" value="#[message.inboundProperties.'http.query.params'.name]" doc:name="Property"/>
</sub-flow>
<sub-flow name="subflow2">
<set-variable variableName="qptype" value="#[message.inboundProperties.'http.query.params'.type]" doc:name="Variable"/>
<set-session-variable variableName="color" value="gray" doc:name="Session Variable"/>
</sub-flow>
</mule>
Synchronous Flow
Synchronous flows are similar to sub-flows. During triggered synchronous flow running, processing on triggering flow stalls until the triggered flow completes the processing and hands over the message to the triggering flow.
Synchronous flows don't inherit the processing and exception strategy from the triggering flow. This type of flow processes the message within a single thread.
Flow references can be used to invoke other flows:
<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:file="http://www.mulesoft.org/schema/mule/file"
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: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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
<flow name="flowexampleFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/GET" doc:name="HTTP"/>
<logger level="INFO" doc:name="Logger"/>
<flow-ref name="flowexampleFlow1" doc:name="FlowReference"/>
</flow>
<flow name="flowexampleFlow1">
<set-variable variableName="code" value="#[message.inboundProperties.'http.query.params'.code]" doc:name="Variable"/>
<file:outbound-endpoint path="src/test/resources" responseTimeout="10000" doc:name="File"/>
</flow>
</mule>
Asynchronous Flow
Asynchronous flows process the message in parallel to the triggered and triggering flow. The flow passes the message to the asynchronous flow, thus triggering this flow. At the same time, the triggering flow sends a copy of the message to another processor of its own flow.
So, triggering and triggered flows execute simultaneously and independently of each other. Asynchronous flows don't inherit the processing and exception strategy from the triggering flow. This type of flow processes the message within multiple threads.
For asynchronous flows, the flow reference must be wrapped with async scope in the triggering flow.
<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:file="http://www.mulesoft.org/schema/mule/file"
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: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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
<flow name="flowexampleFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/GET" doc:name="HTTP"/>
<logger level="INFO" doc:name="Logger"/>
<async doc:name="Async">
<flow-ref name="flowexampleFlow1" doc:name="FlowReference"/>
</async>
</flow>
<flow name="flowexampleFlow1">
<set-payload doc:name="Set Payload"/>
<file:outbound-endpoint path="src/test/resources" responseTimeout="10000" doc:name="File"/>
</flow>
</mule>
I hope that this article clarifies any confusion that you may have had with Mule flows and sub-flows.
Published at DZone with permission of Jitendra Bafna, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments