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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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

MuleSoft's Scope of Session and Flow Variables

Comprehending session and flow variables is essential to understand MuleSoft development and integration. Read on for a brief exploration of these concepts.

Kian Ting user avatar by
Kian Ting
CORE ·
May. 08, 17 · Opinion
Like (16)
Save
Tweet
Share
17.64K Views

Join the DZone community and get the full member experience.

Join For Free

1.0 Overview

We need to know the life of session variables (sessionVars) and flow variables (flowVars) in MuleSoft. If you are like me, aiming to get the "MuleSoft Certified Developer - Integration Professional," the concept of variable scopes needs to be clear in your mind. Some of the questions you might have would be like the following.

1.1 The Question That Tests Our Understanding

Based on Figure 1.0 below, what flow and session variable is accessible in the response scope of "Flow A" and "Flow B?"

Figure 1.0

Figure 1.0

2.0 Crystalizing Variable Scope Behaviours

In order to find out, we could create a response scope in both flow A & B, we could modify it to be like Figure 2.0. I have put in 4 loggers to print out flow and session variables in both the request and response scope of flow A and B.

Figure 2.0

Figure 2.0

I have keyed in the following MEL expressing in the loggers:-

FLOW <A or B> <Request or Response> Scope Accessible Variables: #[flowVars.flowVarriable1] #[sessionVars.SessionVariable1] #[flowVars.flowVarriable2] #[sessionVars.SessionVariable2]

2.1 The Sample Code to Exemplify the Behaviors

These loggers will print out the values of the variables in their respective places. The full mule XML code for this running test program is as per the following:

<?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" />
<vm:connector name="VM" validateConnections="true" doc:name="VM" />
<flow name="FlowA">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP" />
<set-variable variableName="flowVarriable1" value="F1" doc:name="flowVarriable1" />
<set-session-variable variableName="SessionVariable1" value="S1" doc:name="SessionVariable1" />
<logger
message="FLOW A Request Scope Accessible Variables: #[flowVars.flowVarriable1] #[sessionVars.SessionVariable1] #[flowVars.flowVarriable2] #[sessionVars.SessionVariable2]"
level="INFO" doc:name="Logger" />
<vm:outbound-endpoint exchange-pattern="request-response" path="testQ" connector-ref="VM" doc:name="VM" />
<response>
<logger
message="FLOW A Response Scope Accessible Variables: #[flowVars.flowVarriable1] #[sessionVars.SessionVariable1] #[flowVars.flowVarriable2] #[sessionVars.SessionVariable2]"
level="INFO" doc:name="Logger" />
</response>
</flow>
<flow name="FlowB">
<vm:inbound-endpoint exchange-pattern="request-response" path="testQ" connector-ref="VM" doc:name="VM" />
<set-variable variableName="flowVarriable2" value="F2" doc:name="flowVarriable2" />
<set-session-variable variableName="SessionVariable2" value="S2" doc:name="SessionVariable2" />
<logger
message="FLOW B Request Scope Accessible Variables: #[flowVars.flowVarriable1] #[sessionVars.SessionVariable1] #[flowVars.flowVarriable2] #[sessionVars.SessionVariable2]"
level="INFO" doc:name="Logger" />
<response>
<logger
message="FLOW B Response Scope Accessible Variables: #[flowVars.flowVarriable1] #[sessionVars.SessionVariable1] #[flowVars.flowVarriable2] #[sessionVars.SessionVariable2]"
level="INFO" doc:name="Logger" />
</response>
</flow>
</mule>

You can trigger the mule flow via Postman, by pointing to the test URI, when you click send on postman as per Figure 3.0.

Figure 3.0

Figure 3.0

2.2 Result of Code Run

If you run the Mule application the following log entries would be printed.

LoggerMessageProcessor: FLOW A Request Scope Accessible Variables: F1 S1 null null
LoggerMessageProcessor: FLOW B Request Scope Accessible Variables: null S1 F2 S2
LoggerMessageProcessor: FLOW B Response Scope Accessible Variables: null S1 F2 S2
LoggerMessageProcessor: FLOW A Response Scope Accessible Variables: F1 S1 null S2

3.0 Conclusion

A graph representation of the accessible variables in each response and request scope are as depicted in the following Figure 4.0.

Figure 4.0

Figure 4.0

To simplify this, look at the following depiction (Figure 5.0). Notice the response scope of "Flow A", the S2 variable is accessible by "Flow A" once "Flow B" returns the control to the calling flow.

Figure 5.0

Figure 5.0

From this behavior, we can conclude that as long as a session variable is created, it can traverse transport and be accessible by the calling flow and vice versa. With this new understanding, you could answer certification examination questions confidently, because we can’t dispute the results of our test. As always if you want to understand a core concept in Mule or another technology it is always good to devise a lap or a program to test out its behavior. There is no substitute for deliberate practice in gaining new experience.

I have included some references URL in this article; you could read up more about Session and Flow variables via those links.

Flow (web browser) Session (web analytics)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Load Balancing Pattern
  • How Observability Is Redefining Developer Roles
  • OpenID Connect Flows
  • Iptables Basic Commands for Novice

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: