Sub Flow in Mule 4 | Flow Reference in Mule 4

Sub flow in Mule 4 | Flow Reference in Mule 4

Subflow is a scope that enables you to group event processors in a manner similar to that of a flow, but with certain differences and limitations:

1) Subflows do not have event sources.
2) Subflows do not have an error handling scope.
3) During design, sub-flow works as macros that replace the Flow Reference components that call them. When you build the application, all Flow Reference components that call a sub-flow are replaced by the contents of the referenced sub-flow.
4) Referencing sub-flows results in better performance than referencing a flow.

We can connect main flow with sub-flow in mule 4 via flow reference component.

Example:


Method: GET
Output:

XML project code:

<?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="a4e501b9-6daa-4b7e-b0d5-af8f71e4538d" >
              <http:listener-connection host="0.0.0.0" port="8085" />
       </http:listener-config>
       <flow name="test_subflowFlow" doc:id="1a446e09-90cc-49a2-9ce7-b90bddedb3db" >
              <http:listener doc:name="Listener" doc:id="3d4b5b66-8d9b-4742-b6aa-998272ea1905" config-ref="HTTP_Listener_config" path="/ref"/>
              <set-variable value="1" doc:name="Set Variable" doc:id="33ba4c59-98fd-44bb-a2ad-1847506b4ab0" variableName="var1"/>
              <flow-ref doc:name="Flow Reference" doc:id="f12a2f26-411d-4819-ae5d-7748d744a562" name="test_subflowSub_Flow"/>
       </flow>
       <sub-flow name="test_subflowSub_Flow" doc:id="3cdc536a-c92b-4217-88be-b58e224c6824" >
              <logger level="INFO" doc:name="Logger" doc:id="a6287bb5-0ee2-42cd-9107-e2593e2f4503" message="#[vars.var1]"/>
       </sub-flow>
</mule>