|
| 1 | +# Stream Lifecycle Hook Example |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +For example, in order to save resources associated with streaming connections, you might want to close streams: |
| 6 | + |
| 7 | +* 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 |
| 8 | +* Stopping rogue client opening too many subscriptions to the same Task |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +- Java 17 or higher |
| 13 | +- Maven |
| 14 | + |
| 15 | +## What It Does |
| 16 | + |
| 17 | +**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. |
| 18 | + |
| 19 | +**Client** — Connects 3 subscribers sequentially: |
| 20 | +1. Subscriber 1 sends a message (creates the task, starts streaming) |
| 21 | +2. Subscriber 2 subscribes to the same task after 1.5 seconds |
| 22 | +3. Both subscribers receive progress messages for 2 seconds |
| 23 | +4. Subscriber 3 subscribes — the hook fires and closes all streams |
| 24 | +5. All 3 subscribers see their streams end gracefully |
| 25 | + |
| 26 | +## Run the Example |
| 27 | + |
| 28 | +### 1. Build the SDK |
| 29 | + |
| 30 | +From the repository root: |
| 31 | + |
| 32 | +```bash |
| 33 | +mvn clean install -DskipTests |
| 34 | +``` |
| 35 | + |
| 36 | +### 2. Start the Server |
| 37 | + |
| 38 | +```bash |
| 39 | +cd examples/stream-lifecycle/server |
| 40 | +mvn quarkus:dev |
| 41 | +``` |
| 42 | + |
| 43 | +### 3. Run the Client |
| 44 | + |
| 45 | +In a separate terminal: |
| 46 | + |
| 47 | +```bash |
| 48 | +cd examples/stream-lifecycle/client |
| 49 | +mvn exec:java |
| 50 | +``` |
| 51 | + |
| 52 | +### Expected Output (Client) |
| 53 | + |
| 54 | +The exact event interleaving depends on timing, but you should see something like: |
| 55 | + |
| 56 | +``` |
| 57 | +Resolved agent card: Stream Lifecycle Demo Agent |
| 58 | +[Sub-1] Sending message to create task... |
| 59 | +[Sub-1] StatusUpdate — TASK_STATE_WORKING |
| 60 | +
|
| 61 | +=== Task created: <task-id> === |
| 62 | +
|
| 63 | +[Sub-1] ArtifactEvent — Progress update 1/20 |
| 64 | +[Sub-1] ArtifactEvent — Progress update 2/20 |
| 65 | +[Sub-1] ArtifactEvent — Progress update 3/20 |
| 66 | +[Sub-2] Subscribing to task <task-id>... |
| 67 | +
|
| 68 | +=== Subscribers 1 and 2 are active — receiving events... === |
| 69 | +
|
| 70 | +[Sub-2] TaskEvent — state: TASK_STATE_WORKING, id: <task-id> |
| 71 | +[Sub-1] ArtifactEvent — Progress update 4/20 |
| 72 | +[Sub-2] ArtifactEvent — Progress update 4/20 |
| 73 | +[Sub-1] ArtifactEvent — Progress update 5/20 |
| 74 | +[Sub-2] ArtifactEvent — Progress update 5/20 |
| 75 | +... |
| 76 | +[Sub-3] Subscribing to task <task-id> (will trigger stream close)... |
| 77 | +[Sub-1] Stream closed. |
| 78 | +[Sub-2] Stream closed. |
| 79 | +[Sub-3] Stream closed. |
| 80 | +
|
| 81 | +=== All streams closed. === |
| 82 | +``` |
| 83 | + |
| 84 | +### Expected Output (Server) |
| 85 | + |
| 86 | +``` |
| 87 | +[HOOK] Subscriber added for task <id>. Active subscribers: 1 |
| 88 | +[AGENT] Starting execution for task <id> |
| 89 | +[AGENT] Sending: Progress update 1/20 |
| 90 | +[HOOK] Event distributed for task <id>: Message (subscribers: 1) |
| 91 | +... |
| 92 | +[HOOK] Subscriber added for task <id>. Active subscribers: 2 |
| 93 | +... |
| 94 | +[HOOK] Subscriber added for task <id>. Active subscribers: 3 |
| 95 | +[HOOK] Subscriber count reached 3 for task <id> — closing all streams |
| 96 | +[HOOK] Subscriber removed for task <id>. Active subscribers: 2 |
| 97 | +[HOOK] Subscriber removed for task <id>. Active subscribers: 1 |
| 98 | +[HOOK] Subscriber removed for task <id>. Active subscribers: 0 |
| 99 | +``` |
| 100 | + |
| 101 | +## Transport Protocol Selection |
| 102 | + |
| 103 | +Set `quarkus.agentcard.protocol` on both server and client (must match). Available values: |
| 104 | + |
| 105 | +| Value | Transport | |
| 106 | +|-------|-----------| |
| 107 | +| `JSONRPC` | JSON-RPC 2.0 (default) | |
| 108 | +| `GRPC` | gRPC | |
| 109 | +| `HTTP+JSON` | HTTP+JSON/REST | |
| 110 | + |
| 111 | +```bash |
| 112 | +# Server — gRPC example |
| 113 | +mvn quarkus:dev -Dquarkus.agentcard.protocol=GRPC |
| 114 | + |
| 115 | +# Client — must use the same value |
| 116 | +mvn exec:java -Dquarkus.agentcard.protocol=GRPC |
| 117 | +``` |
| 118 | + |
| 119 | +## Key Files |
| 120 | + |
| 121 | +| File | Description | |
| 122 | +|------|-------------| |
| 123 | +| `server/.../CloseStreamsHook.java` | `TaskStreamLifecycleHook` implementation — closes streams at 3 subscribers | |
| 124 | +| `server/.../AgentExecutorProducer.java` | Agent that sends 20 progress messages over 10 seconds | |
| 125 | +| `server/.../AgentCardProducer.java` | Agent card with streaming enabled | |
| 126 | +| `client/.../StreamLifecycleClient.java` | Client that creates 3 subscribers and logs events | |
| 127 | + |
| 128 | +## Integration Tests |
| 129 | + |
| 130 | +The server module includes `@QuarkusTest` integration tests that verify the hook behavior across all three transports: |
| 131 | + |
| 132 | +```bash |
| 133 | +cd examples/stream-lifecycle/server |
| 134 | +mvn test |
| 135 | +``` |
0 commit comments