Flow Variables in Mule
Use a variable transformer to set or remove a variable on the message. The scope of this "flow variable" is limited to the flow where it is set, and accessed in Mule Expression Language by the identifier flowVars. For example, access an integer myInt using #[flowVars.myInt].
Flow Scope versus Session Scope :
When the message leaves the flow, the flow variable, set by the Variable Transformer discussed here, does not carry over to the next flow or application.
By contrast, session variables specified using the Session Variable Transformer persist as long as the message continues to be processed within your Mule application, regardless of flow. To learn more about message scopes, refer to Mule Concepts.
Flow variables are used to store any information in it.Its like key-value pair,name of flow variable is key and value is stored in value attribute.
Flow Scope versus Session Scope :
When the message leaves the flow, the flow variable, set by the Variable Transformer discussed here, does not carry over to the next flow or application.
By contrast, session variables specified using the Session Variable Transformer persist as long as the message continues to be processed within your Mule application, regardless of flow. To learn more about message scopes, refer to Mule Concepts.
Flow variables are used to store any information in it.Its like key-value pair,name of flow variable is key and value is stored in value attribute.
flow variables cannot be accessible in another HTTP flow even not by VM connector flow.
flow variable is accessible by : #[flowVars.variablename]
In dataweave flow variable is accessible by : flowVars.variablename
Here is basic flow for flow variable and here we can see flow variable is accessible in subflow and again in main flow when control goes back to main flow. It is also accessible in transform message component as mentioned in code below.
URL : http://localhost:8085/api/flowvar
Method : GET
Output:
XML Project code :
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" 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/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8085" basePath="/api" doc:name="HTTP Listener Configuration"/>
<flow name="testflowvarFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/flowvar" doc:name="HTTP"/>
<set-variable variableName="demovar" value="Himanshu" doc:name="Variable"/>
<logger message="Value of flow var is : #[flowVars.demovar]" level="INFO" doc:name="Logger"/>
<flow-ref name="testflowvarSub_Flow" doc:name="testflowvarSub_Flow"/>
<logger message="#[flowVars.demovar]" level="INFO" doc:name="Logger"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
var1 : flowVars.demovar
}]]></dw:set-payload>
</dw:transform-message>
<logger message="Dataweave output is : #[payload]" level="INFO" doc:name="Logger"/>
<set-payload value="#[payload]" doc:name="Set Payload"/>
</flow>
<sub-flow name="testflowvarSub_Flow">
<logger message="Value of flow variable in subflow is : #[flowVars.demovar]" level="INFO" doc:name="Logger"/>
</sub-flow>
</mule>