Class ClusterContext
There is exactly one instance per cluster node and all communication is realized using this instance.
- Author:
- Pavel Bucek
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract void
broadcastBinary
(String endpointPath, byte[] data) Broadcast binary message.abstract void
broadcastText
(String endpointPath, String text) Broadcast text message.Close remote session.close
(String sessionId, CloseReason closeReason) Close remote session with customCloseReason
.abstract String
Create connection id.abstract String
Create session id.abstract void
destroyDistributedUserProperties
(String connectionId) Destroy map which holds distributed user properties.abstract Map
<RemoteSession.DistributedMapKey, Object> getDistributedSessionProperties
(String sessionId) Get the map containing session properties to be shared among nodes.getDistributedUserProperties
(String connectionId) Get the map containing session user properties to be shared among nodes.getRemoteSessionIds
(String endpointPath) Get set containing session ids of all remote sessions registered to given endpoint path.abstract boolean
isSessionOpen
(String sessionId, String endpointPath) Get information about session state.abstract void
registerBroadcastListener
(String endpointPath, BroadcastListener listener) Register broadcast listener.abstract void
registerSession
(String sessionId, String endpointPath, SessionEventListener listener) Register local session.abstract void
registerSessionListener
(String endpointPath, SessionListener listener) Register session listener.abstract void
removeSession
(String sessionId, String endpointPath) Remove session from this Cluster context.sendBinary
(String sessionId, byte[] data) Send binary message.sendBinary
(String sessionId, byte[] data, boolean isLast) Send partial binary message.abstract void
sendBinary
(String sessionId, byte[] data, SendHandler sendHandler) Send binary message withSendHandler
.Send ping message.Send pong message.Send text message.Send partial text message.abstract void
sendText
(String sessionId, String text, SendHandler sendHandler) Send text message withSendHandler
.abstract void
shutdown()
Shutdown this ClusterContext.
-
Field Details
-
CLUSTER_CONTEXT
ClusterContext registration property.ClusterContext is registered to the Server container via properties passed to
ServerContainerFactory.createServerContainer(java.util.Map)
.- See Also:
-
-
Constructor Details
-
ClusterContext
public ClusterContext()
-
-
Method Details
-
sendText
Send text message.- Parameters:
sessionId
- remote session id.text
- text to be sent.- Returns:
- future representing the send event.
Future.get()
returns when there is an acknowledge from the other node that the message has been successfully sent. If there is any exception, it will be wrapped intoExecutionException
and thrown.
-
sendText
Send partial text message.- Parameters:
sessionId
- remote session id.text
- text to be sent.isLast
-true
when the partial message being sent is the last part of the message.- Returns:
- future representing the send event.
Future.get()
returns when there is an acknowledge from the other node that the message has been successfully sent. If there is any exception, it will be wrapped intoExecutionException
and thrown.
-
sendBinary
Send binary message.- Parameters:
sessionId
- remote session id.data
- data to be sent.- Returns:
- future representing the send event.
Future.get()
returns when there is an acknowledge from the other node that the message has been successfully sent. If there is any exception, it will be wrapped intoExecutionException
and thrown.
-
sendBinary
Send partial binary message.- Parameters:
sessionId
- remote session id.data
- data to be sent.isLast
-true
when the partial message being sent is the last part of the message.- Returns:
- future representing the send event.
Future.get()
returns when there is an acknowledge from the other node that the message has been successfully sent. If there is any exception, it will be wrapped intoExecutionException
and thrown.
-
sendPing
Send ping message.- Parameters:
sessionId
- remote session id.data
- data to be sent as ping message payload.- Returns:
- future representing the send event.
Future.get()
returns when there is an acknowledge from the other node that the message has been successfully sent. If there is any exception, it will be wrapped intoExecutionException
and thrown.
-
sendPong
Send pong message.- Parameters:
sessionId
- remote session id.data
- data to be sent as pong message payload.- Returns:
- future representing the send event.
Future.get()
returns when there is an acknowledge from the other node that the message has been successfully sent. If there is any exception, it will be wrapped intoExecutionException
and thrown.
-
sendText
Send text message withSendHandler
.- Parameters:
sessionId
- remote session id.text
- text to be sent.sendHandler
- sendhandler instance on whichSendHandler.onResult(jakarta.websocket.SendResult)
will be invoked.- See Also:
-
sendBinary
Send binary message withSendHandler
.- Parameters:
sessionId
- remote session id.data
- data to be sent.sendHandler
- sendhandler instance on whichSendHandler.onResult(jakarta.websocket.SendResult)
will be invoked.- See Also:
-
broadcastText
Broadcast text message.- Parameters:
endpointPath
- endpoint path identifying sessions alignment to the endpoint.text
- message to be broadcasted.
-
broadcastBinary
Broadcast binary message.- Parameters:
endpointPath
- endpoint path identifying sessions alignment to the endpoint.data
- data to be broadcasted.
-
isSessionOpen
Get information about session state.- Parameters:
sessionId
- remote session id.endpointPath
- endpoint path identifying sessions alignment to the endpoint.- Returns:
true
when session is opened,false
otherwise.- See Also:
-
close
Close remote session.- Parameters:
sessionId
- remote session id.- Returns:
- future representing the event.
Future.get()
returns when there is an acknowledge from the other node that the command was successfully executed. If there is any exception, it will be wrapped intoExecutionException
and thrown.
-
close
Close remote session with customCloseReason
.- Parameters:
sessionId
- remote session id.closeReason
- custom close reason.- Returns:
- future representing the event.
Future.get()
returns when there is an acknowledge from the other node that the command was successfully executed. If there is any exception, it will be wrapped intoExecutionException
and thrown.
-
getRemoteSessionIds
Get set containing session ids of all remote sessions registered to given endpoint path.- Parameters:
endpointPath
- endpoint path identifying endpoint within the cluster.- Returns:
- set of sessions ids.
-
createSessionId
Create session id. It has to be unique among all cluster nodes.- Returns:
- session id.
-
createConnectionId
Create connection id. It has to be unique among all cluster nodes.- Returns:
- connection id.
-
registerSession
public abstract void registerSession(String sessionId, String endpointPath, SessionEventListener listener) Register local session.Session id will be broadcasted to other nodes which will call
getDistributedSessionProperties(String)
and process its values. The map must be ready before this method is invoked.- Parameters:
sessionId
- session id to be registered.endpointPath
- endpoint path identifying sessions alignment to the endpoint.listener
- session event listener. When remote node sends a message to this session, it will be invoked.- See Also:
-
registerSessionListener
Register session listener.Gets notification about session creation
.SessionListener#onSessionOpened(String)
and destruction.SessionListener#onSessionClosed(String)
.- Parameters:
endpointPath
- endpoint path identifying sessions alignment to the endpoint.listener
- listener instance.- See Also:
-
registerBroadcastListener
Register broadcast listener.Gets notification about broadcasted messages. Used as an optimized variant of standard websocket broadcast pattern. In this case, only one message is sent to all cluster nodes (instead
n
whenn
represent number of clients connected to remote nodes).- Parameters:
endpointPath
- endpoint path identifying sessions alignment to the endpoint.listener
- listener instance.- See Also:
-
getDistributedSessionProperties
public abstract Map<RemoteSession.DistributedMapKey,Object> getDistributedSessionProperties(String sessionId) Get the map containing session properties to be shared among nodes.Changes must be propagated to remote instances.
- Parameters:
sessionId
- remote session id.- Returns:
- distributed map containing session properties.
-
getDistributedUserProperties
Get the map containing session user properties to be shared among nodes.Changes must be propagated to remote instances.
- Parameters:
connectionId
- connection id. Connection id may be shared among subsequent TCP connection - represents logical connection.- Returns:
- distributed map containing session properties.
-
destroyDistributedUserProperties
Destroy map which holds distributed user properties.This method should be invoked only when session is properly closed.
- Parameters:
connectionId
- connection id. Connection id may be shared among subsequent TCP connection - represents logical connection.
-
removeSession
Remove session from this Cluster context.- Parameters:
sessionId
- session id.endpointPath
- endpoint path identifying sessions alignment to the endpoint.
-
shutdown
public abstract void shutdown()Shutdown this ClusterContext.This will stop whole clustered node, any operation related to this cluster context will fail after this method is invoked.
-