Variables in Mule 3
In Mule 3, there are three variables: flow variables, session variables, and record variables.
Join the DZone community and get the full member experience.
Join For FreeVariables are used to store values for use within a Mule flow in a Mule application. Variables can store a current message, current message payload, or current message attributes.
In Mule 3, there are 3 types of variables:
- Flow Variables
- Session Variables
- Record Variables
In the later part of this article, I also discuss Property Transformer, which provides outbound properties (outbound scoped variables) for the message.
Flow Variables
Flow Variables are used to set or remove variables tied to a message in a current flow. Flow variables are set or removed using a Variable transformer and cannot cross the transport barriers.
Flow Variables can pass from one Flow to another only when using a flow reference component.
Syntax to access flow variable is :#[flowVars.Code], where Code is the name of the flow variable.
Basically, flow.Vars reference is optional. You can also access flow variables using:
#[Code], where Code is the name of the variable.
Flow Variable Example Using Flow Reference:
Make a GET request to the following URL using Postman or ARC for the following Mule application:
http://localhost:8081/variable?Name=kuldeep&Code=red
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="flow1">
<http:listener config-ref="HTTP_Listener_Configuration" path="/variable" allowedMethods="GET" doc:name="HTTP"/>
<set-payload value="Hello Flow1" doc:name="Set Payload"/>
<set-variable variableName="fname" value="#[message.inboundProperties.'http.query.params'.Name]" doc:name="Variable"/>
<flow-ref name="flow2" doc:name="flow2"/>
<logger message="Payload: #[payload] FlowVariable: #[flowVars.fname]" level="INFO" doc:name="Logger"/>
</flow>
<flow name="flow2">
<set-payload value="Hello Flow2" doc:name="Set Payload"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
Session Variables
Session Variables are used to set or remove variables tied to a message for a complete lifecycle across multiple flows. Session Variables are set or removed using Session Variable transformer for the entire lifecycle of the Mule application.
Session variables cannot cross all transport barriers, but in some cases, they can cross barriers and become accessible in another flow.
Example: Session Variables cannot cross HTTP CONNECTOR but they can cross VM CONNECTOR.
Session Variables can be accessed using the syntax: #[sessionVars.Code], where Code is the name of the variable.
Session Variable Example Using VM Connector:
Make a GET request to the following URL, using Postman or ARC for the following Mule application:
http://localhost:8081/variable?Name=kuldeep&Code=red
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="flow1">
<http:listener config-ref="HTTP_Listener_Configuration" path="/variable" allowedMethods="GET" doc:name="HTTP"/>
<set-payload value="Hello Flow1" doc:name="Set Payload"/>
<set-session-variable variableName="sname" value="#[message.inboundProperties.'http.query.params'.Code]" doc:name="Session Variable"/>
<vm:outbound-endpoint exchange-pattern="request-response" path="vmflow" doc:name="VM"/>
<logger message="Payload: #[payload] SessionVariable: #[sessionVars.sname]" level="INFO" doc:name="Logger"/>
</flow>
<flow name="flow2">
<vm:inbound-endpoint exchange-pattern="one-way" path="vmflow" doc:name="VM"/>
<set-payload value="Hello Flow2" doc:name="Set Payload"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
Record Variable
Record Variables, unlike any other variable, are special variable sets that are used only inside a Batch Job. Records variables are available or accessed only in the process phase of the Batch job.
They can persist across multiple batch steps but not in the source (Load and Dispatch) or the On Complete phase.
Records Variable are accessed using the syntax: #[recordVars.Code], where Code is the name of Record Variable.
Record Variable Example:
Create two folders in the src/main/resources folder, with the name input and output respectively.
Add the csv file in the input folder with the following data:
ID, NAME, DEPARTMENT, GENDER
KULDEEP, SOFTWARE, MALE
AMAN, FINANCE, MALE
AMIT, SALES, MALE
ASHISH, SOFTWARE, MALE
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:batch="http://www.mulesoft.org/schema/mule/batch" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/batch http://www.mulesoft.org/schema/mule/batch/current/mule-batch.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="8081" doc:name="HTTP Listener Configuration"/>
<batch:job name="variablesBatch">
<batch:input>
<file:inbound-endpoint path="src/main/resources/input" moveToDirectory="src/main/resources/output" responseTimeout="10000" doc:name="File"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload]]></dw:set-payload>
</dw:transform-message>
</batch:input>
<batch:process-records>
<batch:step name="Batch_Step">
<batch:set-record-variable variableName="Rname" value="#[payload.NAME]" doc:name="Record Variable"/>
<logger message="#[recordVars.Rname]" level="INFO" doc:name="Logger"/>
</batch:step>
<batch:step name="Batch_Step1">
<logger message="#[recordVars.Rname]" level="INFO" doc:name="Logger"/>
</batch:step>
</batch:process-records>
<batch:on-complete>
<logger level="INFO" doc:name="Logger"/>
</batch:on-complete>
</batch:job>
</mule>
Run the following code in debug mode. Look for the Record variable and see that it is only visible in the batch steps (batch step and batch step 1) and not in the On complete phase or the load and dispatch phases (starting phase).
Property Transformer
The Property is used to set, remove, or copy the properties on the outbound scope of a message. Once a message hits an outbound connector, all properties in the outbound scope are sent with the HTTP Headers.
Outbound Properties can be accessed using the syntax: #[message.outboundProperties.Code], where Code is the name of the outbound property.
Outbound Properties are available as Outbound Scope Variables. Debug the project in Mule Studio and step after the property transformer. You can see an outbound property under the outbound tab.
Property Transformer Example:
Make a GET request to the following URL, using Postman or ARC for the following Mule application:
http://localhost:8081/variable
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="flow1">
<http:listener config-ref="HTTP_Listener_Configuration" path="/variable" allowedMethods="GET" doc:name="HTTP"/>
<set-payload value="Hello Flow1" doc:name="Set Payload"/>
<set-property propertyName="oname" value="max" doc:name="Property"/>
<flow-ref name="flow2" doc:name="flow2"/>
<logger message="Payload: #[payload] OutboundProperty: #[message.outboundProperties.oname]" level="INFO" doc:name="Logger"/>
</flow>
<flow name="flow2">
<set-payload value="Hello Flow2" doc:name="Set Payload"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
Conclusion
Different variables in Mule have different behaviors and outcomes. Each one is used for a special purpose in a Mule flow and helps the developer store values.
Debug all the examples above and step through each of the components, have a look at their behavior, and see if they can persist to different flows or not.
Change the flow reference component above in the Flow Variable example with an HTTP Connector and see if the results change as described above. Do the same for the Session Variable; change the VM connector with the HTTP Connector.
Thank you!
Opinions expressed by DZone contributors are their own.
Comments