org.springframework.messaging.simp

## Class SimpAttributes

- java.lang.Object
- - org.springframework.messaging.simp.SimpAttributes

    public class SimpAttributes
    extends Object

A wrapper class for access to attributes associated with a SiMP session (e.g. WebSocket session).

Since:
  4.1
Author:
  Rossen Stoyanchev

### Field Summary

Fields
`static String` `DESTRUCTION_CALLBACK_NAME_PREFIX` Prefix for the name of session attributes used to store destruction callbacks.
`static String` `SESSION_COMPLETED_NAME` Key set after the session is completed.
`static String` `SESSION_MUTEX_NAME` Key for the mutex session attribute.

### Constructor Summary

Constructors
Constructor and Description
`SimpAttributes(String sessionId, Map<String,Object> attributes)` Constructor wrapping the given session attributes map.

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`static SimpAttributes` `fromMessage(Message<?> message)` Extract the SiMP session attributes from the given message and wrap them in a `SimpAttributes` instance.
`Object` `getAttribute(String name)` Return the value for the attribute of the given name, if any.
`String[]` `getAttributeNames()` Retrieve the names of all attributes.
`String` `getSessionId()` Return an id for the associated session.
`Object` `getSessionMutex()` Expose the object to synchronize on for the underlying session.
`boolean` `isSessionCompleted()` Whether the `sessionCompleted()` was already invoked.
`void` `registerDestructionCallback(String name, Runnable callback)` Register a callback to execute on destruction of the specified attribute.
`void` `removeAttribute(String name)` Remove the attribute of the given name, if it exists.
`void` `sessionCompleted()` Invoked when the session is completed.
`void` `setAttribute(String name, Object value)` Set the value with the given name replacing an existing value (if any).

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### SESSION_MUTEX_NAME

    public static final String SESSION_MUTEX_NAME

Key for the mutex session attribute.

#### SESSION_COMPLETED_NAME

    public static final String SESSION_COMPLETED_NAME

Key set after the session is completed.

#### DESTRUCTION_CALLBACK_NAME_PREFIX

    public static final String DESTRUCTION_CALLBACK_NAME_PREFIX

Prefix for the name of session attributes used to store destruction callbacks.

### Constructor Detail

#### SimpAttributes

    public SimpAttributes(String sessionId,
                          Map<String,Object> attributes)

Constructor wrapping the given session attributes map.

Parameters:
  `sessionId` - the id of the associated session
  `attributes` - the attributes

### Method Detail

#### getAttribute

    @Nullable
    public Object getAttribute(String name)

Return the value for the attribute of the given name, if any.

Parameters:
  `name` - the name of the attribute
Returns:
  the current attribute value, or `null` if not found

#### setAttribute

    public void setAttribute(String name,
                             Object value)

Set the value with the given name replacing an existing value (if any).

Parameters:
  `name` - the name of the attribute
  `value` - the value for the attribute

#### removeAttribute

    public void removeAttribute(String name)

Remove the attribute of the given name, if it exists.

Also removes the registered destruction callback for the specified attribute, if any. However it does not execute the callback. It is assumed the removed object will continue to be used and destroyed independently at the appropriate time.

Parameters:
  `name` - the name of the attribute

#### getAttributeNames

    public String[] getAttributeNames()

Retrieve the names of all attributes.

Returns:
  the attribute names as String array, never `null`

#### registerDestructionCallback

    public void registerDestructionCallback(String name,
                                            Runnable callback)

Register a callback to execute on destruction of the specified attribute. The callback is executed when the session is closed.

Parameters:
  `name` - the name of the attribute to register the callback for
  `callback` - the destruction callback to be executed

#### getSessionId

    public String getSessionId()

Return an id for the associated session.

Returns:
  the session id as String (never `null`)

#### getSessionMutex

    public Object getSessionMutex()

Expose the object to synchronize on for the underlying session.

Returns:
  the session mutex to use (never `null`)

#### isSessionCompleted

    public boolean isSessionCompleted()

Whether the `sessionCompleted()` was already invoked.

#### sessionCompleted

    public void sessionCompleted()

Invoked when the session is completed. Executed completion callbacks.

#### fromMessage

    public static SimpAttributes fromMessage(Message<?> message)

Extract the SiMP session attributes from the given message and wrap them in a `SimpAttributes` instance.

Parameters:
  `message` - the message to extract session attributes from