org.springframework.web.socket.handler

## Class ConcurrentWebSocketSessionDecorator

- java.lang.Object
- - org.springframework.web.socket.handler.WebSocketSessionDecorator
  - - org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator

All Implemented Interfaces:
  Closeable, AutoCloseable, WebSocketSession

    public class ConcurrentWebSocketSessionDecorator
    extends WebSocketSessionDecorator

Wrap a `WebSocketSession` to guarantee only one thread can send messages at a time.

If a send is slow, subsequent attempts to send more messages from other threads will not be able to acquire the flush lock and messages will be buffered instead. At that time, the specified buffer-size limit and send-time limit will be checked and the session will be closed if the limits are exceeded.

Since:
  4.0.3
Author:
  Rossen Stoyanchev, Juergen Hoeller

### Nested Class Summary

Nested Classes
`static class` `ConcurrentWebSocketSessionDecorator.OverflowStrategy` Enum for options of what to do when the buffer fills up.

### Constructor Summary

Constructors
Constructor and Description
`ConcurrentWebSocketSessionDecorator(WebSocketSession delegate, int sendTimeLimit, int bufferSizeLimit)` Basic constructor.
`ConcurrentWebSocketSessionDecorator(WebSocketSession delegate, int sendTimeLimit, int bufferSizeLimit, ConcurrentWebSocketSessionDecorator.OverflowStrategy overflowStrategy)` Constructor that also specifies the overflow strategy to use.

### Method Summary

All Methods Instance Methods Concrete Methods
`void` `close(CloseStatus status)` Close the WebSocket connection with the given close status.
`int` `getBufferSize()` Return the current buffer size (number of bytes).
`int` `getBufferSizeLimit()` Return the configured buffer-size limit (number of bytes).
`int` `getSendTimeLimit()` Return the configured send-time limit (milliseconds).
`long` `getTimeSinceSendStarted()` Return the time (milliseconds) since the current send started, or 0 if no send is currently in progress.
`void` `sendMessage(WebSocketMessage<?> message)` Send a WebSocket message: either `TextMessage` or `BinaryMessage`.
`String` `toString()`

### Methods inherited from class org.springframework.web.socket.handler.WebSocketSessionDecorator

`close, getAcceptedProtocol, getAttributes, getBinaryMessageSizeLimit, getDelegate, getExtensions, getHandshakeHeaders, getId, getLastSession, getLocalAddress, getPrincipal, getRemoteAddress, getTextMessageSizeLimit, getUri, isOpen, setBinaryMessageSizeLimit, setTextMessageSizeLimit, unwrap`

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

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

### Constructor Detail

#### ConcurrentWebSocketSessionDecorator

    public ConcurrentWebSocketSessionDecorator(WebSocketSession delegate,
                                               int sendTimeLimit,
                                               int bufferSizeLimit)

Basic constructor.

Parameters:
  `delegate` - the `WebSocketSession` to delegate to
  `sendTimeLimit` - the send-time limit (milliseconds)
  `bufferSizeLimit` - the buffer-size limit (number of bytes)

#### ConcurrentWebSocketSessionDecorator

    public ConcurrentWebSocketSessionDecorator(WebSocketSession delegate,
                                               int sendTimeLimit,
                                               int bufferSizeLimit,
                                               ConcurrentWebSocketSessionDecorator.OverflowStrategy overflowStrategy)

Constructor that also specifies the overflow strategy to use.

Parameters:
  `delegate` - the `WebSocketSession` to delegate to
  `sendTimeLimit` - the send-time limit (milliseconds)
  `bufferSizeLimit` - the buffer-size limit (number of bytes)
  `overflowStrategy` - the overflow strategy to use; by default the session is terminated.
Since:
  5.1

### Method Detail

#### getSendTimeLimit

    public int getSendTimeLimit()

Return the configured send-time limit (milliseconds).

Since:
  4.3.13

#### getBufferSizeLimit

    public int getBufferSizeLimit()

Return the configured buffer-size limit (number of bytes).

Since:
  4.3.13

#### getBufferSize

    public int getBufferSize()

Return the current buffer size (number of bytes).

#### getTimeSinceSendStarted

    public long getTimeSinceSendStarted()

Return the time (milliseconds) since the current send started, or 0 if no send is currently in progress.

#### sendMessage

    public void sendMessage(WebSocketMessage<?> message)
                     throws IOException

Description copied from interface: `WebSocketSession`
Send a WebSocket message: either `TextMessage` or `BinaryMessage`.

Note: The underlying standard WebSocket session (JSR-356) does not allow concurrent sending. Therefore sending must be synchronized. To ensure that, one option is to wrap the `WebSocketSession` with the `ConcurrentWebSocketSessionDecorator`.

Specified by:
  `sendMessage` in interface `WebSocketSession`
Overrides:
  `sendMessage` in class `WebSocketSessionDecorator`
Throws:
  `IOException`
See Also:
  `ConcurrentWebSocketSessionDecorator`

#### close

    public void close(CloseStatus status)
               throws IOException

Description copied from interface: `WebSocketSession`
Close the WebSocket connection with the given close status.

Specified by:
  `close` in interface `WebSocketSession`
Overrides:
  `close` in class `WebSocketSessionDecorator`
Throws:
  `IOException`

#### toString

    public String toString()

Overrides:
  `toString` in class `WebSocketSessionDecorator`