Top Description Inners Fields Constructors Methods
org.springframework.web.socket.sockjs.transport.session

public abstract Class AbstractSockJsSession

extends Object
implements SockJsSession
Class Inheritance
All Implemented Interfaces
org.springframework.web.socket.sockjs.transport.SockJsSession, org.springframework.web.socket.WebSocketSession, java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession, org.springframework.web.socket.sockjs.transport.session.AbstractHttpSockJsSession
Imports
java.io.IOException, java.time.Instant, java.time.temporal.ChronoUnit, java.util.Arrays, .Collections, .List, .Map, java.util.concurrent.ConcurrentHashMap, .ScheduledFuture, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable, org.springframework.util.Assert, org.springframework.web.socket.CloseStatus, .TextMessage, .WebSocketHandler, .WebSocketMessage, org.springframework.web.socket.sockjs.SockJsMessageDeliveryException, .SockJsTransportFailureException, org.springframework.web.socket.sockjs.frame.SockJsFrame, .SockJsMessageCodec, org.springframework.web.socket.sockjs.transport.SockJsServiceConfig, .SockJsSession, org.springframework.web.util.DisconnectedClientHelper

An abstract base class for SockJS sessions implementing SockJsSession.
Authors
Rossen Stoyanchev, Sam Brannen
Since
4.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private static enum

Field Summary

Modifier and TypeField and Description
private final Map<String, Object>
private final SockJsServiceConfig
public static final String
DISCONNECTED_CLIENT_LOG_CATEGORY

Log category to use for network failure after a client has gone away.

private static final DisconnectedClientHelper
private final WebSocketHandler
private volatile boolean
private ScheduledFuture<?>
private AbstractSockJsSession.HeartbeatTask
private final String
protected final Log
protected final Object
private volatile AbstractSockJsSession.State
private final long
private volatile long

Constructor Summary

AccessConstructor and Description
public
AbstractSockJsSession(String
the session ID
id
,
SockJsServiceConfig
the SockJS service configuration options
config
,
WebSocketHandler
the recipient of SockJS messages
handler
,
Map<String, Object>
the attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.
attributes
)

Create a new instance.

Method Summary

Modifier and TypeMethod and Description
protected void
public final void
public final void
public final void
delegateConnectionClosed(CloseStatus status)

Invoked when the underlying connection is closed.

public void
public void
public void
delegateMessages(String... messages)

public void
disableHeartbeat()

Implements org.springframework.web.socket.sockjs.transport.SockJsSession.disableHeartbeat.

Disable the SockJS heartbeat, presumably because a higher-level protocol has heartbeats enabled for the session already.

protected abstract void
disconnect(CloseStatus status)

Actually close the underlying WebSocket session or in the case of HTTP transports complete the underlying request.

public Map<String, Object>
getAttributes()

Implements org.springframework.web.socket.WebSocketSession.getAttributes.

Return the map with attributes associated with the WebSocket session.

public String
getId()

Implements org.springframework.web.socket.WebSocketSession.getId.

Return a unique session identifier.

protected SockJsMessageCodec
public SockJsServiceConfig
public long
getTimeSinceLastActive()

Implements org.springframework.web.socket.sockjs.transport.SockJsSession.getTimeSinceLastActive.

Return the time (in ms) since the session was last active, or otherwise if the session is new, then the time since the session was created.

private static List<String>
getUndelivered(String[] messages, int i)

public abstract boolean
isActive()

Polling and Streaming sessions periodically close the current HTTP request and wait for the next request to come through.

public boolean
public boolean
public boolean
isOpen()

Implements org.springframework.web.socket.WebSocketSession.isOpen.

Whether the underlying connection is open.

private void
logUndeliveredMessages(int index, String[] messages)

private void
protected void
protected void
public final void
protected abstract void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public void
tryCloseWithSockJsTransportError(Throwable error, CloseStatus closeStatus)

Close due to error arising from SockJS transport handling.

protected void
updateLastActiveTime()

Should be invoked whenever the session becomes inactive.

protected void
writeFrame(SockJsFrame frame)

For internal use within a TransportHandler and the (TransportHandler-specific) session class.

protected abstract void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

attributesback to summary
private final Map<String, Object> attributes
configback to summary
private final SockJsServiceConfig config
DISCONNECTED_CLIENT_LOG_CATEGORYback to summary
public static final String DISCONNECTED_CLIENT_LOG_CATEGORY

Log category to use for network failure after a client has gone away.

See Also
DisconnectedClientHelper
disconnectedClientHelperback to summary
private static final DisconnectedClientHelper disconnectedClientHelper
handlerback to summary
private final WebSocketHandler handler
heartbeatDisabledback to summary
private volatile boolean heartbeatDisabled
heartbeatFutureback to summary
private ScheduledFuture<?> heartbeatFuture
Annotations
@Nullable
heartbeatTaskback to summary
private AbstractSockJsSession.HeartbeatTask heartbeatTask
Annotations
@Nullable
idback to summary
private final String id
loggerback to summary
protected final Log logger
responseLockback to summary
protected final Object responseLock
stateback to summary
private volatile AbstractSockJsSession.State state
timeCreatedback to summary
private final long timeCreated
timeLastActiveback to summary
private volatile long timeLastActive

Constructor Detail

AbstractSockJsSessionback to summary
public AbstractSockJsSession(String id, SockJsServiceConfig config, WebSocketHandler handler, Map<String, Object> attributes)

Create a new instance.

Parameters
id:String

the session ID

config:SockJsServiceConfig

the SockJS service configuration options

handler:WebSocketHandler

the recipient of SockJS messages

attributes:Map<String, Object>

the attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.

Method Detail

cancelHeartbeatback to summary
protected void cancelHeartbeat()
closeback to summary
public final void close() throws IOException

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

