Class GrpcTransportConfigBuilder


public class GrpcTransportConfigBuilder extends ClientTransportConfigBuilder<GrpcTransportConfig, GrpcTransportConfigBuilder>
Builder for creating GrpcTransportConfig instances.

This builder provides a fluent API for configuring the gRPC transport protocol. Unlike other transports, gRPC requires a channel factory to be explicitly provided - the channelFactory(Function) method must be called before build().

The channel factory gives you complete control over gRPC channel configuration:

  • Connection management: Connection pooling, keep-alive settings
  • Security: TLS configuration, client certificates
  • Performance: Message size limits, compression, load balancing
  • Timeouts: Deadline configuration, idle timeout
  • Interceptors: Request/response transformation, authentication

Basic development setup (insecure):

// Simple channel for local development
Function<String, Channel> channelFactory = url -> {
    // Extract "localhost:9999" from "http://localhost:9999"
    String target = url.replaceAll("^https?://", "");
    return ManagedChannelBuilder.forTarget(target)
        .usePlaintext()  // No TLS
        .build();
};

GrpcTransportConfig config = new GrpcTransportConfigBuilder()
    .channelFactory(channelFactory)
    .build();

Client client = Client.builder(agentCard)
    .withTransport(GrpcTransport.class, config)
    .build();

Production setup with TLS and connection pooling:

Function<String, Channel> channelFactory = url -> {
    String target = extractTarget(url);
    return ManagedChannelBuilder.forTarget(target)
        .useTransportSecurity()  // Enable TLS
        .keepAliveTime(30, TimeUnit.SECONDS)
        .keepAliveTimeout(10, TimeUnit.SECONDS)
        .idleTimeout(5, TimeUnit.MINUTES)
        .maxInboundMessageSize(10 * 1024 * 1024)  // 10MB messages
        .build();
};

GrpcTransportConfig config = new GrpcTransportConfigBuilder()
    .channelFactory(channelFactory)
    .build();

With custom SSL certificates:

SslContext sslContext = GrpcSslContexts.forClient()
    .trustManager(new File("ca.crt"))
    .keyManager(new File("client.crt"), new File("client.key"))
    .build();

Function<String, Channel> channelFactory = url -> {
    String target = extractTarget(url);
    return NettyChannelBuilder.forTarget(target)
        .sslContext(sslContext)
        .build();
};

GrpcTransportConfig config = new GrpcTransportConfigBuilder()
    .channelFactory(channelFactory)
    .build();

With load balancing and health checks:

Function<String, Channel> channelFactory = url -> {
    String target = extractTarget(url);
    return ManagedChannelBuilder.forTarget(target)
        .defaultLoadBalancingPolicy("round_robin")
        .enableRetry()
        .maxRetryAttempts(3)
        .build();
};

GrpcTransportConfig config = new GrpcTransportConfigBuilder()
    .channelFactory(channelFactory)
    .build();

With A2A interceptors:

GrpcTransportConfig config = new GrpcTransportConfigBuilder()
    .channelFactory(channelFactory)
    .addInterceptor(new LoggingInterceptor())
    .addInterceptor(new MetricsInterceptor())
    .addInterceptor(new AuthenticationInterceptor(apiKey))
    .build();

Direct usage in ClientBuilder:

// Channel factory inline
Client client = Client.builder(agentCard)
    .withTransport(GrpcTransport.class, new GrpcTransportConfigBuilder()
        .channelFactory(url -> ManagedChannelBuilder
            .forTarget(extractTarget(url))
            .usePlaintext()
            .build())
        .addInterceptor(loggingInterceptor))
    .build();

Channel Lifecycle Management:

// Store channels for cleanup
Map<String, ManagedChannel> channels = new ConcurrentHashMap<>();

Function<String, Channel> channelFactory = url -> {
    return channels.computeIfAbsent(url, u -> {
        String target = extractTarget(u);
        return ManagedChannelBuilder.forTarget(target)
            .usePlaintext()
            .build();
    });
};

// Cleanup when done
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    channels.values().forEach(ManagedChannel::shutdown);
}));
See Also:
  • Constructor Details

    • GrpcTransportConfigBuilder

      public GrpcTransportConfigBuilder()
  • Method Details

    • channelFactory

      public GrpcTransportConfigBuilder channelFactory(Function<String, io.grpc.Channel> channelFactory)
      Set the channel factory for creating gRPC channels.

      This method is required - build() will throw IllegalStateException if the channel factory is not set.

      The factory function receives the agent's URL (e.g., "http://localhost:9999") and must return a configured Channel. You are responsible for:

      • Extracting the target address from the URL
      • Configuring TLS and security settings
      • Setting connection pool and timeout parameters
      • Managing channel lifecycle and shutdown

      Example:

      Function<String, Channel> factory = url -> {
          String target = url.replaceAll("^https?://", "");
          return ManagedChannelBuilder.forTarget(target)
              .usePlaintext()
              .build();
      };
      
      builder.channelFactory(factory);
      
      Parameters:
      channelFactory - function to create gRPC channels from agent URLs (must not be null)
      Returns:
      this builder for method chaining
      Throws:
      IllegalArgumentException - if channelFactory is null
    • build

      public GrpcTransportConfig build()
      Build the gRPC transport configuration.

      The channel factory must have been set via channelFactory(Function) before calling this method. Any configured interceptors are transferred to the configuration.

      Specified by:
      build in class ClientTransportConfigBuilder<GrpcTransportConfig, GrpcTransportConfigBuilder>
      Returns:
      the configured gRPC transport configuration
      Throws:
      IllegalStateException - if the channel factory was not set