Implementation:A2aproject A2a java TaskStreamLifecycleHook
| Knowledge Sources | |
|---|---|
| Domains | Server, Resource_Management |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Concrete server-side interface for observing and controlling the lifecycle of task event streams, enabling subscriber count monitoring and proactive stream closure.
Description
`TaskStreamLifecycleHook` is a CDI-injectable interface that fires callbacks at three critical points in a task stream's lifecycle: when a subscriber connects (`onSubscribe`), when a subscriber disconnects (`onUnsubscribe`), and after each event is processed (`onEvent`).
Each callback receives a `StreamCloseHandle` that provides:
- `closeStreams()` — closes all ChildQueues (active subscriber streams) for the task
- `getActiveSubscriberCount()` — returns the current number of active subscribers
The default implementation is a no-op. Custom implementations are registered as CDI alternatives with `@Alternative @Priority(1)`.
Threading model:
- `onSubscribe` runs synchronously on the `MainQueue.tap()` thread, before the ChildQueue is returned
- `onEvent` runs on the `MainEventBusProcessor` thread (single-threaded, after distribution)
- `onUnsubscribe` runs on whichever thread closes the ChildQueue
- Because `onSubscribe` and `onEvent` run on different threads, hook implementations must be thread-safe
Usage
- Subscriber limit: Close streams when subscriber count exceeds a threshold (e.g., max 3 subscribers)
- Stream timeout: Track last event time and close streams after inactivity
- Resource cleanup: Release resources when all subscribers disconnect
- Access control: Validate subscriber authorization on subscribe
Code Reference
Source Location
- Repository: a2a-java
- File: `server-common/src/main/java/org/a2aproject/sdk/server/events/TaskStreamLifecycleHook.java`
- Lines: L53-89
Signature
public interface TaskStreamLifecycleHook {
void onSubscribe(String taskId, StreamCloseHandle handle);
void onUnsubscribe(String taskId, StreamCloseHandle handle);
void onEvent(String taskId, Event event, StreamCloseHandle handle);
}
// StreamCloseHandle (companion interface):
// void closeStreams();
// int getActiveSubscriberCount();
Import
import org.a2aproject.sdk.server.events.TaskStreamLifecycleHook;
import org.a2aproject.sdk.server.events.StreamCloseHandle;
import org.a2aproject.sdk.spec.Event;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| taskId | String | Yes | Task identifier for the stream |
| handle | StreamCloseHandle | Yes | Handle to close streams and query subscriber count |
| event | Event | Yes (onEvent only) | The event that was just processed |
Outputs
| Name | Type | Description |
|---|---|---|
| Side effects | handle.closeStreams() | Closes all ChildQueues for the task |
| Subscriber count | handle.getActiveSubscriberCount() | Current active subscriber count |
Usage Examples
Subscriber Limit Hook (from stream-lifecycle example)
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative;
import jakarta.annotation.Priority;
import org.a2aproject.sdk.server.events.TaskStreamLifecycleHook;
import org.a2aproject.sdk.server.events.StreamCloseHandle;
@ApplicationScoped
@Alternative
@Priority(1)
public class CloseStreamsHook implements TaskStreamLifecycleHook {
private static final int MAX_SUBSCRIBERS = 3;
@Override
public void onSubscribe(String taskId, StreamCloseHandle handle) {
if (handle.getActiveSubscriberCount() > MAX_SUBSCRIBERS) {
System.out.println("Max subscribers reached, closing streams for task " + taskId);
handle.closeStreams();
}
}
@Override
public void onUnsubscribe(String taskId, StreamCloseHandle handle) {
System.out.println("Subscriber disconnected for task " + taskId
+ ", remaining: " + handle.getActiveSubscriberCount());
}
@Override
public void onEvent(String taskId, Event event, StreamCloseHandle handle) {
// Must return promptly — runs on MainEventBusProcessor thread
}
}
No-Op Default
// The default implementation does nothing:
public class NoOpStreamLifecycleHook implements TaskStreamLifecycleHook {
@Override
public void onSubscribe(String taskId, StreamCloseHandle handle) { }
@Override
public void onUnsubscribe(String taskId, StreamCloseHandle handle) { }
@Override
public void onEvent(String taskId, Event event, StreamCloseHandle handle) { }
}