Wednesday, May 3, 2017

MuleSoft's Scope of Session and Flow Variables

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

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 logging to print out flow and session variables in both the request and response scope of flow A and B.

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.

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 on the accessible variables in each response and request scope are as depicted in the following 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.

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 it's behavior. There is no better substitute to 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.

5 comments: