Class A2AHttpClientFactory
A2AHttpClient instances using the ServiceLoader mechanism.
This factory discovers available A2AHttpClientProvider implementations at runtime,
tries them in descending priority order, and returns the first one that succeeds.
If no provider can be instantiated, it throws an IllegalStateException.
Usage
// Get the default client (highest available priority provider)
A2AHttpClient client = A2AHttpClientFactory.create();
// Use with try-with-resources if the client implements AutoCloseable
try (A2AHttpClient client = A2AHttpClientFactory.create()) {
A2AHttpResponse response = client.createGet()
.url("https://example.com")
.get();
}
Priority System
Providers are tried in descending priority order. If a provider's create() throws,
the factory logs the failure at WARNING level and falls back to the next provider:
- CdiA2AHttpClient: priority 200 (CDI-provided bean, when a container and bean are active)
- AndroidA2AHttpClient: priority 110 (Android runtime only)
- VertxA2AHttpClient: priority 100 (when
vertx-web-clientis on the classpath) - JdkA2AHttpClient: priority 0 (always available, last resort)
Custom Providers
To add a custom provider, implement A2AHttpClientProvider and register it
in META-INF/services/org.a2aproject.sdk.client.http.A2AHttpClientProvider.
-
Method Summary
Modifier and TypeMethodDescriptionstatic A2AHttpClientcreate()Creates a new A2AHttpClient instance using the highest available priority provider.static A2AHttpClientCreates a new A2AHttpClient instance using a specific provider by name.
-
Method Details
-
create
Creates a new A2AHttpClient instance using the highest available priority provider.Providers are tried in descending priority order. If a provider's
create()throws, it is skipped and the next provider is tried. Failures are logged atWARNINGlevel.- Returns:
- a new A2AHttpClient instance
- Throws:
IllegalStateException- if no provider found or all providers failed to instantiate
-
create
Creates a new A2AHttpClient instance using a specific provider by name.This method is useful for testing or when you need to force a specific implementation.
- Parameters:
providerName- the name of the provider to use- Returns:
- a new A2AHttpClient instance from the specified provider
- Throws:
IllegalArgumentException- if no provider with the given name is found
-