Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:A2aproject A2a java AgentExecutor

From Leeroopedia
Knowledge Sources
Domains Server, Agent_Execution
Last Updated 2026-10-29 00:00 GMT

Overview

Concrete functional interface for implementing A2A agent business logic, executed asynchronously by the server framework when clients send messages.

Description

`AgentExecutor` is a `@FunctionalInterface` that serves as the primary extension point for agent developers. It defines two methods: `execute()` (required) and `cancel()` (optional, with a default implementation that throws `TaskNotCancelableError`).

The `execute()` method is called by `DefaultRequestHandler` in a background thread pool when a client message arrives. The implementation receives a `RequestContext` (containing the user message, task state, and configuration) and an `AgentEmitter` (for sending messages, streaming artifacts, and managing task state transitions).

Key constraints:

  • Don't throw exceptions for business logic errors — use `emitter.fail()` instead
  • Only throw `A2AError` for catastrophic failures
  • Don't block waiting for events to be consumed — enqueue and return
  • Be thread-safe if maintaining state across invocations
  • The `cancel()` method may run concurrently with `execute()` on another thread

The executor is provided to the framework via a CDI `@Produces` method.

Usage

  • Simple message response: Implement `execute()` to send a direct message via `emitter.sendMessage()` — no task lifecycle needed.
  • Task with artifacts: Use `emitter.submit()`, `emitter.startWork()`, `emitter.addArtifact()`, `emitter.complete()` for full task lifecycle.
  • Streaming LLM output: Call `emitter.addArtifact()` in a loop for each token/chunk, then `emitter.complete()`.
  • Cancellable agents: Override `cancel()` to interrupt ongoing work and call `emitter.cancel()`.

Code Reference

Source Location

  • Repository: a2a-java
  • File: `server-common/src/main/java/org/a2aproject/sdk/server/agentexecution/AgentExecutor.java`
  • Lines: L100-154

Signature

@FunctionalInterface
public interface AgentExecutor {
    void execute(RequestContext context, AgentEmitter emitter) throws A2AError;

    default void cancel(RequestContext context, AgentEmitter emitter) throws A2AError {
        throw new TaskNotCancelableError("Agent execution is not cancelable.");
    }
}

Import

import org.a2aproject.sdk.server.agentexecution.AgentExecutor;
import org.a2aproject.sdk.server.agentexecution.RequestContext;
import org.a2aproject.sdk.server.tasks.AgentEmitter;
import org.a2aproject.sdk.spec.A2AError;

I/O Contract

Inputs

Name Type Required Description
context RequestContext Yes Contains user message, task state, configuration
emitter AgentEmitter Yes Event emission helper for sending results to client

Outputs

Name Type Description
Events via AgentEmitter Messages, artifacts, status transitions enqueued to EventQueue
Exceptions A2AError Catastrophic failures only; business errors via emitter.fail()

Usage Examples

Simple Hello World Executor

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import org.a2aproject.sdk.server.agentexecution.AgentExecutor;
import org.a2aproject.sdk.server.agentexecution.RequestContext;
import org.a2aproject.sdk.server.tasks.AgentEmitter;
import org.a2aproject.sdk.spec.A2AError;

@ApplicationScoped
public class AgentExecutorProducer {
    @Produces
    public AgentExecutor agentExecutor() {
        return new AgentExecutor() {
            @Override
            public void execute(RequestContext context, AgentEmitter emitter) throws A2AError {
                emitter.sendMessage("Hello World");
            }

            @Override
            public void cancel(RequestContext context, AgentEmitter emitter) throws A2AError {
                throw new UnsupportedOperationError();
            }
        };
    }
}

Task Lifecycle with Artifacts

public void execute(RequestContext context, AgentEmitter emitter) throws A2AError {
    // Initialize task if new
    if (context.getTask() == null) {
        emitter.submit();
    }
    emitter.startWork();

    // Extract user input
    String userMessage = context.getUserInput("\n");

    // Process and return result
    String result = processRequest(userMessage);
    emitter.addArtifact(List.of(new TextPart(result)));
    emitter.complete();
}

Streaming LLM Output

public void execute(RequestContext context, AgentEmitter emitter) throws A2AError {
    emitter.startWork();

    // Stream tokens as they arrive from the LLM
    for (String chunk : llmService.stream(context.getUserInput("\n"))) {
        emitter.addArtifact(List.of(new TextPart(chunk)));
    }

    emitter.complete();  // Final event closes the queue
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment