Overview
Concrete event aggregator that collects streaming events from EventConsumer into a final task result (Message or Task), supporting both blocking and non-blocking consumption modes with task store reconciliation.
Description
The ResultAggregator class bridges the EventConsumer reactive stream to synchronous result collection. It provides two modes: consumeAndEmit streams all events without persistence (used for fire-and-forget streaming), and consumeAndBreakOnInterrupt consumes events until a terminal task status is reached (blocking mode), capturing the final Message or Task. When a blocking operation completes, it reconciles with the TaskStore by polling until the persisted task state matches the streamed state, with a configurable timeout. Uses AsyncUtils reactive stream operators (Mutiny Zero) for backpressure-managed processing.
Usage
- Blocking request-response: Used by DefaultRequestHandler when `blocking=true` to collect events until task completion
- Streaming response: Used when `blocking=false` to stream all events to the client without waiting for completion
- Task store reconciliation: After blocking completion, polls TaskStore to ensure persisted state is consistent
Code Reference
Source Location
- Repository: a2a-java
- File: `server-common/src/main/java/org/a2aproject/sdk/server/tasks/ResultAggregator.java`
- Lines: L30-303
Signature
public class ResultAggregator {
public ResultAggregator(TaskManager taskManager, @Nullable Message message,
Executor executor, Executor eventConsumerExecutor,
long reconciliationTimeoutNanos);
public @Nullable EventKind getCurrentResult();
public Flow.Publisher<EventQueueItem> consumeAndEmit(EventConsumer consumer);
public EventTypeAndInterrupt consumeAndBreakOnInterrupt(
EventConsumer consumer, boolean blocking) throws A2AError;
// Internal: reconcileWithTaskStore, EventTypeAndInterrupt record
}
Import
import org.a2aproject.sdk.server.tasks.ResultAggregator;
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| taskManager |
TaskManager |
Yes |
Manages task state
|
| message |
Message |
No |
Initial message if already known
|
| executor |
Executor |
Yes |
Thread pool for event processing
|
| eventConsumerExecutor |
Executor |
Yes |
Thread pool for event consumption
|
| reconciliationTimeoutNanos |
long |
Yes |
Timeout for task store reconciliation
|
| consumer |
EventConsumer |
Yes |
Event queue consumer
|
| blocking |
boolean |
Yes |
Whether to block until completion
|
Outputs
| Name |
Type |
Description
|
| getCurrentResult() |
EventKind (nullable) |
Message or current Task
|
| consumeAndEmit() |
Flow.Publisher<EventQueueItem> |
Stream of all events
|
| consumeAndBreakOnInterrupt() |
EventTypeAndInterrupt |
Final event type and interrupt flag
|
Usage Examples
Blocking Aggregation
ResultAggregator aggregator = new ResultAggregator(
taskManager, null, executor, eventConsumerExecutor,
TimeUnit.SECONDS.toNanos(30));
// Block until task completes or interrupts
EventTypeAndInterrupt result = aggregator.consumeAndBreakOnInterrupt(
eventConsumer, true);
EventKind finalResult = aggregator.getCurrentResult();
if (finalResult instanceof Task task) {
System.out.println("Task state: " + task.status().state());
}
Non-Blocking Streaming
// Stream all events without blocking
Flow.Publisher<EventQueueItem> stream = aggregator.consumeAndEmit(eventConsumer);
// Subscriber receives events as they arrive
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.