Class AbstractMarketDataStream
java.lang.Object
markets.alpaca.client.ws.internal.AbstractAlpacaStream
markets.alpaca.client.ws.internal.AbstractMarketDataStream
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
AlpacaCryptoStream,AlpacaNewsStream,AlpacaStockStream
Shared protocol logic for the three market-data WebSocket streams (stock, crypto, and news).
All three streams follow the same message envelope:
- Server sends
[{"T":"success","msg":"connected"}]. - Client sends
{"action":"auth","key":"...","secret":"..."}. - Server sends
[{"T":"success","msg":"authenticated"}]. - Client sends subscribe/unsubscribe messages.
- Server streams data as JSON arrays; each element has a
Tdiscriminator.
This class handles the auth/connected handshake, subscription state tracking (updated from
confirmed subscription messages), and reconnect-aware re-subscription. Subclasses implement
data-message dispatch via dispatchDataMessage(com.google.gson.JsonObject, java.lang.String).
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractMarketDataStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials) protectedAbstractMarketDataStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamReconnectPolicy reconnectPolicy) protectedAbstractMarketDataStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor) protectedAbstractMarketDataStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor, String streamName) protectedAbstractMarketDataStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor, ScheduledExecutorService scheduler) -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voiddispatchDataMessage(com.google.gson.JsonObject msg, String type) Called for each data message element with a non-controlTvalue.Returns the current server-confirmed subscriptions, keyed by channel name.protected final voidhandleClose(int code, String reason, boolean willReconnect) Called when the connection closes, either cleanly or due to an error.protected abstract voidhandleMarketDataClose(int code, String reason, boolean willReconnect) protected voidhandleOpen(okhttp3.WebSocket ws) Called when the WebSocket connection is open.protected final voidhandleText(okhttp3.WebSocket ws, String text) Called for each text frame received from the server.protected abstract voidprotected abstract voidprotected abstract voidnotifyError(int code, String message) protected abstract voidnotifySubscriptionConfirmed(Map<String, List<String>> subscriptions) protected voidresubscribeAfterAuth(okhttp3.WebSocket ws) Called after (re-)authentication to re-subscribe with the previously confirmed state.protected voidSends a subscribe or unsubscribe message and returns immediately.Methods inherited from class markets.alpaca.client.ws.internal.AbstractAlpacaStream
authenticationFuture, authenticationResultFuture, buildRequest, close, closeTerminal, closeTerminal, completeAuthentication, completeAuthentication, connect, handleBytes, invokeCallback, isClosed, isReconnecting, notifyReconnected, notifyReconnecting, requireOpenForCommand, resetReconnectAttempts, send, waitForAuthentication, waitForAuthenticationResult
-
Field Details
-
GSON
protected static final com.google.gson.Gson GSON
-
-
Constructor Details
-
AbstractMarketDataStream
-
AbstractMarketDataStream
protected AbstractMarketDataStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamReconnectPolicy reconnectPolicy) -
AbstractMarketDataStream
protected AbstractMarketDataStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor) -
AbstractMarketDataStream
protected AbstractMarketDataStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor, String streamName) -
AbstractMarketDataStream
protected AbstractMarketDataStream(okhttp3.OkHttpClient httpClient, AlpacaCredentials credentials, AlpacaStreamReconnectPolicy reconnectPolicy, Executor callbackExecutor, ScheduledExecutorService scheduler)
-
-
Method Details
-
handleOpen
protected void handleOpen(okhttp3.WebSocket ws) Description copied from class:AbstractAlpacaStreamCalled when the WebSocket connection is open.- Specified by:
handleOpenin classAbstractAlpacaStream
-
handleText
Description copied from class:AbstractAlpacaStreamCalled for each text frame received from the server.- Specified by:
handleTextin classAbstractAlpacaStream
-
handleClose
Description copied from class:AbstractAlpacaStreamCalled when the connection closes, either cleanly or due to an error.- Specified by:
handleClosein classAbstractAlpacaStream- Parameters:
code- WebSocket close code, or-1for transport failuresreason- human-readable close reason
-
getConfirmedSubscriptions
Returns the current server-confirmed subscriptions, keyed by channel name. -
sendSubscriptionMessage
Sends a subscribe or unsubscribe message and returns immediately. The confirmed state is updated when the server responds with a subscription message.- Parameters:
action-"subscribe"or"unsubscribe"payload- channel-to-symbols map (channels with empty sets are omitted)
-
resubscribeAfterAuth
protected void resubscribeAfterAuth(okhttp3.WebSocket ws) Called after (re-)authentication to re-subscribe with the previously confirmed state. Subclasses may override to add their own reconnect-subscribe payload building. -
dispatchDataMessage
Called for each data message element with a non-controlTvalue.- Parameters:
msg- the JSON object for this messagetype- theTfield value (e.g."t","q","b")
-
notifyConnected
protected abstract void notifyConnected() -
notifyAuthenticated
protected abstract void notifyAuthenticated() -
notifySubscriptionConfirmed
-
notifyError
-
handleMarketDataClose
-