Class AbstractWebsocketClient
java.lang.Object
io.fluxzero.sdk.common.websocket.AbstractWebsocketClient
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
WebSocketEventStoreClient, WebsocketGatewayClient, WebsocketKeyValueClient, WebsocketSchedulingClient, WebSocketSearchClient, WebsocketTrackingClient
Abstract base class for all WebSocket-based clients in the Fluxzero Java client.
This class provides robust connection management, message dispatching, result handling, batching, metrics publishing,
and ping-based health checking. It underpins core components such as WebsocketGatewayClient, providing the
shared infrastructure needed for durable, resilient WebSocket communication with the Fluxzero Runtime.
Core Responsibilities
- Establishing and maintaining WebSocket connections with automatic reconnection support
- Managing message sending and batching via
RequestandRequestBatch - Receiving and processing incoming
RequestResultandResultBatchmessages - Supporting command guarantees (e.g., SENT, STORED) with retries and backpressure handling
- Sending periodic ping frames to detect connection drops
- Integrating with the Fluxzero metrics infrastructure for custom performance telemetry
Key Features
- Session Pooling: Maintains multiple concurrent sessions to handle high-throughput scenarios
- Request Backlogs: Each session has a backlog to buffer and batch outgoing requests
- Ping Scheduling: Scheduled tasks detect broken sessions using WebSocket pings
- Auto Retry: Failed requests are retried if the session is closed unexpectedly
- Async Result Handling: Responses are handled on a separate thread pool to avoid blocking I/O
- Metrics Publishing: Optional emission of message-related metrics based on configuration
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classprotected class -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic com.fasterxml.jackson.databind.ObjectMapperstatic jakarta.websocket.WebSocketContainer -
Constructor Summary
ConstructorsConstructorDescriptionAbstractWebsocketClient(jakarta.websocket.WebSocketContainer container, URI endpointUri, WebSocketClient client, boolean allowMetrics, Duration reconnectDelay, com.fasterxml.jackson.databind.ObjectMapper objectMapper, int numberOfSessions) Constructs a WebSocket client with fine-grained control over connection setup.AbstractWebsocketClient(URI endpointUri, WebSocketClient client, boolean allowMetrics) Creates a WebSocket client using the given endpoint URI, client implementation, and a flag to allow metrics.AbstractWebsocketClient(URI endpointUri, WebSocketClient client, boolean allowMetrics, int numberOfSessions) Creates a WebSocket client with multiple parallel sessions using default settings. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidabort(jakarta.websocket.Session session) voidclose()protected voidclose(boolean clearOutstandingRequests) protected voidhandleResult(RequestResult result, String batchId) protected MetadatavoidonClose(jakarta.websocket.Session session, jakarta.websocket.CloseReason closeReason) voidvoidonMessage(byte[] bytes, jakarta.websocket.Session session) voidonOpen(jakarta.websocket.Session session) voidonPong(jakarta.websocket.PongMessage message, jakarta.websocket.Session session) protected voidretryOutstandingRequests(String sessionId) protected AbstractWebsocketClient.PingRegistrationschedulePing(jakarta.websocket.Session session) protected <R extends RequestResult>
CompletableFuture<R> protected <R extends RequestResult>
RsendAndWait(Request request) protected CompletableFuture<Void> sendCommand(Command command) protected voidsendPing(jakarta.websocket.Session session) toString()protected voidtryPublishMetrics(JsonType message, Metadata metadata)
-
Field Details
-
defaultWebSocketContainer
public static jakarta.websocket.WebSocketContainer defaultWebSocketContainer -
defaultObjectMapper
public static com.fasterxml.jackson.databind.ObjectMapper defaultObjectMapper
-
-
Constructor Details
-
AbstractWebsocketClient
Creates a WebSocket client using the given endpoint URI, client implementation, and a flag to allow metrics. Uses a default WebSocket container, default object mapper, and a single WebSocket session.- Parameters:
endpointUri- the URI of the WebSocket endpoint to connect toclient- the client implementation that provides configuration and gateway accessallowMetrics- whether metrics should be published for each request
-
AbstractWebsocketClient
public AbstractWebsocketClient(URI endpointUri, WebSocketClient client, boolean allowMetrics, int numberOfSessions) Creates a WebSocket client with multiple parallel sessions using default settings. This constructor allows you to specify the number of WebSocket sessions to use, which is useful for increasing throughput and isolating message streams.- Parameters:
endpointUri- the URI of the WebSocket endpoint to connect toclient- the client implementation that provides configuration and gateway accessallowMetrics- whether metrics should be published for each requestnumberOfSessions- the number of WebSocket sessions to maintain concurrently
-
AbstractWebsocketClient
public AbstractWebsocketClient(jakarta.websocket.WebSocketContainer container, URI endpointUri, WebSocketClient client, boolean allowMetrics, Duration reconnectDelay, com.fasterxml.jackson.databind.ObjectMapper objectMapper, int numberOfSessions) Constructs a WebSocket client with fine-grained control over connection setup. This constructor allows you to specify a custom container, reconnect delay, object mapper, and session count. It is primarily used for advanced configuration or test scenarios.- Parameters:
container- the WebSocket container to use for establishing connectionsendpointUri- the WebSocket server endpointclient- the client providing config and access to the Fluxzero RuntimeallowMetrics- flag to enable or disable automatic metrics publishingreconnectDelay- the delay between reconnect attempts if the connection is lostobjectMapper- the Jackson object mapper for (de)serializing requests and responsesnumberOfSessions- the number of WebSocket sessions to establish in parallel
-
-
Method Details
-
send
-
sendAndWait
-
sendCommand
-
onMessage
public void onMessage(byte[] bytes, jakarta.websocket.Session session) -
handleResult
-
onOpen
public void onOpen(jakarta.websocket.Session session) -
schedulePing
-
sendPing
protected void sendPing(jakarta.websocket.Session session) -
abort
protected void abort(jakarta.websocket.Session session) -
onPong
public void onPong(jakarta.websocket.PongMessage message, jakarta.websocket.Session session) -
onClose
public void onClose(jakarta.websocket.Session session, jakarta.websocket.CloseReason closeReason) -
retryOutstandingRequests
-
onError
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
toString
-
close
protected void close(boolean clearOutstandingRequests) -
tryPublishMetrics
-
metricsMetadata
-