Class DefaultSocketSession

java.lang.Object
io.fluxcapacitor.javaclient.web.DefaultSocketSession
All Implemented Interfaces:
SocketSession

public class DefaultSocketSession extends Object implements 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 via SocketRequest/SocketResponse
  • Tracking and completing pending requests with timeout handling
  • Dispatching and serializing socket messages using the configured ResultGateway and TaskScheduler
  • Supporting graceful closure of sessions and safe completion of outstanding requests
Internally, this class uses a ConcurrentHashMap to track open request-response pairs and leverages HandlerInvoker delegation to wrap handler execution and return results as SocketResponses.

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 Details

    • DefaultSocketSession

      public DefaultSocketSession()
  • Method Details

    • sendMessage

      public CompletableFuture<Void> sendMessage(Object value, Guarantee guarantee)
      Description copied from interface: SocketSession
      Sends a message over the WebSocket session with the specified delivery guarantee.
      Specified by:
      sendMessage in interface SocketSession
      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

      public <R> CompletionStage<R> sendRequest(Request<R> request, Duration timeout)
      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 by SocketRequest and then sent over the WebSocket session. Once the session receives a SocketResponse the request will be completed.
      Specified by:
      sendRequest in interface SocketSession
      Type Parameters:
      R - the type of the response expected from the request
      Parameters:
      request - the request object to be sent
      timeout - 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 type R 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

      public Optional<HandlerInvoker> tryCompleteRequest(DeserializingMessage message)
    • sendPing

      public CompletableFuture<Void> sendPing(Object value, Guarantee guarantee)
      Description copied from interface: SocketSession
      Sends a ping message with the specified value to the WebSocket session.
      Specified by:
      sendPing in interface SocketSession
      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

      public CompletableFuture<Void> close(int code, Guarantee guarantee)
      Description copied from interface: SocketSession
      Closes the WebSocket session with the specified close reason and delivery guarantee.
      Specified by:
      close in interface SocketSession
      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 the Guarantee 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 interface SocketSession
      Returns:
      true if the session is open; false otherwise.