The Scope of Flow Variables and Session Variables in Async Flow in Mulesoft
In this article, we will see how these variables are processed in an async scope of a flow.
Join the DZone community and get the full member experience.
Join For FreeMule variables are used to store values for use in a flow or mule app. The values that can be stored in variables are objects, strings, numbers, messages, message attributes, message payloads, and dataweave expressions.
Mule 3 has 3 different types of variables:
Flow variable
Session variable
Record variable
However, in Mule 4, the record variable is not there.
The scope of a flow variable is within a flow or subflow until there is no transport barrier like HTTP, VM, etc. Session variable scope is throughout the app even though there is a transport barrier.
In this article, we will see how these variables are processed in an async scope of a flow.
If you add an Async Flow in parent flow, the async flow runs in parallel along with the parent flow. The flow variable defined in the parent flow is passed to Async Flow and beyond it to the remaining flow. However, the changes in the variable inside the Async flow are not applicable or visible to the flow outside of the Async flow.
Async Flows don't send any response to parent flow. Let us see this in the example below:
In the example, the parent flow named AsyncExampleFlow calls the SubFlow named AsyncSubFlow using a flow reference component where the variable named number is set with value two.
From subflow (AsyncSubFlow) through VM connector, Flow2 is getting called since the flow variable crossed the transport barrier and the value of the number variable set in subflow is not applicable to Flow2.
The variable number is not accessible in the first logger in Flow2.
In Flow2, the number variable value is set as three. In the log, the value is printed as three from the second logger.
Once the subflow execution completes from parent flow, the async scope flow gets executed along with the parent flow. Even though inside the async flow the number variable value is set as four, the response we get through the payload is two. Since the value of the variable is set as two in the subflow, the same is returned to the parent flow.
The async scope does not return the response of flow variable to the parent flow as it is also executing along with the parent flow.
In the above example, instead of flow variable, if we use a session variable, the response will be three since the session variable scope is available outside of the transport barrier as well.
I hope you now understand flow variables in async flows a bit better. Please leave your feedback.
Opinions expressed by DZone contributors are their own.
Comments