Class VertxA2AHttpClient
- All Implemented Interfaces:
AutoCloseable,A2AHttpClient
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
}
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.a2aproject.sdk.client.http.A2AHttpClient
A2AHttpClient.Builder<T extends A2AHttpClient.Builder<T>>, A2AHttpClient.DeleteBuilder, A2AHttpClient.GetBuilder, A2AHttpClient.PostBuilder -
Field Summary
Fields inherited from interface org.a2aproject.sdk.client.http.A2AHttpClient
ACCEPT, APPLICATION_JSON, CONTENT_TYPE, EVENT_STREAM -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new VertxA2AHttpClient with an internally managed Vert.x instance.VertxA2AHttpClient(io.vertx.core.Vertx vertx) Creates a new VertxA2AHttpClient using an externally managed Vert.x instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this HTTP client and releases associated resources.Creates a builder for DELETE requests.Creates a builder for GET requests.Creates a builder for POST requests.
-
Constructor Details
-
VertxA2AHttpClient
public VertxA2AHttpClient()Creates a new VertxA2AHttpClient with an internally managed Vert.x instance.The client creates a new
Vertxinstance andWebClientconfigured with HTTP keep-alive and automatic redirect following. Whenclose()is called, both the WebClient and Vertx instance are closed.Important: Always call
close()when done with this client to prevent resource leaks. -
VertxA2AHttpClient
public VertxA2AHttpClient(io.vertx.core.Vertx vertx) Creates a new VertxA2AHttpClient using an externally managed Vert.x instance.The client creates a
WebClientusing the providedVertxinstance. Whenclose()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:
closein interfaceAutoCloseable
-
createGet
Description copied from interface:A2AHttpClientCreates a builder for GET requests.- Specified by:
createGetin interfaceA2AHttpClient- Returns:
- a new GetBuilder instance
-
createPost
Description copied from interface:A2AHttpClientCreates a builder for POST requests.- Specified by:
createPostin interfaceA2AHttpClient- Returns:
- a new PostBuilder instance
-
createDelete
Description copied from interface:A2AHttpClientCreates a builder for DELETE requests.- Specified by:
createDeletein interfaceA2AHttpClient- Returns:
- a new DeleteBuilder instance
-