Contact Us 1-800-596-4880

Session Variable Transformer Reference

Use a session variable to set or remove a variable that is tied to a message for its entire lifecycle, across multiple flows, applications, and even servers. (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

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.

Persistence

Session variables set with a session variable transformer persist for the entire message lifecycle, regardless of transport barriers.

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

Once a message hits an outbound-endpoint, all properties in the outbound scope are sent with the message in the form of transport-specific metadata ( HTTP headers for an HTTP outbound-endpoint, 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.

Configuration

STUDIO Visual Editor

Studio_SessionVariablePPP
Field Value Description XML

Display Name

Session Variable

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

doc:name="Session Variable"

Operation

Set Session Variable

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

<set-session-variable>

Remove Session Variable

Select to delete an existing session variable from your message.

<remove-session-variable>

Name

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.

variableName="MyNewSessionVariableName"

Value

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.

value="MyNewSessionVariableValue"

XML Editor or Standalone

# Set session variable

<set-session-variable variableName="MyNewSessionVariableName" value="MyNewSessionVariableValue" doc:name="Session Variable"/>


# Remove session variable
<remove-session-variable variableName="NameofSessionVariabletoRemove" doc:name="Session Variable"/>
Element Description

set-session-variable

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

remove-session-variable

Delete an existing variable from your message.

Element Attribute Description

doc:name

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

Note: Attribute not required in Mule Standalone configuration.

variableName

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 _variable_Name="http.*" will remove all variables whose names begin with "http." from the message.

value

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.

Example

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

STUDIO Visual Editor

Studio_SessionVariable_ex

XML Editor or Standalone

<flow name="SessionVariableTransformingFlow" doc:name="SessionVariableTransformingFlow">
   <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
   <set-session-variable variableName="messageID" value="#[java.util.UUID.randomUUID().toString()]" doc:name="Set Message ID"/>
</flow>

Complete Code Example

<flow name="SessionVariableTransformingFlow" doc:name="SessionVariableTransformingFlow">
   <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
   <set-session-variable variableName="messageID" value="#[java.util.UUID.randomUUID().toString()]" doc:name="Set Message ID"/>
   <remove-session-variable "NameofSessionVariabletoRemove" doc:name="Session Variable"/>
</flow>

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.