You are viewing an older version of this section. Click here to navigate to the latest version.

Session Variable Transformer Reference

Use a session variable 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. By contrast, variables specified through the variable transformer persist only as long as the message with which they are associated remains within the same flow. To learn more about message scopes, refer to Mule Concepts.

The session variable transformer differs from the variable transformer and the property transformer. See the table below for a comparison of these three transformers.

Session Variable Transformer Variable Transformer Property Transformer


Use to set or remove a variable that is tied to the current message for its entire lifecycle, across multiple flows, applications, and even servers.

Use to set or remove a variable on the message, tied to the current flow.

Use to set, remove, or copy properties on the outbound scope of a message.


Session variables set with a session variable transformer persist for the entire message lifecycle, regardless of transport barriers, except for the HTTP Connector which doesn’t propagate them.

Variables set with a variable transformer persist only for the current flow and cannot cross the transport barrier.

Once a message hits an outbound-connector, all properties in the outbound scope are sent with the message in the form of transport-specific metadata ( HTTP headers for an HTTP outbound-connector, for example).

Once you have set a session variable, you can invoke it using the sessionVars map in a Mule expression. For example, if you have set a session variable with the name “SVname” and the value “SVvalue”, you can later invoke that session variable using the expression #[sessionVars.SVname], which evaluates to SVvalue.

Note that the session variable is linked to a Mule Message, but not to a MuleEvent. This means that if you make use of an element that makes copies of the message – such as an async scope or a scatter gather component – changes in flow variables won’t be persisted in between these replicated messages, even though they started as one.



Field Value Description XML

Display Name

Session Variable

Customize to display a unique name for the transformer in your application.

doc:name="Session Variable"


Set Session Variable

Select to set a new session variable on your message (as shown in example screenshot above).


Remove Session Variable

Select to delete an existing session variable from your message.



String or Mule Expression

Specify the name for the session variable that you are creating, or identify the name of the session variable that you are removing. If you are removing session variables, this field accepts a wildcard "*" character.



String or Mule Expression

Mule displays this field only if you are setting a new session variable. Specify the value using either a string or a Mule expression.


# Set session variable
&lt;set-session-variable variableName="MyNewSessionVariableName" value="MyNewSessionVariableValue" doc:name="Session Variable"/&gt;
# Remove session variable
&lt;remove-session-variable variableName="NameofSessionVariabletoRemove" doc:name="Session Variable"/&gt;
Element Description


Set a new variable on your message (as shown in example above).


Delete an existing variable from your message.

Element Attribute Description


Customize to display a unique name for the transformer in your application.

Note: Attribute not required in Mule Standalone configuration.


The name of the session variable that you are setting or removing. Can be a string or a Mule expression.

Note: If you are using the remove-session-variable element, you may use a wildcard "" character. For example, a remove-session-variable transformer with the element variableName="http." will remove all variables whose names begin with "http." from the message.


The value of the session variable that you are setting. This attribute is only relevant for the set-session-variable element. Can be a string or a Mule expression.


In many cases, messages travel across multiple flows, applications and even servers. In such cases, it can be useful to attach some kind of metadata to a message so as to track it from its origin to its final destination across all systems. In the example below, we set a session variable named messageID. This messageID contains a unique identifier that persists on the message over its entire lifecycle (unless it is explicitly removed by another session variable transformer).


&lt;http:listener-config name="listener-config" host="localhost" port="8081"/&gt;
&lt;flow name="SessionVariableTransformingFlow" doc:name="SessionVariableTransformingFlow"&gt;
   &lt;http:listener config-ref="listener-config" path="/" doc:name="HTTP Connector"/&gt;
   &lt;set-session-variable variableName="messageID" value="#[java.util.UUID.randomUUID().toString()]" doc:name="Set Message ID"/&gt;
   &lt;logger message="Variable set with #[sessionVars.messageID]" level="INFO"/&gt;
   &lt;remove-session-variable "NameofSessionVariabletoRemove" doc:name="Session Variable"/&gt;

Complete Code Example

 View namespace

<http:listener-config name="listener-config" host="localhost" port="8081"/>
<flow name="SessionVariableTransformingFlow" doc:name="SessionVariableTransformingFlow">
   <http:listener config-ref="listener-config" path="/" doc:name="HTTP Connector"/>
   <set-session-variable variableName="messageID" value="#[java.util.UUID.randomUUID().toString()]" doc:name="Set Message ID"/>
   <logger message="Variable set with #[sessionVars.messageID]" level="INFO"/>
   <remove-session-variable "NameofSessionVariabletoRemove" doc:name="Session Variable"/>

See Also

  • Refer to Mule Concepts to learn more about message scopes.

  • Read about related transformers, the variable transformer and the properties transformer, which you can use to set properties and variables for different scopes.

  • Learn how to use Mule Expression Language to read session variables using the sessionVars map.