In this article we will look into mule’s session scoped property. Session scoped property can be accessed across multiple flows.
Add session scoped property
In the below mule context, we create a session scoped property using <message-properties-transformer>
, with scope set to session. Message is then sent to an outbound endpoint which takes it to a different flow. We access the session scoped property in the second flow and print it. When the flow returns back from the outbound endpoint, back to the first flow, we print the session scoped property once again and as expected it is still accessible.
Life of a session scoped property
Session scoped properties can be accessed across flows so when a message traverses thru flows, the session values too are passed from invocation to invocation.
muleContext.xml:
<?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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:test="http://www.mulesoft.org/schema/mule/test" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/current/mule-test.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.8/mule-vm.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.8/mule.xsd "> <flow name="flow1"> <vm:inbound-endpoint path="in" exchange-pattern="request-response" /> <message-properties-transformer scope="session"> <add-message-property key="session_property" value="Session Example" /> </message-properties-transformer> <vm:outbound-endpoint path="out_res" exchange-pattern="request-response" /> <custom-transformer class="com.javarticles.mule.PrintSessionProperty"> <spring:property name="property" value="session_property" /> </custom-transformer> </flow> <flow name="flow2"> <vm:inbound-endpoint path="out_res" exchange-pattern="request-response" /> <custom-transformer class="com.javarticles.mule.PrintSessionProperty"> <spring:property name="property" value="session_property" /> </custom-transformer> </flow> </mule>
Access session scoped property
Here is the component that prints the session scoped property.
PrintSessionProperty:
package com.javarticles.mule; import org.mule.api.MuleEvent; import org.mule.api.MuleException; import org.mule.api.processor.MessageProcessor; public class PrintSessionProperty implements MessageProcessor { private String property; public String getProperty() { return property; } public void setProperty(String property) { this.property = property; } public MuleEvent process(MuleEvent event) throws MuleException { Object sessionProperty = event.getSession().getProperty(property); System.out.println(getProperty() + ": "+ sessionProperty); return event; } }
Mule session scoped property example
We now send a message to the inbound endpoint to trigger the flow. After we receive the response, we verify whether the session scoped property still exists.
MuleSessionScopeExample:
package com.javarticles.mule; import org.mule.api.MuleContext; import org.mule.api.MuleMessage; import org.mule.api.context.MuleContextBuilder; import org.mule.api.context.MuleContextFactory; import org.mule.config.DefaultMuleConfiguration; import org.mule.config.spring.SpringXmlConfigurationBuilder; import org.mule.context.DefaultMuleContextBuilder; import org.mule.context.DefaultMuleContextFactory; import org.mule.module.client.MuleClient; public class MuleSessionScopeExample { public static void main(String[] args) throws Exception { DefaultMuleConfiguration dmc = new DefaultMuleConfiguration(); dmc.setId("muleexample"); dmc.setWorkingDirectory("/esb/mule"); SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder( "muleContext.xml"); MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder(); contextBuilder.setMuleConfiguration(dmc); MuleContextFactory contextFactory = new DefaultMuleContextFactory(); MuleContext ctx = contextFactory.createMuleContext(configBuilder, contextBuilder); ctx.start(); try { MuleClient muleClient = new MuleClient(ctx); sendMsgAndVerifyProperties("vm://in", ctx, muleClient); } finally { ctx.dispose(); } } private static void sendMsgAndVerifyProperties(String endpointUri, MuleContext ctx, MuleClient muleClient) throws Exception { System.out.println("send message to " + endpointUri); MuleMessage response = muleClient.send(endpointUri, "Hello", null); printInboundProperties(response); } private static void printInboundProperties(MuleMessage response) throws Exception { System.out.println("Verify properties after response"); System.out.println("Response: " + response.getPayloadAsString()); System.out.println("session_property: " + response.getSessionProperty("session_property")); } }
You can see from the output the session scoped parameter is accessible in both the flows and in the response too.
Output:
send message to vm://in session_property: Session Example session_property: Session Example Verify properties after response Response: Hello session_property: Session Example
Download the source code
This was an example about mule session scoped properties.