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 | Field and Description |
---|---|
private java.util.Queue<WebSocketMessage<?>> |
buffer |
private java.util.concurrent.atomic.AtomicInteger |
bufferSize |
private int |
bufferSizeLimit |
private boolean |
closeInProgress |
private java.util.concurrent.locks.Lock |
closeLock |
private java.util.concurrent.locks.Lock |
flushLock |
private boolean |
limitExceeded |
private static Log |
logger |
private long |
sendStartTime |
private int |
sendTimeLimit |
Constructor and Description |
---|
ConcurrentWebSocketSessionDecorator(WebSocketSession delegate,
int sendTimeLimit,
int bufferSizeLimit)
Create a new
ConcurrentWebSocketSessionDecorator . |
Modifier and Type | Method and Description |
---|---|
private void |
checkSessionLimits() |
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.
|
private void |
limitExceeded(java.lang.String reason) |
void |
sendMessage(WebSocketMessage<?> message)
Send a WebSocket message: either
TextMessage or BinaryMessage . |
private boolean |
shouldNotSend() |
java.lang.String |
toString() |
private boolean |
tryFlushMessageBuffer() |
close, getAcceptedProtocol, getAttributes, getBinaryMessageSizeLimit, getDelegate, getExtensions, getHandshakeHeaders, getId, getLastSession, getLocalAddress, getPrincipal, getRemoteAddress, getTextMessageSizeLimit, getUri, isOpen, setBinaryMessageSizeLimit, setTextMessageSizeLimit, unwrap
private static final Log logger
private final int sendTimeLimit
private final int bufferSizeLimit
private final java.util.Queue<WebSocketMessage<?>> buffer
private final java.util.concurrent.atomic.AtomicInteger bufferSize
private volatile long sendStartTime
private volatile boolean limitExceeded
private volatile boolean closeInProgress
private final java.util.concurrent.locks.Lock flushLock
private final java.util.concurrent.locks.Lock closeLock
public ConcurrentWebSocketSessionDecorator(WebSocketSession delegate, int sendTimeLimit, int bufferSizeLimit)
ConcurrentWebSocketSessionDecorator
.delegate
- the WebSocketSession
to delegate tosendTimeLimit
- the send-time limit (milliseconds)bufferSizeLimit
- the buffer-size limit (number of bytes)public int getSendTimeLimit()
public int getBufferSizeLimit()
public int getBufferSize()
public long getTimeSinceSendStarted()
public void sendMessage(WebSocketMessage<?> message) throws java.io.IOException
WebSocketSession
TextMessage
or BinaryMessage
.sendMessage
in interface WebSocketSession
sendMessage
in class WebSocketSessionDecorator
java.io.IOException
private boolean shouldNotSend()
private boolean tryFlushMessageBuffer() throws java.io.IOException
java.io.IOException
private void checkSessionLimits()
private void limitExceeded(java.lang.String reason)
public void close(CloseStatus status) throws java.io.IOException
WebSocketSession
close
in interface WebSocketSession
close
in class WebSocketSessionDecorator
java.io.IOException
public java.lang.String toString()
toString
in class WebSocketSessionDecorator