Top Description Inners Fields Constructors Methods
org.springframework.web.socket.handler

public Class ConcurrentWebSocketSessionDecorator

extends WebSocketSessionDecorator
Class Inheritance
Imports
java.io.IOException, java.util.Queue, java.util.concurrent.LinkedBlockingQueue, java.util.concurrent.atomic.AtomicInteger, java.util.concurrent.locks.Lock, .ReentrantLock, java.util.function.Consumer, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable, org.springframework.web.socket.CloseStatus, .WebSocketMessage, .WebSocketSession

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.

Authors
Rossen Stoyanchev, Juergen Hoeller
Since
4.0.3

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
ConcurrentWebSocketSessionDecorator.OverflowStrategy

Enum for options of what to do when the buffer fills up.

Field Summary

Modifier and TypeField and Description
private final Queue<WebSocketMessage<?>>
private final AtomicInteger
private final int
private volatile boolean
private final Lock
private final Lock
private volatile boolean
private static final Log
private final ConcurrentWebSocketSessionDecorator.OverflowStrategy
private Consumer<WebSocketMessage<?>>
private volatile long
private final int

Constructor Summary

AccessConstructor and Description
public
ConcurrentWebSocketSessionDecorator(WebSocketSession
the WebSocketSession to delegate to
delegate
,
int
the send-time limit (milliseconds)
sendTimeLimit
,
int
the buffer-size limit (number of bytes)
bufferSizeLimit
)

Basic constructor.

public
ConcurrentWebSocketSessionDecorator(WebSocketSession
the WebSocketSession to delegate to
delegate
,
int
the send-time limit (milliseconds)
sendTimeLimit
,
int
the buffer-size limit (number of bytes)
bufferSizeLimit
,
ConcurrentWebSocketSessionDecorator.OverflowStrategy
the overflow strategy to use; by default the session is terminated.
overflowStrategy
)

Constructor that also specifies the overflow strategy to use.

Method Summary

Modifier and TypeMethod and Description
private void
public void
close(CloseStatus status)

Overrides org.springframework.web.socket.handler.WebSocketSessionDecorator.close.

Implements org.springframework.web.socket.WebSocketSession.close.

Close the WebSocket connection with the given close status.

public int
getBufferSize()

Return the current buffer size (number of bytes).

public int
getBufferSizeLimit()

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

public int
getSendTimeLimit()

Return the configured send-time limit (milliseconds).

public long
getTimeSinceSendStarted()

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

private void
public void
public void
setMessageCallback(Consumer<WebSocketMessage<?>>
the callback to invoke
callback
)

Set a callback invoked after a message is added to the send buffer.

private boolean
public String
toString()

Overrides org.springframework.web.socket.handler.WebSocketSessionDecorator.toString.

Returns a string representation of the object.

private boolean
Inherited from org.springframework.web.socket.handler.WebSocketSessionDecorator:
closegetAcceptedProtocolgetAttributesgetBinaryMessageSizeLimitgetDelegategetExtensionsgetHandshakeHeadersgetIdgetLastSessiongetLocalAddressgetPrincipalgetRemoteAddressgetTextMessageSizeLimitgetUriisOpensetBinaryMessageSizeLimitsetTextMessageSizeLimitunwrap

Field Detail

bufferback to summary
private final Queue<WebSocketMessage<?>> buffer
bufferSizeback to summary
private final AtomicInteger bufferSize
bufferSizeLimitback to summary
private final int bufferSizeLimit
closeInProgressback to summary
private volatile boolean closeInProgress
closeLockback to summary
private final Lock closeLock
flushLockback to summary
private final Lock flushLock
limitExceededback to summary
private volatile boolean limitExceeded
loggerback to summary
private static final Log logger
overflowStrategyback to summary
private final ConcurrentWebSocketSessionDecorator.OverflowStrategy overflowStrategy
preSendCallbackback to summary
private Consumer<WebSocketMessage<?>> preSendCallback
Annotations
@Nullable
sendStartTimeback to summary
private volatile long sendStartTime
sendTimeLimitback to summary
private final int sendTimeLimit

Constructor Detail

