Migrating SSE from 0.1.4 to 0.2.0
Version 0.2.0 adds Trading account-activity and Market Data corporate-actions SSE, and moves
Broker SSE onto the shared bounded, reconnecting transport. Existing Broker method signatures
remain available, but applications should review cancellation, callback scheduling, failure
handling, and uses of the exposed OkHttp EventSource.
BrokerSseSubscription now implements the shared AlpacaSseSubscription interface. This is
additive: existing Broker declarations continue to compile, while shared lifecycle code can accept
Broker, Trading, and Market Data subscriptions through one type.
Recommended migration
Close the subscription directly
eventSource() remains binary/source compatible, but now returns a stable facade rather than the
live OkHttp implementation. Replace cancellation through that facade:
// 0.1.4
subscription.eventSource().cancel();
// 0.2.0
subscription.close();
Do not cast the facade, compare its identity, or rely on implementation-specific methods. Its
supported operations are request() and cancel().
The repository includes a conservative scanner/codemod:
# Report safe rewrites and manual-review findings.
python3 scripts/migrate_sse_0_1_4_to_0_2_0.py src/main/java
# Apply only proven BrokerSseSubscription rewrites.
python3 scripts/migrate_sse_0_1_4_to_0_2_0.py --write src/main/java
# CI/reporting modes.
python3 scripts/migrate_sse_0_1_4_to_0_2_0.py --check --json src/main/java
The tool rewrites only a bare identifier receiver with exactly one explicit
BrokerSseSubscription declaration in the file and in the call's lexical scope, recognized through
either the exact import or the fully qualified class name. Automatic rewrites are limited to
parameters and local variables; fields are report-only because a text scanner cannot safely resolve
inheritance and member lookup. The same identifier must have no other detected binder anywhere in
the file. It reports SSE001 and leaves unchanged qualified or chained access
(this.subscription or other.subscription), fields, out-of-scope declarations, shadowed names or
same-named local/nested types, var, lambda parameters, comments inside the call chain, and any
declaration it cannot prove unique. Control-flow header declarations are also report-only because
an unbraced statement's scope cannot be established safely by this text scanner. Same-named type
parameters and files containing Java Unicode escapes are report-only because Java performs Unicode
translation before tokenization. Rewrites preserve the source file's existing line endings. The
tool also reports ambiguous imports,
EventSource casts/identity assumptions, raw Gson exception checks, deep OkHttp Response use,
blocking callbacks, generated SSE invocations or method references, and listeners overriding both
rich and legacy callbacks. Invocations and method references on a uniquely bound imported or fully
qualified BrokerEventsSseClient using a bare receiver are recognized as the handwritten
replacement. A this.receiver call is recognized only when the scanner can prove the current class
declares that handwritten-client field; inherited and other-qualified receivers remain report-only.
Unqualified generated-method calls are reported conservatively, including calls from indirect and
anonymous generated API subclasses. Every generated SSE invocation or method reference is
reported. These deliberate false positives keep --write source-safe; migrate SSE001 findings
manually.
Missing input paths and explicit non-Java files fail with exit code 2 instead of producing an empty
success report.
Await startup without a latch
Replace an onOpen latch with the first-connection future and a caller-controlled timeout:
var subscription = events.subscribeToActivities(listener);
var connection = subscription.opened().get(10, TimeUnit.SECONDS);
System.out.printf("%d %s%n", connection.statusCode(), connection.uri());
opened() retains the first accepted connection. connection() returns the latest accepted
connection after reconnects. User closure before the first response cancels the defensive
opened() future; cancelling a returned copy does not close the subscription.
Prefer structured Broker callbacks
Legacy listeners continue to work:
- malformed events delegate their original deserialization cause to
onFailure(Throwable, null); - non-successful HTTP responses delegate to
onFailure(null, boundedResponse); - user cancellation delegates to
onFailure(new IOException("canceled"), null); - other normal remote/bounded endings delegate to
onClosed().
The old stripped OkHttp response from cancellation is not reproduced. Protocol/content-type and
resource-limit failures now use the SDK's structured exception hierarchy and do not expose a live
response. HTTP compatibility responses are reconstructed from bounded status, headers, request ID,
and body data; handshake(), networkResponse(), cacheResponse(), and related transport internals
are unavailable.
New listeners can override the richer methods instead:
new BrokerSseEventListener<TradeUpdateEventV2>() {
@Override
public void onEventFailure(AlpacaSseDeserializationException failure) {
System.err.printf(
"event %s (%s) was malformed%n", failure.eventId(), failure.eventType());
}
@Override
public void onHttpFailure(
AlpacaSseHttpException failure, okhttp3.Response boundedResponse) {
System.err.printf("HTTP %d request=%s%n",
failure.statusCode(), failure.requestId());
}
@Override
public void onClosed(AlpacaSseCloseResult result) {
System.out.println("closed: " + result.reason());
}
};
The transport invokes only the richest applicable callback. Its default method performs legacy delegation, so overriding both a rich method and its legacy target does not cause duplicate delivery.
User cancellation remains a successful structured lifecycle result:
subscription.close();
var result = subscription.completion().join();
assert result.reason() == AlpacaSseCloseResult.Reason.USER_CLOSED;
That successful completion is independent from the legacy cancellation-shaped onFailure
callback. Override onClosed(AlpacaSseCloseResult) when an application does not want the legacy
callback shape.
Callback and threading changes
SSE callbacks are serialized per subscription. Parsing waits for each data callback, providing backpressure and deterministic callback order. The default executor runs event callbacks directly on a transport thread. Opening and terminal callback delivery that is not already queued starts through separate bounded SDK dispatchers.
Use an application-owned executor when callbacks block:
var callbacks = Executors.newSingleThreadExecutor();
var events = AlpacaClientFactory.tradingEventsSseClient(
tradingClient, sseOptions, callbacks);
The SDK does not shut down that executor. Sharing one single-thread executor between subscriptions intentionally serializes their callbacks. It cannot stall lifecycle timers or cancellation, but an active event callback backpressures response parsing and can delay detection of remote EOF and the following reconnect.
Lifecycle completion is deliberately separate from listener delivery:
close()selectsUSER_CLOSEDwhen it wins the terminal transition, cancels active/future transport work, and waits forcompletion()to settle before returning; if failure already won, it observes the exceptional completion without rethrowing it;- a timeout or terminal failure also completes lifecycle state without waiting for a blocked callback;
- terminal listener delivery remains ordered after callbacks that were already admitted;
- no event, comment, retry, or reconnect callback is admitted after the terminal transition.
Terminal listener delivery is admitted before lifecycle futures notify synchronous continuations,
then waits for lifecycle completion to settle before invoking the listener. Closing while opening
callback startup is queued also releases the HTTP response thread without violating callback order;
the already-admitted callbacks may finish later. Completion settlement is handed off from an active
callback, so a callback may close even when a synchronous completion continuation waits for the
ordered terminal listener. Each opened() or completion() call returns an independently settled
defensive future; cancelling or manually completing one does not affect the subscription or another
saved future. Lifecycle completion uses isolated, direct-handoff SDK workers, so a blocked
continuation cannot delay another saved lifecycle future, queue another subscription's settlement,
or occupy the scheduler. Synchronous continuations must remain short because concurrently blocked
continuations consume additional daemon workers. The related lifecycle wait above is supported; use
an async continuation with an application-owned executor for unrelated or potentially unbounded
blocking work.
Once response headers are accepted, opened() succeeds with that connection even if closure races
with opening. Its admitted onOpen callback starts independently before opened() continuations
run and remains ordered before the terminal listener callback. A synchronous continuation can
therefore close the subscription and await onClosed without preventing callback delivery.
The opening and terminal dispatchers have finite primary workers and queue capacity. Saturation from
unrelated subscriptions hands an already-admitted callback to the transport lifecycle executor
instead of failing the opening subscription or dropping terminal listener delivery. Blocking
callbacks can therefore consume additional daemon workers during overload, so callbacks should
remain non-blocking; an application-owned executor can isolate opening and event work. Rejection by
that application-owned executor during opening or event delivery remains an
AlpacaSseCallbackException; lifecycle futures remain the authoritative terminal signal if the
application executor rejects terminal delivery.
Each subscription derives an HTTP client with a private, one-call daemon dispatcher and shuts that
dispatcher down at terminal completion. Close streams through AlpacaSseSubscription; the supplied
client's dispatcher().cancelAll() does not own SSE calls. The derived client retains caller
interceptors but removes the application-level AlpacaRetryInterceptor so it cannot layer retries
beneath AlpacaSseReconnectPolicy. Configure any other custom retry interceptor to exclude SSE
requests.
A listener RuntimeException is logged and considered an application error, not a transport
failure. A non-runtime fatal throwable is logged at severe level and makes non-terminal dispatch
fail; terminal lifecycle completion remains authoritative when a terminal listener fails. The event
is considered delivered after a runtime failure and its resume ID advances. An executor rejection
is a terminal AlpacaSseCallbackException. Persist work transactionally and deduplicate by event ID
when exactly-once effects matter.
Reconnect and decode behavior
Trading SSE reconnects on transient failures by default and sends its committed cursor as the
documented since_id query parameter and in Last-Event-ID. Date-bounded retries retain the
original date range and can replay events. If an empty SSE id: clears the committed cursor, an
unbounded request retains its original since or sinceId lower bound on reconnect rather than
risk a cursorless gap; deduplicate because this safe replay can redeliver earlier events. Existing
cursorless live requests fail closed after an empty id: or when an established connection drops
before supplying any event ID, because the Trading endpoint does not replay history without a lower
bound. Existing Broker constructors keep the 0.1.4 one-connection default; pass
AlpacaSseOptions to opt into Broker reconnects.
AlpacaSseOptions.initialLastEventId rejects an empty value. Do not combine an initial ID with a
date-bounded Trading request; 0.2.0 rejects that ambiguous combination before opening a
connection. Use an ID-bounded request instead, and keep the initial ID at or before its untilId.
Corporate Actions also rejects an initial ID after an ID-bounded request's untilId.
Trading, Corporate Actions, and Broker malformed payloads invoke onEventFailure, advance the
cursor after callback invocation, and keep the healthy connection open. The SDK logs each skipped
event. AlpacaSseListener.onEventFailure defaults to no action; Broker's
BrokerSseEventListener.onEventFailure instead delegates to its legacy two-argument onFailure.
Override the rich callback to persist or alert on the data gap when complete event accounting
matters. If it throws a RuntimeException, the cursor still advances and delivery continues.
AlpacaSseListener.onFailure(Throwable) remains terminal; Broker's legacy
onFailure(Throwable, Response) retains its documented non-terminal malformed-event delegation.
Broker admin-action events retain SubscribeToAdminActionSSE200ResponseInner but now select its
concrete generated model from the required type field; missing, unknown, or schema-invalid types
follow that same malformed-event policy.
Both Trading and Broker retain activity type DIVTXEX while representing its details as
CDIVActivityV2; the pinned OAS accepts the type but does not provide a dedicated detail schema.
Servers may replay the last event inclusively. Resume support provides at-least-once delivery, not exactly-once processing.
Replace generated blocking SSE calls
OpenAPI-generated SSE methods buffer a response like an ordinary REST call and can wait indefinitely. Replace them with the handwritten clients:
var tradingEvents = AlpacaClientFactory.tradingEventsSseClient(tradingClient);
var tradingSubscription = tradingEvents.subscribeToActivities(listener);
var corporateActions =
AlpacaClientFactory.corporateActionsSseClient(dataClient);
var corporateActionsSubscription =
corporateActions.subscribeToCorporateActions(corporateActionsListener);
var brokerEvents = AlpacaClientFactory.brokerEventsSseClient(brokerClient);
var brokerSubscription =
brokerEvents.subscribeToTradeEvents(BrokerSseDateOptions.empty(), brokerListener);
See Streaming & Events for all stream types and Broker for Broker endpoint-specific examples.
Review generated validation changes
The 0.2.0 generated models enforce two newly required payload fields:
- Broker and Trading
OptionContractrequiresppind; - Broker and Trading
CommonFixedIncomeInterestActivityV2requiresinterest_type.
Update stored JSON, fixtures, mocks, and custom integrations before upgrading. Generated
validateJsonElement methods reject payloads that omit these fields even when application code does
not read them. See the complete generated-symbol list in the repository
MIGRATIONS.md.
Migration checklist
- Replace
eventSource().cancel()withclose(). - Review
EventSourcecasts, identity checks, and cancellation-response assumptions. - Replace startup latches with bounded
opened()waits. - Choose rich Broker callbacks or confirm the legacy delegation shapes.
- Move blocking callback work to an application-owned executor.
- Confirm listener exceptions may advance the resume cursor.
- Replace generated blocking SSE methods.
- Add
ppindandinterest_typeto affected stored or mocked generated-model payloads. - Select the Market Data corporate-actions stream environment explicitly when production is not appropriate.
- Test reconnect/replay behavior and deduplicate by event ID.