public class ConcurrentWebSocketSessionDecorator extends WebSocketSessionDecorator
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.
Modifier and Type | Class and Description |
---|---|
static class |
ConcurrentWebSocketSessionDecorator.OverflowStrategy
Enum for options of what to do when the buffer fills up.
|
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.
|
Modifier and Type | Method and Description |
---|---|
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() |
close, getAcceptedProtocol, getAttributes, getBinaryMessageSizeLimit, getDelegate, getExtensions, getHandshakeHeaders, getId, getLastSession, getLocalAddress, getPrincipal, getRemoteAddress, getTextMessageSizeLimit, getUri, isOpen, setBinaryMessageSizeLimit, setTextMessageSizeLimit, unwrap
public ConcurrentWebSocketSessionDecorator(WebSocketSession delegate, int sendTimeLimit, int bufferSizeLimit)
delegate
- the WebSocketSession
to delegate tosendTimeLimit
- the send-time limit (milliseconds)bufferSizeLimit
- the buffer-size limit (number of bytes)public ConcurrentWebSocketSessionDecorator(WebSocketSession delegate, int sendTimeLimit, int bufferSizeLimit, ConcurrentWebSocketSessionDecorator.OverflowStrategy overflowStrategy)
delegate
- the WebSocketSession
to delegate tosendTimeLimit
- 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.public int getSendTimeLimit()
public int getBufferSizeLimit()
public int getBufferSize()
public long getTimeSinceSendStarted()
public void sendMessage(WebSocketMessage<?> message) throws IOException
WebSocketSession
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
.
sendMessage
in interface WebSocketSession
sendMessage
in class WebSocketSessionDecorator
IOException
ConcurrentWebSocketSessionDecorator
public void close(CloseStatus status) throws IOException
WebSocketSession
close
in interface WebSocketSession
close
in class WebSocketSessionDecorator
IOException
public String toString()
toString
in class WebSocketSessionDecorator