Class A2AHttpClientFactory

java.lang.Object
org.a2aproject.sdk.client.http.A2AHttpClientFactory

public final class A2AHttpClientFactory extends Object
Factory for creating 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-client is 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 Details

    • create

      public static A2AHttpClient 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 at WARNING level.

      Returns:
      a new A2AHttpClient instance
      Throws:
      IllegalStateException - if no provider found or all providers failed to instantiate
    • createWithSseConfig

      public static A2AHttpClient createWithSseConfig(SSEParserConfig sseParserConfig)
      Creates a new A2AHttpClient instance with the given SSEParserConfig using the highest available priority provider that honours it.

      Only providers that return true from A2AHttpClientProvider.supportsSseConfig() are considered. This prevents a higher-priority provider (e.g. Vert.x at priority 100 or CDI at priority 200) from silently ignoring the supplied configuration. If no SSE-config-aware provider is available the method falls back to any available provider and logs a warning.

      Parameters:
      sseParserConfig - the SSE parser configuration to apply
      Returns:
      a new A2AHttpClient instance
      Throws:
      IllegalStateException - if no provider found or all providers failed to instantiate
    • create

      public static A2AHttpClient create(String providerName)
      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