Performs cleanup and notify the WebSocketHandler.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.Closeable.close.

if an I/O error occurs

closeback to summary
public final void close(CloseStatus status) throws IOException

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

Performs cleanup and notify the WebSocketHandler.

Annotations
@Override
delegateConnectionClosedback to summary
public final void delegateConnectionClosed(CloseStatus status) throws Exception

Invoked when the underlying connection is closed.

delegateConnectionEstablishedback to summary
public void delegateConnectionEstablished() throws Exception
delegateErrorback to summary
public void delegateError(Throwable ex) throws Exception
delegateMessagesback to summary
public void delegateMessages(String... messages) throws SockJsMessageDeliveryException
disableHeartbeatback to summary
public void disableHeartbeat()

Implements org.springframework.web.socket.sockjs.transport.SockJsSession.disableHeartbeat.

Doc from org.springframework.web.socket.sockjs.transport.SockJsSession.disableHeartbeat.

Disable the SockJS heartbeat, presumably because a higher-level protocol has heartbeats enabled for the session already. It is not recommended to disable this otherwise, as it helps proxies to know the connection is not hanging.

Annotations
@Override
disconnectback to summary
protected abstract void disconnect(CloseStatus status) throws IOException

Actually close the underlying WebSocket session or in the case of HTTP transports complete the underlying request.

getAttributesback to summary
public Map<String, Object> getAttributes()

Implements org.springframework.web.socket.WebSocketSession.getAttributes.

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

Return the map with attributes associated with the WebSocket session.

On the server side the map can be populated initially through a HandshakeInterceptor. On the client side the map can be populated via WebSocketClient handshake methods.

Returns:Map<String, Object>

a Map with the session attributes (never null)

Annotations
@Override
getIdback to summary
public String getId()

Implements org.springframework.web.socket.WebSocketSession.getId.

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

Return a unique session identifier.

Annotations
@Override
getMessageCodecback to summary
protected SockJsMessageCodec getMessageCodec()
getSockJsServiceConfigback to summary
public SockJsServiceConfig getSockJsServiceConfig()
getTimeSinceLastActiveback to summary
public long getTimeSinceLastActive()

Implements org.springframework.web.socket.sockjs.transport.SockJsSession.getTimeSinceLastActive.

Doc from org.springframework.web.socket.sockjs.transport.SockJsSession.getTimeSinceLastActive.

Return the time (in ms) since the session was last active, or otherwise if the session is new, then the time since the session was created.

Annotations
@Override
getUndeliveredback to summary
private static List<String> getUndelivered(String[] messages, int i)
isActiveback to summary
public abstract boolean isActive()

Polling and Streaming sessions periodically close the current HTTP request and wait for the next request to come through. During this "downtime" the session is still open but inactive and unable to send messages and therefore has to buffer them temporarily. A WebSocket session by contrast is stateful and remain active until closed.

isClosedback to summary
public boolean isClosed()
isNewback to summary
public boolean isNew()
isOpenback to summary
public boolean isOpen()

Implements org.springframework.web.socket.WebSocketSession.isOpen.

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

Whether the underlying connection is open.

Annotations
@Override
logUndeliveredMessagesback to summary
private void logUndeliveredMessages(int index, String[] messages)
logWriteFrameFailureback to summary
private void logWriteFrameFailure(Throwable ex)
scheduleHeartbeatback to summary
protected void scheduleHeartbeat()
sendHeartbeatback to summary
protected void sendHeartbeat() throws SockJsTransportFailureException
sendMessageback to summary
public final void sendMessage(WebSocketMessage<?> message) throws IOException

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
sendMessageInternalback to summary
protected abstract void sendMessageInternal(String message) throws IOException
toStringback to summary
public String toString()

Overrides java.lang.Object.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
tryCloseWithSockJsTransportErrorback to summary
public void tryCloseWithSockJsTransportError(Throwable error, CloseStatus closeStatus)

Close due to error arising from SockJS transport handling.

updateLastActiveTimeback to summary
protected void updateLastActiveTime()

Should be invoked whenever the session becomes inactive.

writeFrameback to summary
protected void writeFrame(SockJsFrame frame) throws SockJsTransportFailureException

For internal use within a TransportHandler and the (TransportHandler-specific) session class.

writeFrameInternalback to summary
protected abstract void writeFrameInternal(SockJsFrame frame) throws IOException
org.springframework.web.socket.sockjs.transport.session back to summary

private Class AbstractSockJsSession.HeartbeatTask

extends Object
implements Runnable
Class Inheritance
  • java.lang.Object
  • org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession.HeartbeatTask
All Implemented Interfaces
java.lang.Runnable

Field Summary

Modifier and TypeField and Description
private boolean

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv void
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

expiredback to summary
private boolean expired

Constructor Detail

HeartbeatTaskback to summary
private HeartbeatTask()

Method Detail

cancelback to summary
pack-priv void cancel()
runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
org.springframework.web.socket.sockjs.transport.session back to summary

private final Enum AbstractSockJsSession.State

extends Enum<AbstractSockJsSession.State>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final AbstractSockJsSession.State
public static final AbstractSockJsSession.State
public static final AbstractSockJsSession.State

Constructor Summary

AccessConstructor and Description
private

Method Summary

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

Field Detail

CLOSEDback to summary
public static final AbstractSockJsSession.State CLOSED
NEWback to summary
public static final AbstractSockJsSession.State NEW
OPENback to summary
public static final AbstractSockJsSession.State OPEN

Constructor Detail

Stateback to summary
private State()

Method Detail

valueOfback to summary
public static AbstractSockJsSession.State valueOf(String name)
valuesback to summary
public static AbstractSockJsSession.State[] values()