Session Variable in Mule with Basic Example
Session variable transformer is used to set or remove a variable that is tied to a message for its entire lifecycle, potentially across multiple flows, applications, and even servers. Session variables can be easily propagated from one flow to another through the VM transport, or a flow reference, but not through the HTTP Connector.
flow of session variable :
Session variable is accessible in main flow as well as in sub flow by : #[sessionVars.variableName]
URL : http://localhost:8085/api/session
Method : POST
Input :
Logger Output :
XML project code :
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" 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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8085" basePath="/api" doc:name="HTTP Listener Configuration"/>
<flow name="test_sessionvarFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/session" doc:name="HTTP"/>
<object-to-string-transformer doc:name="Object to String"/>
<set-session-variable variableName="var1" value="#[payload]" doc:name="Session Variable"/>
<logger message="In Main Flow : #[sessionVars.var1]" level="INFO" doc:name="Logger"/>
<flow-ref name="test_sessionvarSub_Flow" doc:name="test_sessionvarSub_Flow"/>
</flow>
<sub-flow name="test_sessionvarSub_Flow">
<logger message="In Sub Flow : #[sessionVars.var1]" level="INFO" doc:name="Logger"/>
</sub-flow>
</mule>
No comments:
Post a Comment