ConcurrentWebSocketSessionDecoratorback to summary
public ConcurrentWebSocketSessionDecorator(WebSocketSession delegate, int sendTimeLimit, int bufferSizeLimit)

Basic constructor.

Parameters
delegate:WebSocketSession

the WebSocketSession to delegate to

sendTimeLimit:int

the send-time limit (milliseconds)

bufferSizeLimit:int

the buffer-size limit (number of bytes)

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

Constructor that also specifies the overflow strategy to use.

Parameters
delegate:WebSocketSession

the WebSocketSession to delegate to

sendTimeLimit:int

the send-time limit (milliseconds)

bufferSizeLimit:int

the buffer-size limit (number of bytes)

overflowStrategy:ConcurrentWebSocketSessionDecorator.OverflowStrategy

the overflow strategy to use; by default the session is terminated.

Since
5.1

Method Detail

checkSessionLimitsback to summary
private void checkSessionLimits()
closeback to summary
public void close(CloseStatus status) throws IOException

Overrides org.springframework.web.socket.handler.WebSocketSessionDecorator.close.

Implements org.springframework.web.socket.WebSocketSession.close.

Doc from org.springframework.web.socket.WebSocketSession.close.

Close the WebSocket connection with the given close status.

Annotations
@Override
getBufferSizeback to summary
public int getBufferSize()

Return the current buffer size (number of bytes).

getBufferSizeLimitback to summary
public int getBufferSizeLimit()

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

Since
4.3.13
getSendTimeLimitback to summary
public int getSendTimeLimit()

Return the configured send-time limit (milliseconds).

Since
4.3.13
getTimeSinceSendStartedback to summary
public long getTimeSinceSendStarted()

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

limitExceededback to summary
private void limitExceeded(String reason)
sendMessageback to summary
public void sendMessage(WebSocketMessage<?> message) throws IOException

Overrides org.springframework.web.socket.handler.WebSocketSessionDecorator.sendMessage.

Implements org.springframework.web.socket.WebSocketSession.sendMessage.

Doc from org.springframework.web.socket.WebSocketSession.sendMessage.

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.

Annotations
@Override
setMessageCallbackback to summary
public void setMessageCallback(Consumer<WebSocketMessage<?>> callback)

Set a callback invoked after a message is added to the send buffer.

Parameters
callback:Consumer<WebSocketMessage<?>>

the callback to invoke

Since
5.3
shouldNotSendback to summary
private boolean shouldNotSend()
toStringback to summary
public String toString()

Overrides org.springframework.web.socket.handler.WebSocketSessionDecorator.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
tryFlushMessageBufferback to summary
private boolean tryFlushMessageBuffer() throws IOException
org.springframework.web.socket.handler back to summary

public final Enum ConcurrentWebSocketSessionDecorator.OverflowStrategy

extends Enum<ConcurrentWebSocketSessionDecorator.OverflowStrategy>
Class Inheritance

Enum for options of what to do when the buffer fills up.
Since
5.1

Field Summary

Modifier and TypeField and Description
public static final ConcurrentWebSocketSessionDecorator.OverflowStrategy
DROP

Drop the oldest messages from the buffer.

public static final ConcurrentWebSocketSessionDecorator.OverflowStrategy
TERMINATE

Throw SessionLimitExceededException that will result in the session being terminated.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ConcurrentWebSocketSessionDecorator.OverflowStrategy
public static ConcurrentWebSocketSessionDecorator.OverflowStrategy[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

DROPback to summary
public static final ConcurrentWebSocketSessionDecorator.OverflowStrategy DROP

Drop the oldest messages from the buffer.

TERMINATEback to summary
public static final ConcurrentWebSocketSessionDecorator.OverflowStrategy TERMINATE

Throw SessionLimitExceededException that will result in the session being terminated.

Constructor Detail

OverflowStrategyback to summary
private OverflowStrategy()

Method Detail

valueOfback to summary
public static ConcurrentWebSocketSessionDecorator.OverflowStrategy valueOf(String name)
valuesback to summary
public static ConcurrentWebSocketSessionDecorator.OverflowStrategy[] values()