DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • MuleSoft Integration With RabbitMQ
  • How to Use Mulesoft VM Connector
  • Step-by-Step Guide to Use Anypoint MQ: Part 1
  • Migrate Mule 3 to Mule 4 Using MMA (Mule Migration Assistant)

Trending

  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • Automatic Code Transformation With OpenRewrite
  • Integration Isn’t a Task — It’s an Architectural Discipline
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer

Variables in Mule 3

In Mule 3, there are three variables: flow variables, session variables, and record variables.

By 
Kuldeep Rana user avatar
Kuldeep Rana
·
Jul. 16, 19 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
37.0K Views

Join the DZone community and get the full member experience.

Join For Free

Variables 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:

  1. Flow Variables
  2. Session Variables
  3. 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

Image title

<?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

Image title

<?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

  1. KULDEEP, SOFTWARE, MALE

  2. AMAN, FINANCE, MALE

  3. AMIT, SALES, MALE

  4. ASHISH, SOFTWARE, MALE

Image title

<?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

Image title

<?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!

Flow (web browser) Property (programming) Session (web analytics) Record (computer science) application Connector (mathematics) Arc (programming language) Input and output (medicine) Syntax (programming languages)

Opinions expressed by DZone contributors are their own.

Related

  • MuleSoft Integration With RabbitMQ
  • How to Use Mulesoft VM Connector
  • Step-by-Step Guide to Use Anypoint MQ: Part 1
  • Migrate Mule 3 to Mule 4 Using MMA (Mule Migration Assistant)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!