Principle:A2aproject A2a java Stream Lifecycle Management
| Knowledge Sources | |
|---|---|
| Domains | Distributed_Systems, Resource_Management |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
A hook-based lifecycle management pattern for event streams that allows server-side code to observe subscriber connections, monitor subscriber counts, and gracefully close streams based on custom policies.
Description
Stream Lifecycle Management addresses the problem of unbounded stream retention: in a multi-subscriber event streaming system, streams stay open as long as any subscriber is connected. Without a lifecycle management mechanism, abandoned subscribers, resource exhaustion, or policy limits (e.g., maximum concurrent subscribers) cannot be enforced.
The `TaskStreamLifecycleHook` interface provides three callbacks — `onSubscribe`, `onUnsubscribe`, and `onEvent` — that fire at critical points in a stream's lifecycle. The `StreamCloseHandle` passed to each callback allows the hook implementation to close all streams for a task and query the active subscriber count.
In the broader landscape, this is an instance of the Observer pattern combined with a resource handle. It parallels gRPC's `ServerCall.Listener` for stream lifecycle events and WebSocket `@OnOpen`/`@OnClose` annotations. The key design choice is that the hook can proactively close streams (e.g., when a subscriber count threshold is reached), not just observe.
Usage
Use this principle when:
- Limiting the number of concurrent subscribers per task
- Implementing stream timeouts (close streams after inactivity)
- Cleaning up resources when all subscribers disconnect
- Enforcing access control on stream subscription
Theoretical Basis
The lifecycle hook fires at three points:
onSubscribe(taskId, handle):
// Called synchronously during MainQueue.tap()
// AFTER the ChildQueue is added to children list
// handle.getActiveSubscriberCount() includes the new subscriber
// Calling handle.closeStreams() here closes the new ChildQueue before it's returned
onUnsubscribe(taskId, handle):
// Called synchronously inside ChildQueue.close()
// On whichever thread closes the child
// Calling handle.closeStreams() here is safe but has no effect (reentrancy guard)
onEvent(taskId, event, handle):
// Called on MainEventBusProcessor thread
// AFTER event persisted and distributed to all ChildQueues
// Must return promptly to avoid stalling event distribution
The `StreamCloseHandle` provides:
interface StreamCloseHandle:
void closeStreams() // Close all ChildQueues for this task
int getActiveSubscriberCount() // Current subscriber count
Threading considerations:
onSubscribe: MainQueue.tap() thread (synchronous, before queue returned)
onEvent: MainEventBusProcessor thread (single-threaded, after distribution)
onUnsubscribe: ChildQueue.close() thread (whichever thread closes the child)
Because onSubscribe and onEvent run on different threads,
a fast event may cause onEvent to fire concurrently with onSubscribe.
Hook implementations MUST be thread-safe.
Practical Guide
To implement a stream lifecycle hook:
- Implement `TaskStreamLifecycleHook` with custom logic in `onSubscribe`, `onUnsubscribe`, `onEvent`
- Register as a CDI alternative: `@ApplicationScoped @Alternative @Priority(1)`
- Use `handle.getActiveSubscriberCount()` to check subscriber thresholds
- Call `handle.closeStreams()` to gracefully close all streams for a task
Related Pages
Implemented By
- Implementation:A2aproject_A2a_java_TaskStreamLifecycleHook
- Implementation:A2aproject_A2a_java_EventQueue
- Implementation:A2aproject_A2a_java_QueueManager_SPI
- Implementation:A2aproject_A2a_java_ServerCallContext