Google API Extensions for Java providing gRPC-specific functionality for Google Cloud client libraries
—
gRPC transport channel implementation and status code handling for the GAX transport abstraction layer.
gRPC-based implementation of the GAX TransportChannel interface providing channel access and DirectPath support.
/**
* gRPC-based implementation of TransportChannel
* Provides access to underlying gRPC channels and DirectPath optimization
*/
public abstract class GrpcTransportChannel implements TransportChannel {
/** Get the gRPC transport name identifier */
public static String getGrpcTransportName();
/** Create a new builder for transport channel */
public static Builder newBuilder();
/** Create transport channel from managed channel */
public static GrpcTransportChannel create(ManagedChannel channel);
/** Get the underlying gRPC channel */
public Channel getChannel();
/** Check if using DirectPath optimization */
public boolean isDirectPath();
/** Shutdown the transport channel */
public void shutdown();
/** Check if transport is shutdown */
public boolean isShutdown();
/** Check if transport is terminated */
public boolean isTerminated();
/** Shutdown transport immediately */
public void shutdownNow();
/** Await termination with timeout */
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException;
/** Close the transport channel */
public void close();
}Builder for creating and configuring GrpcTransportChannel instances.
/**
* Builder for GrpcTransportChannel configuration
*/
public abstract static class GrpcTransportChannel.Builder {
/** Set the managed channel */
public Builder setManagedChannel(ManagedChannel managedChannel);
/** Set channel pooling settings */
public Builder setChannelPoolSettings(ChannelPoolSettings channelPoolSettings);
/** Set keep-alive time */
public Builder setKeepAliveTime(Duration keepAliveTime);
/** Set keep-alive timeout */
public Builder setKeepAliveTimeout(Duration keepAliveTimeout);
/** Set keep-alive without calls */
public Builder setKeepAliveWithoutCalls(Boolean keepAliveWithoutCalls);
/** Set interceptor provider */
public Builder setInterceptorProvider(GrpcInterceptorProvider interceptorProvider);
/** Build the transport channel */
public GrpcTransportChannel build();
}gRPC-specific implementation of GAX StatusCode interface for error handling and status mapping.
/**
* gRPC-specific status code implementation
* Maps gRPC status codes to GAX StatusCode interface
*/
public abstract class GrpcStatusCode implements StatusCode {
/** Create status code from gRPC status code */
public static GrpcStatusCode of(Status.Code grpcCode);
/** Get the GAX status code */
public StatusCode.Code getCode();
/** Get the gRPC status code */
public Status.Code getGrpcCode();
/** Check if status represents a timeout */
public boolean isTimeout();
/** Check if status is retryable */
public boolean isRetryable();
/** Get string representation */
public String toString();
}Descriptor providing metadata about the gRPC transport implementation.
/**
* Descriptor for gRPC transport providing metadata and capabilities
*/
public class GrpcTransportDescriptor implements TransportDescriptor {
/** Get the transport name */
public String getTransportName();
/** Get supported authentication types */
public Set<String> getSupportedAuthTypes();
/** Check if transport supports streaming */
public boolean supportsStreaming();
/** Check if transport supports operation cancellation */
public boolean supportsCancellation();
/** Get maximum message size supported */
public long getMaxMessageSize();
/** Check if DirectPath is supported */
public boolean supportsDirectPath();
}/**
* Standard gRPC to GAX status code mappings
*/
public class GrpcStatusCodeMapping {
/** Map from gRPC Status.Code to GAX StatusCode.Code */
public static final Map<Status.Code, StatusCode.Code> GRPC_TO_GAX_MAPPING;
/** Map from GAX StatusCode.Code to gRPC Status.Code */
public static final Map<StatusCode.Code, Status.Code> GAX_TO_GRPC_MAPPING;
/** Convert gRPC status to GAX status */
public static StatusCode.Code toGaxCode(Status.Code grpcCode);
/** Convert GAX status to gRPC status */
public static Status.Code toGrpcCode(StatusCode.Code gaxCode);
/** Check if status code is retryable */
public static boolean isRetryable(Status.Code grpcCode);
/** Check if status code represents timeout */
public static boolean isTimeout(Status.Code grpcCode);
}/**
* Maps gRPC exceptions to GAX exceptions
*/
public class GrpcExceptionMapping {
/** Convert gRPC StatusException to ApiException */
public static ApiException toApiException(StatusException statusException);
/** Convert gRPC StatusRuntimeException to ApiException */
public static ApiException toApiException(StatusRuntimeException statusRuntimeException);
/** Extract retry information from exception */
public static RetryInfo extractRetryInfo(Throwable exception);
/** Check if exception is retryable */
public static boolean isRetryable(Throwable exception);
}/**
* Configuration options for gRPC transport
*/
public static class GrpcTransportOptions {
/** Set maximum inbound message size */
public GrpcTransportOptions withMaxInboundMessageSize(int size);
/** Set maximum inbound metadata size */
public GrpcTransportOptions withMaxInboundMetadataSize(int size);
/** Enable keep-alive */
public GrpcTransportOptions withKeepAlive(Duration time, Duration timeout);
/** Enable compression */
public GrpcTransportOptions withCompression(String compression);
/** Set user agent */
public GrpcTransportOptions withUserAgent(String userAgent);
/** Enable DirectPath */
public GrpcTransportOptions withDirectPath(boolean enabled);
}/**
* Utilities for monitoring gRPC channel state
*/
public class ChannelStateMonitor {
/** Get current channel connectivity state */
public static ConnectivityState getChannelState(ManagedChannel channel);
/** Wait for channel state change */
public static boolean waitForStateChange(
ManagedChannel channel,
ConnectivityState sourceState,
long timeout,
TimeUnit unit);
/** Check if channel is ready */
public static boolean isChannelReady(ManagedChannel channel);
/** Check if channel is shutdown */
public static boolean isChannelShutdown(ManagedChannel channel);
}import com.google.api.gax.grpc.GrpcTransportChannel;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
// Create managed channel
ManagedChannel managedChannel = ManagedChannelBuilder
.forAddress("your-service.googleapis.com", 443)
.useTransportSecurity()
.build();
// Create transport channel
GrpcTransportChannel transportChannel = GrpcTransportChannel.create(managedChannel);
// Use the transport channel
Channel channel = transportChannel.getChannel();
boolean isDirectPath = transportChannel.isDirectPath();import com.google.api.gax.grpc.ChannelPoolSettings;
// Create transport channel with builder
ChannelPoolSettings poolSettings = ChannelPoolSettings.builder()
.setMinChannelCount(2)
.setMaxChannelCount(10)
.build();
GrpcTransportChannel transportChannel = GrpcTransportChannel.newBuilder()
.setManagedChannel(managedChannel)
.setChannelPoolSettings(poolSettings)
.setKeepAliveTime(Duration.ofMinutes(2))
.setKeepAliveTimeout(Duration.ofSeconds(30))
.build();import com.google.api.gax.grpc.GrpcStatusCode;
import com.google.api.gax.rpc.StatusCode;
import io.grpc.Status;
// Create status code from gRPC status
GrpcStatusCode statusCode = GrpcStatusCode.of(Status.Code.UNAVAILABLE);
// Check status properties
StatusCode.Code gaxCode = statusCode.getCode();
boolean isRetryable = statusCode.isRetryable();
boolean isTimeout = statusCode.isTimeout();
// Use in exception handling
try {
// Make gRPC call
} catch (StatusRuntimeException e) {
GrpcStatusCode code = GrpcStatusCode.of(e.getStatus().getCode());
if (code.getCode() == StatusCode.Code.UNAVAILABLE) {
// Handle service unavailable
} else if (code.getCode() == StatusCode.Code.DEADLINE_EXCEEDED) {
// Handle timeout
}
}import com.google.api.gax.rpc.ApiException;
import io.grpc.StatusException;
import io.grpc.StatusRuntimeException;
// Handle gRPC exceptions
try {
// Make gRPC call
} catch (StatusRuntimeException e) {
// Convert to GAX exception
ApiException apiException = GrpcExceptionMapping.toApiException(e);
if (apiException.isRetryable()) {
// Retry the operation
} else {
// Handle non-retryable error
throw apiException;
}
} catch (StatusException e) {
ApiException apiException = GrpcExceptionMapping.toApiException(e);
// Handle exception
}import java.util.concurrent.TimeUnit;
// Proper shutdown sequence
try {
// Use transport channel
// ... perform operations ...
} finally {
// Graceful shutdown
transportChannel.shutdown();
// Wait for termination
if (!transportChannel.awaitTermination(30, TimeUnit.SECONDS)) {
// Force shutdown if graceful shutdown times out
transportChannel.shutdownNow();
// Wait again for force shutdown
if (!transportChannel.awaitTermination(5, TimeUnit.SECONDS)) {
System.err.println("Transport channel did not terminate gracefully");
}
}
}import io.grpc.ConnectivityState;
// Monitor channel state
ManagedChannel channel = transportChannel.getChannel();
ConnectivityState currentState = ChannelStateMonitor.getChannelState(channel);
switch (currentState) {
case READY:
System.out.println("Channel is ready for calls");
break;
case CONNECTING:
System.out.println("Channel is connecting");
break;
case TRANSIENT_FAILURE:
System.out.println("Channel experienced a failure, will retry");
break;
case IDLE:
System.out.println("Channel is idle");
break;
case SHUTDOWN:
System.out.println("Channel is shutdown");
break;
}
// Wait for channel to become ready
boolean stateChanged = ChannelStateMonitor.waitForStateChange(
channel, currentState, 30, TimeUnit.SECONDS);
if (stateChanged && ChannelStateMonitor.isChannelReady(channel)) {
// Channel is now ready
System.out.println("Channel is ready for calls");
}// Check DirectPath status
if (transportChannel.isDirectPath()) {
System.out.println("Using DirectPath optimization for Google Cloud");
// DirectPath provides optimized networking for Google Cloud services
} else {
System.out.println("Using standard gRPC transport");
}
// Get transport name
String transportName = GrpcTransportChannel.getGrpcTransportName();
System.out.println("Transport: " + transportName);Install with Tessl CLI
npx tessl i tessl/maven-com-google-api--gax-grpc