Implementation:A2aproject A2a java EventQueue
| Knowledge Sources | |
|---|---|
| Domains | Server, Event_Streaming, Concurrency |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Concrete abstract base class for the A2A SDK's thread-safe, backpressure-managed event queue that streams task events from agent executors to multiple subscribers.
Description
`EventQueue` is the server-side event streaming infrastructure. It uses a hierarchical queue structure with two concrete implementations:
- `MainQueue` — the primary queue for a task. The `AgentEmitter` enqueues events here. Backpressure is enforced via a `Semaphore` that limits the queue to `DEFAULT_QUEUE_SIZE` (1000) events. When the queue is full, `enqueueEvent()` blocks the producer (the agent executor thread).
- `ChildQueue` — a per-subscriber fan-out queue. Each client subscription creates a `ChildQueue` that receives copies of all events from the parent `MainQueue`. When a terminal event (COMPLETED, FAILED, CANCELED) is dequeued, the `ChildQueue` closes itself.
The `MainEventBusProcessor` is a single-threaded processor that reads events from the `MainQueue`, persists them to the `TaskStore`, and fans them out to all active `ChildQueue` instances. This ensures consistent ordering and state transitions.
The `EventConsumer` bridges the blocking `ChildQueue` to a reactive stream via Mutiny Zero's `ZeroPublisher`, enabling SSE transport to clients. The reactive stream contract (request-n) provides a second layer of backpressure between the queue and the network.
The `TaskStreamLifecycleHook` can be attached to the `MainQueue` for lifecycle management (subscriber count monitoring, stream closure policies).
Usage
- Agent developers: Indirect usage via `AgentEmitter` which wraps the `EventQueue`
- Framework developers: Subclass `MainQueue`/`ChildQueue` for custom queue behavior; use `EventQueue.builder()` for configuration
- Stream lifecycle: Attach `TaskStreamLifecycleHook` via the builder for subscriber management
Code Reference
Source Location
- Repository: a2a-java
- File: `server-common/src/main/java/org/a2aproject/sdk/server/events/EventQueue.java`
- Lines: L36-1117 (abstract base + MainQueue + ChildQueue)
Signature
public abstract class EventQueue implements AutoCloseable {
public static final int DEFAULT_QUEUE_SIZE = 1000;
// Enqueue with backpressure (blocks if queue full)
void enqueueEvent(Event event);
void enqueueEvent(Event event, long timeout, TimeUnit unit);
// Dequeue (blocking with timeout)
Event dequeueEventItem(int timeout) throws InterruptedException;
// Queue state
boolean isClosed();
void close();
// Builder
static EventQueueBuilder builder(MainEventBus mainEventBus);
public static class EventQueueBuilder {
EventQueueBuilder queueSize(int queueSize);
EventQueueBuilder hook(EventEnqueueHook hook);
EventQueueBuilder taskId(String taskId);
EventQueueBuilder onCloseCallback(Runnable callback);
EventQueueBuilder taskStateProvider(TaskStateProvider provider);
EventQueueBuilder streamLifecycleHook(TaskStreamLifecycleHook hook);
EventQueue build();
}
}
// MainQueue: producer side, manages ChildQueue fan-out
// ChildQueue: subscriber side, receives fan-out copies
Import
import org.a2aproject.sdk.server.events.EventQueue;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| event | Event | Yes (enqueue) | TaskStatusUpdateEvent, TaskArtifactUpdateEvent, Message, Task, A2AError |
| queueSize | int | No (builder) | Max events in queue (default: 1000) |
| hook | EventEnqueueHook | No (builder) | Callback on enqueue (for replication) |
| streamLifecycleHook | TaskStreamLifecycleHook | No (builder) | Subscriber lifecycle management |
Outputs
| Name | Type | Description |
|---|---|---|
| dequeueEventItem() returns | Event | Next event from queue (blocking with timeout) |
| Backpressure | Semaphore blocks | Producer blocked when queue full |
| Fan-out | ChildQueue copies | Each subscriber gets its own copy of events |
| Terminal close | Auto-close | Terminal events (COMPLETED/FAILED/CANCELED) close ChildQueues |
Usage Examples
Indirect Usage via AgentEmitter
// AgentEmitter wraps EventQueue — agents don't interact directly
public void execute(RequestContext context, AgentEmitter emitter) throws A2AError {
emitter.startWork();
emitter.addArtifact(List.of(new TextPart("result")));
emitter.complete(); // Terminal event closes MainQueue and all ChildQueues
}
Builder Configuration (Framework)
EventQueue queue = EventQueue.builder(mainEventBus)
.queueSize(500) // Limit to 500 events
.taskId("task-123")
.streamLifecycleHook(myHook) // Attach lifecycle management
.onCloseCallback(() -> cleanup()) // Cleanup on close
.build();
Related Pages
Implements Principle
- Principle:A2aproject_A2a_java_Event_Queue_Backpressure_Streaming
- Principle:A2aproject_A2a_java_Stream_Lifecycle_Management
- Principle:A2aproject_A2a_java_Task_State_Machine