Contact Us 1-800-596-4880

Flow Reference (<flow-ref/>)

Routes the Mule event to another flow or subflow, executes all processors in the referenced flow, and then routes the event back within the same Mule application.

This process enables you to treat the entire referenced flow like a single component in your current flow.

Flow Reference breaks the Mule application into discrete and reusable units. For example, a flow that lists files on a regular basis might reference another flow that processes the output of the List operation. Instead of appending all the processing steps to the flow that lists the files, you can append a Flow Reference that points to the processing flow.

Use actual flow names to define the Flow name (name) attribute. Using dynamic values (DataWeave expressions, variables) to specify the referenced flow negatively affects performance. Also, MUnit tests and application analysis tools cannot work on dynamic references.

Component XML

This component supports the following XML structure:

<flow-ref name="anotherflow" />

Flow Reference (<flow-ref/>) attributes are configurable through the UI and XML.

Attribute Name Attribute XML Description

Name

name

The name of the flow that this component references.

N/A

doc:id

Automatically generated identifier for the component.

Target Variable

target

Name of the variable in which you want to store message data. Names can only include numbers, characters, and underscores. For example, hyphens are not allowed in the name. See Enrich Data with Target Variables in the Mule documentation.

Target Value

targetValue

Value of the data to store in the target variable. By default, the value is the message payload (payload). The field accepts any value that a variable accepts: any supported data type, DataWeave expressions, the keywords payload, attributes, and message, but not the keyword vars. See Enrich Data with Target Variables in the Mule documentation.

Considerations when Using a Target Variable

Setting the target variable in a Flow Reference component causes the original message to remain unchanged. This means that any modification to the payload or to the variables that occur in the referenced flow revert after the referenced flow finishes executing its processors, returning the payload and variables to their original values.

Alternatively, if you do not define a target variable, any modification that occurs to the payload or to the variables in the referenced flow persist after the referenced flow finishes its execution, changing the original value of the payload or variables.

Examples

The following example configures an HTTP listener, updates the payload, sets a variable, and references another flow by adding the target variable flowReferenceVar, which stores the result of the secondary flow called by the Flow Reference component. This variable is then used in the main flow by logging vars.flowReferenceVar:

<!-- Main flow -->
<flow name="mainFlow" >
  <http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="test"/>
  <set-payload value="Original payload" doc:name="Set Payload"  />
  <set-variable value="1" doc:name="Set myVar=1" variableName="myVar"/>
  <!-- Flow Reference component with target variable flowReferenceVar -->
  <flow-ref doc:name="secondaryFlow" name="secondaryFlow" target="flowReferenceVar"/>
  <logger level="INFO" doc:name="Log payload" message='#["Payload = " ++ payload]'/>
  <logger level="INFO" doc:name="Log myVar" message='#["myVar = " ++ vars.myVar]'/>
  <logger level="INFO" doc:name="Log flowReferenceVar" message='#["flowReferenceVar = " ++ vars.flowReferenceVar]'/>
</flow>
<!-- Secondary flow that is referenced from Main flow-->
<flow name="secondaryFlow" doc:id="044ece19-aa71-4fc9-ad34-8df655dd59a8" >
  <set-payload value="Modified payload" doc:name="Update Payload" />
  <set-variable value="2" doc:name="Update myVar=2" variableName="myVar"/>
</flow>

The following log represents the output of the example application after its execution:

INFO  LoggerMessageProcessor: Payload = Original payload
INFO  LoggerMessageProcessor: myVar =  1
INFO  LoggerMessageProcessor: flowReferenceVar =  Modified payload

If you remove target="flowReferenceVar" from the Flow Reference component, the application returns the following output:

INFO  LoggerMessageProcessor: Payload = Modified payload
INFO  LoggerMessageProcessor: myVar = 2
INFO  LoggerMessageProcessor: flowReferenceVar = null

The logs are shortened to improve readability.

Error Handling

When an error occurs inside a flow that is referenced by a Flow Reference component, the execution of the referenced flow stops and the process transfers to the referenced flow’s error handler. If the referenced flow does not have a configured error handler, the error is propagated to the flow that contains the Flow Reference call so this flow’s configured Error Handler component processes the error.

Target variables are not set when an error occurs because the execution of the flow stops and does not end successfully.