Mule session scoped property


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.

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="" xmlns=""
	xmlns:xsi="" xmlns:vm=""
	xmlns:spring="" xmlns:test=""
	<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" />
		<vm:outbound-endpoint path="out_res"
			exchange-pattern="request-response" />
		<custom-transformer class="com.javarticles.mule.PrintSessionProperty">
			<spring:property name="property" value="session_property" />

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


Access session scoped property

Here is the component that prints the session scoped property.


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) { = 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.


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();
        SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder(
        MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
        MuleContextFactory contextFactory = new DefaultMuleContextFactory();
        MuleContext ctx = contextFactory.createMuleContext(configBuilder,
        try {
            MuleClient muleClient = new MuleClient(ctx);
            sendMsgAndVerifyProperties("vm://in", ctx, muleClient);           
        } finally {
    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);   
    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.


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.

You can download the source code here:

Comments are closed.