Package io.fluxcapacitor.javaclient.web
Class DefaultSocketSession
java.lang.Object
io.fluxcapacitor.javaclient.web.DefaultSocketSession
- All Implemented Interfaces:
SocketSession
Default implementation of a
SocketSession
for managing Flux WebSocket sessions.
This class handles bidirectional communication over a WebSocket session, including:
- Sending regular messages, pings, and close instructions using various
Guarantee
levels - Sending and awaiting responses to
Request
objects viaSocketRequest
/SocketResponse
- Tracking and completing pending requests with timeout handling
- Dispatching and serializing socket messages using the configured
ResultGateway
andTaskScheduler
- Supporting graceful closure of sessions and safe completion of outstanding requests
ConcurrentHashMap
to track open request-response pairs and leverages
HandlerInvoker
delegation to wrap handler execution and return results as SocketResponse
s.
On receiving an incoming SocketRequest
, it attempts to resolve and invoke the appropriate handler.
On receiving a SocketResponse
, it attempts to match and complete the original request.
Sessions are identified by a unique session ID and can include custom headers. Delivery guarantees are honored
according to the underlying ResultGateway
implementation.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCloses the WebSocket session with the specified close reason and delivery guarantee.boolean
isOpen()
Determines whether the WebSocket session is currently open.boolean
onClose()
sendMessage
(Object value, Guarantee guarantee) Sends a message over the WebSocket session with the specified delivery guarantee.Sends a ping message with the specified value to the WebSocket session.<R> CompletionStage
<R> sendRequest
(Request<R> request, Duration timeout) Sends a request over the WebSocket session with a specified timeout duration.tryCompleteRequest
(DeserializingMessage message) tryHandleRequest
(DeserializingMessage message, Handler<DeserializingMessage> handler) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.fluxcapacitor.javaclient.web.SocketSession
close, close, close, sendMessage, sendPing, sendRequest, sessionId
-
Constructor Details
-
DefaultSocketSession
public DefaultSocketSession()
-
-
Method Details
-
sendMessage
Description copied from interface:SocketSession
Sends a message over the WebSocket session with the specified delivery guarantee.- Specified by:
sendMessage
in interfaceSocketSession
- Parameters:
value
- the value of the message to be sent. The value will be serialized and transmitted as bytes.guarantee
- the delivery guarantee for the message.- Returns:
- a
CompletableFuture
that completes when the message has been sent or stored, or fails with an exception if that operation fails.
-
sendRequest
Description copied from interface:SocketSession
Sends a request over the WebSocket session with a specified timeout duration. This method asynchronously sends the given request and waits for the corresponding response. The request object will be wrapped bySocketRequest
and then sent over the WebSocket session. Once the session receives aSocketResponse
the request will be completed.- Specified by:
sendRequest
in interfaceSocketSession
- Type Parameters:
R
- the type of the response expected from the request- Parameters:
request
- the request object to be senttimeout
- the timeout duration for the request, after which it will fail if no response is received- Returns:
- a
CompletionStage<R>
that completes with the response of the specified typeR
or completes exceptionally if an error occurs or the timeout is exceeded - See Also:
-
tryHandleRequest
public Optional<HandlerInvoker> tryHandleRequest(DeserializingMessage message, Handler<DeserializingMessage> handler) -
tryCompleteRequest
-
sendPing
Description copied from interface:SocketSession
Sends a ping message with the specified value to the WebSocket session.- Specified by:
sendPing
in interfaceSocketSession
- Parameters:
value
- the value to be included in the ping message. This can be any object that is serializable or manageable according to the WebSocket session's implementation.guarantee
- the delivery guarantee for the ping message. It specifies how assurances (e.g., sending, storing) are handled for the message delivery.- Returns:
- a CompletableFuture that completes once the ping message is processed or the delivery guarantee has been fulfilled.
-
close
Description copied from interface:SocketSession
Closes the WebSocket session with the specified close reason and delivery guarantee.- Specified by:
close
in interfaceSocketSession
- Parameters:
code
- an integer indicating the reason for closing the WebSocket session, typically based on WebSocket close codes (e.g., 1000 for normal closure).guarantee
- the delivery guarantee for ensuring the closure message is sent, which can be one of theGuarantee
values: NONE, SENT, or STORED.- Returns:
- a
CompletableFuture<Void>
that completes when the close operation is performed successfully.
-
onClose
public boolean onClose() -
isOpen
public boolean isOpen()Description copied from interface:SocketSession
Determines whether the WebSocket session is currently open.- Specified by:
isOpen
in interfaceSocketSession
- Returns:
- true if the session is open; false otherwise.
-