Class VertxA2AHttpClient

java.lang.Object
org.a2aproject.sdk.client.http.VertxA2AHttpClient
All Implemented Interfaces:
AutoCloseable, A2AHttpClient

public class VertxA2AHttpClient extends Object implements A2AHttpClient, AutoCloseable
Vert.x WebClient-based implementation of A2AHttpClient.

This implementation uses Vert.x's reactive HTTP client to execute requests. For synchronous methods (A2AHttpClient.GetBuilder.get(), A2AHttpClient.PostBuilder.post(), A2AHttpClient.DeleteBuilder.delete()), the implementation blocks the calling thread until the asynchronous operation completes. For SSE streaming methods, the implementation returns immediately with a CompletableFuture and streams events asynchronously via callbacks.

Lifecycle Management

This client implements AutoCloseable and should be closed when no longer needed:


 try (VertxA2AHttpClient client = new VertxA2AHttpClient()) {
     A2AHttpResponse response = client.createGet()
         .url("https://example.com/api")
         .get();
     // Use response
 }
 

If constructed with the no-args constructor, the client creates and owns a Vertx instance which will be closed when close() is called. If constructed with an external Vertx instance, only the WebClient is closed, leaving the Vertx instance management to the caller.

Thread Safety

This client is thread-safe. Multiple threads can create and execute requests concurrently. However, individual builder instances are NOT thread-safe and should not be shared across threads.

HTTP/2 Support

Vert.x WebClient automatically negotiates HTTP/2 when supported by the server via ALPN. No explicit configuration is required.

Usage Examples

Simple GET Request


 try (VertxA2AHttpClient client = new VertxA2AHttpClient()) {
     A2AHttpResponse response = client.createGet()
         .url("https://api.example.com/data")
         .addHeader("Authorization", "Bearer token")
         .get();

     if (response.success()) {
         System.out.println(response.body());
     }
 }
 

POST Request with JSON Body


 try (VertxA2AHttpClient client = new VertxA2AHttpClient()) {
     A2AHttpResponse response = client.createPost()
         .url("https://api.example.com/submit")
         .addHeader("Content-Type", "application/json")
         .body("{\"key\":\"value\"}")
         .post();

     System.out.println("Status: " + response.status());
 }
 

Async SSE Streaming


 try (VertxA2AHttpClient client = new VertxA2AHttpClient()) {
     CompletableFuture<Void> future = client.createGet()
         .url("https://api.example.com/stream")
         .getAsyncSSE(
             event -> System.out.println("Received: " + event.data()),
             error -> error.printStackTrace(),
             () -> System.out.println("Stream complete")
         );

     // Do other work while streaming...
     future.join(); // Wait for completion if needed
 }
 
  • Constructor Details

    • VertxA2AHttpClient

      public VertxA2AHttpClient()
      Creates a new VertxA2AHttpClient with an internally managed Vert.x instance.

      The client creates a new Vertx instance and WebClient configured with HTTP keep-alive and automatic redirect following. When close() is called, both the WebClient and Vertx instance are closed.

      Important: Always call close() when done with this client to prevent resource leaks.

      See Also:
    • VertxA2AHttpClient

      public VertxA2AHttpClient(io.vertx.core.Vertx vertx)
      Creates a new VertxA2AHttpClient using an externally managed Vert.x instance.

      The client creates a WebClient using the provided Vertx instance. When close() is called, only the WebClient is closed; the Vertx instance remains open and must be managed by the caller.

      This constructor is useful in environments where Vert.x is already managed, such as Quarkus applications.

      Parameters:
      vertx - the Vert.x instance to use; must not be null
      Throws:
      IllegalArgumentException - if vertx is null
  • Method Details

    • close

      public void close()
      Closes this HTTP client and releases associated resources.

      This method always closes the WebClient. If the client was created with the no-args constructor (and thus owns the Vert.x instance), the Vertx instance is also closed. Otherwise, the Vertx instance is left open for the caller to manage.

      Specified by:
      close in interface AutoCloseable
    • createGet

      public A2AHttpClient.GetBuilder createGet()
      Description copied from interface: A2AHttpClient
      Creates a builder for GET requests.
      Specified by:
      createGet in interface A2AHttpClient
      Returns:
      a new GetBuilder instance
    • createPost

      public A2AHttpClient.PostBuilder createPost()
      Description copied from interface: A2AHttpClient
      Creates a builder for POST requests.
      Specified by:
      createPost in interface A2AHttpClient
      Returns:
      a new PostBuilder instance
    • createDelete

      public A2AHttpClient.DeleteBuilder createDelete()
      Description copied from interface: A2AHttpClient
      Creates a builder for DELETE requests.
      Specified by:
      createDelete in interface A2AHttpClient
      Returns:
      a new DeleteBuilder instance