Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

MuleSoft's Scope of Session and Flow Variables

DZone's Guide to

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.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

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.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
mulesoft ,variables ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}