Class AbstractWebsocketClient
java.lang.Object
jakarta.websocket.Endpoint
io.fluxzero.sdk.common.websocket.AbstractWebsocketClient
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
WebSocketEventStoreClient, WebsocketGatewayClient, WebsocketKeyValueClient, WebsocketSchedulingClient, WebSocketSearchClient, WebsocketTrackingClient
public abstract class AbstractWebsocketClient
extends jakarta.websocket.Endpoint
implements AutoCloseable
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 static final recordprotected static classprotected class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Stringprotected static final Stringprotected static final intprotected static final Durationstatic com.fasterxml.jackson.databind.ObjectMapperstatic jakarta.websocket.WebSocketContainerprotected static final Stringprotected static final Stringprotected static final String -
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 voidvoidclose()protected voidclose(boolean clearOutstandingRequests) protected jakarta.websocket.SessionconnectToServer(jakarta.websocket.WebSocketContainer container, URI endpointUri) protected RetryConfigurationcreateConnectionRetryConfiguration(URI endpointUri, Duration reconnectDelay) protected static AbstractWebsocketClient.ConnectionSetupcreateConnectionSetup(WebSocketClient.ClientConfig clientConfig) protected CompressionAlgorithmgetCompressionAlgorithm(jakarta.websocket.Session session) protected Durationprotected StringgetNegotiatedSessionId(jakarta.websocket.Session session) getRuntimeVersion(jakarta.websocket.Session session) protected voidhandleClose(jakarta.websocket.Session session, jakarta.websocket.CloseReason closeReason) protected voidhandleError(jakarta.websocket.Session session, Throwable e) protected voidhandleMessage(byte[] bytes, jakarta.websocket.Session session) protected voidhandlePong(jakarta.websocket.Session session) protected voidhandleResult(RequestResult result, String batchId, String sessionId) protected voidlogConnectionRetryStatus(URI endpointUri, RetryStatus status) protected voidlogSuccessfulReconnect(URI endpointUri, RetryStatus status) protected MetadatavoidonClose(jakarta.websocket.Session session, jakarta.websocket.CloseReason closeReason) voidvoidonMessage(byte[] bytes, jakarta.websocket.Session session) voidonOpen(jakarta.websocket.Session session, jakarta.websocket.EndpointConfig config) voidonPong(jakarta.websocket.PongMessage message, jakarta.websocket.Session session) protected voidretryOutstandingRequests(String sessionId) protected voidretryOutstandingRequestsAsync(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
-
CONNECTION_TIMEOUT_FAILSAFE_GRACE
-
CONNECTION_RETRY_LOG_INTERVAL
protected static final int CONNECTION_RETRY_LOG_INTERVAL- See Also:
-
CLIENT_HANDSHAKE_CONFIGURATOR_USER_PROPERTY
-
CLIENT_SESSION_ID_USER_PROPERTY
-
RUNTIME_SESSION_ID_USER_PROPERTY
-
RUNTIME_VERSION_USER_PROPERTY
-
SELECTED_COMPRESSION_ALGORITHM_USER_PROPERTY
-
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
-
createConnectionRetryConfiguration
protected RetryConfiguration createConnectionRetryConfiguration(URI endpointUri, Duration reconnectDelay) -
logSuccessfulReconnect
-
logConnectionRetryStatus
-
connectToServer
-
getConnectionTimeoutFailsafeGrace
-
onOpen
public void onOpen(jakarta.websocket.Session session, jakarta.websocket.EndpointConfig config) - Specified by:
onOpenin classjakarta.websocket.Endpoint
-
send
-
sendAndWait
-
sendCommand
-
onMessage
public void onMessage(byte[] bytes, jakarta.websocket.Session session) -
handleMessage
protected void handleMessage(byte[] bytes, jakarta.websocket.Session session) -
handleResult
-
schedulePing
-
sendPing
protected void sendPing(jakarta.websocket.Session session) -
onPong
public void onPong(jakarta.websocket.PongMessage message, jakarta.websocket.Session session) -
handlePong
protected void handlePong(jakarta.websocket.Session session) -
abort
-
onClose
public void onClose(jakarta.websocket.Session session, jakarta.websocket.CloseReason closeReason) - Overrides:
onClosein classjakarta.websocket.Endpoint
-
handleClose
protected void handleClose(jakarta.websocket.Session session, jakarta.websocket.CloseReason closeReason) -
retryOutstandingRequestsAsync
-
retryOutstandingRequests
-
onError
- Overrides:
onErrorin classjakarta.websocket.Endpoint
-
handleError
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
toString
-
close
protected void close(boolean clearOutstandingRequests) -
tryPublishMetrics
-
metricsMetadata
-
createConnectionSetup
protected static AbstractWebsocketClient.ConnectionSetup createConnectionSetup(WebSocketClient.ClientConfig clientConfig) -
getNegotiatedSessionId
-
getCompressionAlgorithm
-
getRuntimeVersion
-