Google API Extensions for Java providing gRPC-specific functionality for Google Cloud client libraries
—
Comprehensive gRPC channel lifecycle management with connection pooling, load balancing, and Google Cloud optimizations including DirectPath support.
Primary TransportChannelProvider that creates and manages gRPC ManagedChannels with sophisticated configuration options.
/**
* Primary channel provider for creating gRPC ManagedChannels
* Supports connection pooling, DirectPath, and comprehensive configuration
*/
public final class InstantiatingGrpcChannelProvider implements TransportChannelProvider {
/** Create a new builder for channel provider configuration */
public static Builder newBuilder();
/** Get the transport name identifier */
public String getTransportName();
/** Create and return the configured transport channel */
public TransportChannel getTransportChannel() throws IOException;
/** Get the configured endpoint */
public String getEndpoint();
/** Get keep-alive time duration setting */
public Duration getKeepAliveTimeDuration();
/** Get keep-alive timeout duration setting */
public Duration getKeepAliveTimeoutDuration();
/** Get keep-alive without calls setting */
public Boolean getKeepAliveWithoutCalls();
/** Get maximum inbound metadata size setting */
public Integer getMaxInboundMetadataSize();
/** Get channel pool configuration settings */
public ChannelPoolSettings getChannelPoolSettings();
/** Create a new builder from this provider */
public Builder toBuilder();
}Comprehensive builder for configuring gRPC channel providers with all available options.
/**
* Builder for InstantiatingGrpcChannelProvider with comprehensive configuration
*/
public static final class InstantiatingGrpcChannelProvider.Builder {
/** Set the service endpoint URL */
public Builder setEndpoint(String endpoint);
/** Set credentials for authentication */
public Builder setCredentials(Credentials credentials);
/** Set the header provider for request headers */
public Builder setHeaderProvider(HeaderProvider headerProvider);
/** Set channel configurator for advanced ManagedChannelBuilder customization */
public Builder setChannelConfigurator(ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> channelConfigurator);
/** Set keep-alive time for connections */
public Builder setKeepAliveTime(Duration keepAliveTime);
/** Set keep-alive timeout for connections */
public Builder setKeepAliveTimeout(Duration keepAliveTimeout);
/** Set whether to send keep-alive pings without active calls */
public Builder setKeepAliveWithoutCalls(Boolean keepAliveWithoutCalls);
/** Set maximum inbound message size */
public Builder setMaxInboundMessageSize(Integer maxInboundMessageSize);
/** Set maximum inbound metadata size */
public Builder setMaxInboundMetadataSize(Integer maxInboundMetadataSize);
/** Set channel pool settings for connection pooling */
public Builder setChannelPoolSettings(ChannelPoolSettings channelPoolSettings);
/** Set DirectPath service configuration */
public Builder setDirectPathServiceConfig(Map<String, ?> serviceConfig);
/** Set quota project ID for billing */
public Builder setQuotaProjectId(String quotaProjectId);
/** Set custom client interceptor provider */
public Builder setInterceptorProvider(GrpcInterceptorProvider interceptorProvider);
/** Build the configured channel provider */
public InstantiatingGrpcChannelProvider build();
}Configuration for sophisticated channel pool management with dynamic scaling capabilities.
/**
* Settings for channel pool behavior and scaling
* Supports dynamic scaling based on RPC load
*/
public abstract class ChannelPoolSettings {
/** Create a statically sized channel pool */
public static ChannelPoolSettings staticallySized(int size);
/** Create a new builder for channel pool settings */
public static Builder builder();
/** Get minimum RPCs per channel threshold */
public int getMinRpcsPerChannel();
/** Get maximum RPCs per channel threshold */
public int getMaxRpcsPerChannel();
/** Get minimum number of channels in pool */
public int getMinChannelCount();
/** Get maximum number of channels in pool */
public int getMaxChannelCount();
/** Get initial number of channels to create */
public int getInitialChannelCount();
/** Check if preemptive refresh is enabled */
public boolean isPreemptiveRefreshEnabled();
/** Create a builder from these settings */
public Builder toBuilder();
}Builder for configuring channel pool behavior with fine-grained control over scaling parameters.
/**
* Builder for ChannelPoolSettings with scaling configuration
*/
public abstract static class ChannelPoolSettings.Builder {
/** Set minimum RPCs per channel before scaling up */
public Builder setMinRpcsPerChannel(int minRpcsPerChannel);
/** Set maximum RPCs per channel before scaling up */
public Builder setMaxRpcsPerChannel(int maxRpcsPerChannel);
/** Set minimum number of channels to maintain */
public Builder setMinChannelCount(int minChannelCount);
/** Set maximum number of channels allowed */
public Builder setMaxChannelCount(int maxChannelCount);
/** Set initial number of channels to create */
public Builder setInitialChannelCount(int initialChannelCount);
/** Enable or disable preemptive refresh of channels */
public Builder setPreemptiveRefreshEnabled(boolean preemptiveRefreshEnabled);
/** Build the channel pool settings */
public ChannelPoolSettings build();
}Factory interface for creating custom ManagedChannels with specific configurations.
/**
* Factory interface for custom channel creation
*/
public interface ChannelFactory {
/** Create a ManagedChannel with the given configuration */
ManagedChannel createChannel(String endpoint, Map<String, ?> channelArgs);
}Interface for preparing channels before use, typically for pre-loading or warming up connections.
/**
* Interface for preparing ManagedChannels for requests
* Used for connection warming and pre-loading
*/
public interface ChannelPrimer {
/** Prepare the given channel for use */
void primeChannel(ManagedChannel managedChannel);
}import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.auth.oauth2.GoogleCredentials;
// Create basic channel provider
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setEndpoint("translate.googleapis.com:443")
.setCredentials(GoogleCredentials.getApplicationDefault())
.build();
// Get the transport channel
TransportChannel channel = provider.getTransportChannel();import com.google.api.gax.grpc.ChannelPoolSettings;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
// Configure sophisticated channel pooling
ChannelPoolSettings poolSettings = ChannelPoolSettings.builder()
.setMinChannelCount(2)
.setMaxChannelCount(10)
.setMinRpcsPerChannel(5)
.setMaxRpcsPerChannel(100)
.setPreemptiveRefreshEnabled(true)
.build();
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setEndpoint("your-service.googleapis.com:443")
.setChannelPoolSettings(poolSettings)
.setKeepAliveTime(Duration.ofMinutes(2))
.setKeepAliveTimeout(Duration.ofSeconds(30))
.build();import io.grpc.ManagedChannelBuilder;
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
// Advanced channel configuration with custom configurator
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setEndpoint("your-service.googleapis.com:443")
.setChannelConfigurator(channelBuilder -> {
return ((NettyChannelBuilder) channelBuilder)
.maxInboundMessageSize(4 * 1024 * 1024) // 4MB
.usePlaintext(); // For testing only
})
.build();import java.util.Map;
import java.util.HashMap;
// Configure DirectPath for optimized Google Cloud connections
Map<String, Object> directPathConfig = new HashMap<>();
directPathConfig.put("loadBalancingConfig",
List.of(Map.of("grpclb", Map.of("serviceName", "your-service"))));
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setEndpoint("your-service.googleapis.com:443")
.setDirectPathServiceConfig(directPathConfig)
.build();Install with Tessl CLI
npx tessl i tessl/maven-com-google-api--gax-grpc