This example demonstrates TaskStreamLifecycleHook, a CDI-discoverable hook that lets you observe and control streaming connections for a task. The server closes all active streams when 3 subscribers connect to the same task.
For example, in order to save resources associated with streaming connections, you might want to close streams:
- If no events are received for a Task in a given timeframe. This can help if you have a lot of Tasks taking a very long time to complete
- Stopping rogue client opening too many subscriptions to the same Task
- Java 17 or higher
- Maven
Server — An agent sends 20 progress messages (one every 500ms). A CloseStreamsHook monitors subscriber count and calls StreamCloseHandle.closeStreams() when 3 subscribers are connected, gracefully closing all streams.
Client — Connects 3 subscribers sequentially:
- Subscriber 1 sends a message (creates the task, starts streaming)
- Subscriber 2 subscribes to the same task after 1.5 seconds
- Both subscribers receive progress messages for 2 seconds
- Subscriber 3 subscribes — the hook fires and closes all streams
- All 3 subscribers see their streams end gracefully
From the repository root:
mvn clean install -DskipTestscd examples/stream-lifecycle/server
mvn quarkus:devIn a separate terminal:
cd examples/stream-lifecycle/client
mvn exec:javaThe exact event interleaving depends on timing, but you should see something like:
Resolved agent card: Stream Lifecycle Demo Agent
[Sub-1] Sending message to create task...
[Sub-1] StatusUpdate — TASK_STATE_WORKING
=== Task created: <task-id> ===
[Sub-1] ArtifactEvent — Progress update 1/20
[Sub-1] ArtifactEvent — Progress update 2/20
[Sub-1] ArtifactEvent — Progress update 3/20
[Sub-2] Subscribing to task <task-id>...
=== Subscribers 1 and 2 are active — receiving events... ===
[Sub-2] TaskEvent — state: TASK_STATE_WORKING, id: <task-id>
[Sub-1] ArtifactEvent — Progress update 4/20
[Sub-2] ArtifactEvent — Progress update 4/20
[Sub-1] ArtifactEvent — Progress update 5/20
[Sub-2] ArtifactEvent — Progress update 5/20
...
[Sub-3] Subscribing to task <task-id> (will trigger stream close)...
[Sub-1] Stream closed.
[Sub-2] Stream closed.
[Sub-3] Stream closed.
=== All streams closed. ===
[HOOK] Subscriber added for task <id>. Active subscribers: 1
[AGENT] Starting execution for task <id>
[AGENT] Sending: Progress update 1/20
[HOOK] Event distributed for task <id>: Message (subscribers: 1)
...
[HOOK] Subscriber added for task <id>. Active subscribers: 2
...
[HOOK] Subscriber added for task <id>. Active subscribers: 3
[HOOK] Subscriber count reached 3 for task <id> — closing all streams
[HOOK] Subscriber removed for task <id>. Active subscribers: 2
[HOOK] Subscriber removed for task <id>. Active subscribers: 1
[HOOK] Subscriber removed for task <id>. Active subscribers: 0
Set quarkus.agentcard.protocol on both server and client (must match). Available values:
| Value | Transport |
|---|---|
JSONRPC |
JSON-RPC 2.0 (default) |
GRPC |
gRPC |
HTTP+JSON |
HTTP+JSON/REST |
# Server — gRPC example
mvn quarkus:dev -Dquarkus.agentcard.protocol=GRPC
# Client — must use the same value
mvn exec:java -Dquarkus.agentcard.protocol=GRPC| File | Description |
|---|---|
server/.../CloseStreamsHook.java |
TaskStreamLifecycleHook implementation — closes streams at 3 subscribers |
server/.../AgentExecutorProducer.java |
Agent that sends 20 progress messages over 10 seconds |
server/.../AgentCardProducer.java |
Agent card with streaming enabled |
client/.../StreamLifecycleClient.java |
Client that creates 3 subscribers and logs events |
The server module includes @QuarkusTest integration tests that verify the hook behavior across all three transports:
cd examples/stream-lifecycle/server
mvn test