Core gRPC library containing transport implementation, channel abstraction, load balancing, name resolution, and other fundamental gRPC functionalities for Java
—
gRPC Core defines the fundamental transport abstractions that enable different transport implementations (Netty, OkHttp, in-process) to work with the gRPC framework. These interfaces provide contracts for client and server communication.
Defines the contract for client-side transport implementations.
/**
* Interface for client-side transport implementations
* Located: io.grpc.internal.ClientTransport
*/
interface ClientTransport extends InternalInstrumented<SocketStats> {
/**
* Creates a new stream for an RPC call
* @param method The method descriptor for the RPC
* @param headers Initial metadata/headers for the request
* @param callOptions Options for this specific call
* @param tracers Array of tracers for monitoring the stream
* @return ClientStream instance for this RPC
*/
ClientStream newStream(
MethodDescriptor<?, ?> method,
Metadata headers,
CallOptions callOptions,
ClientStreamTracer[] tracers
);
/**
* Ping the remote endpoint to verify connectivity
* @param callback Callback to invoke when ping completes
* @param executor Executor to run the callback on
*/
void ping(PingCallback callback, Executor executor);
}Extended interface for connection-based client transports.
/**
* Interface for connection-based client transports
* Located: io.grpc.internal.ConnectionClientTransport
*/
interface ConnectionClientTransport extends ClientTransport {
/**
* Initiates shutdown of the transport
* @param reason Status indicating reason for shutdown
* @return ListenableFuture that completes when shutdown is initiated
*/
ListenableFuture<Void> shutdown(Status reason);
/**
* Initiates graceful shutdown with specified timeout
* @param reason Status indicating reason for shutdown
* @param timeout Timeout for graceful shutdown
* @param unit Time unit for timeout
* @return ListenableFuture that completes when shutdown is complete
*/
ListenableFuture<Void> shutdownNow(Status reason, long timeout, TimeUnit unit);
/**
* Gets connectivity state of the transport
* @return Current connectivity state
*/
ConnectivityState getConnectivityState();
/**
* Registers a listener for connectivity state changes
* @param state State to watch for changes from
* @param listener Listener to notify of state changes
* @param executor Executor to run listener on
*/
void notifyWhenStateChanged(
ConnectivityState state,
Runnable listener,
Executor executor
);
}Defines the contract for server-side transport implementations.
/**
* Interface for server-side transport implementations
* Located: io.grpc.internal.ServerTransport
*/
interface ServerTransport {
/**
* Starts the server transport with the given listener
* @param listener Listener for transport events
*/
void start(ServerTransportListener listener);
/**
* Initiates graceful shutdown of the transport
*/
void shutdown();
/**
* Initiates immediate shutdown of the transport
* @param reason Status indicating reason for shutdown
*/
void shutdownNow(Status reason);
/**
* Gets the log ID for this transport (for debugging/logging)
* @return Unique log ID string
*/
InternalLogId getLogId();
/**
* Gets the attributes associated with this transport
* @return Transport attributes
*/
Attributes getAttributes();
}Listener interfaces for handling transport events.
/**
* Listener for server transport events
* Located: io.grpc.internal.ServerTransportListener
*/
interface ServerTransportListener {
/**
* Called when a new stream is created on the transport
* @param stream The new server stream
* @param method Method descriptor for the RPC
* @param headers Initial metadata from the client
*/
void streamCreated(ServerStream stream, String method, Metadata headers);
/**
* Called when the transport is ready to accept streams
* @param attributes Updated transport attributes
*/
void transportReady(Attributes attributes);
/**
* Called when the transport is shutting down
* @param status Status indicating reason for shutdown
*/
void transportTerminated(Status status);
}
/**
* Listener for client transport events
* Located: io.grpc.internal.ClientTransportListener
*/
interface ClientTransportListener {
/**
* Called when the transport is ready for use
*/
void transportReady();
/**
* Called when the transport is shutting down
* @param status Status indicating reason for shutdown
*/
void transportShutdown(Status status);
/**
* Called when the transport is completely terminated
* @param status Status indicating reason for termination
*/
void transportTerminated(Status status);
/**
* Called when transport is in use (has active streams)
* @param inUse true if transport has active streams
*/
void transportInUse(boolean inUse);
}Callback interface for ping operations.
/**
* Callback for ping operations
* Located: io.grpc.internal.ClientTransport.PingCallback
*/
interface PingCallback {
/**
* Called when ping operation succeeds
* @param roundTripTimeNanos Round trip time in nanoseconds
*/
void onSuccess(long roundTripTimeNanos);
/**
* Called when ping operation fails
* @param cause Status indicating the failure reason
*/
void onFailure(Status cause);
}Special transport implementation that queues operations until the actual transport is ready.
/**
* Transport that delays operations until ready
* Located: io.grpc.internal.DelayedClientTransport
*/
class DelayedClientTransport implements ClientTransport {
/**
* Creates a new delayed client transport
* @param executor Executor for callback operations
* @param syncContext Synchronization context
*/
public DelayedClientTransport(Executor executor, SynchronizationContext syncContext);
/**
* Sets the real transport once it becomes available
* @param transport The actual transport to delegate to
*/
public final void setTransport(ClientTransport transport);
/**
* Shuts down the transport with the given status
* @param status Status indicating shutdown reason
* @param errorDescription Optional error description
*/
public final void shutdownNow(Status status, String errorDescription);
@Override
public ClientStream newStream(
MethodDescriptor<?, ?> method,
Metadata headers,
CallOptions callOptions,
ClientStreamTracer[] tracers
);
@Override
public void ping(PingCallback callback, Executor executor);
}Base class for transport implementations that delegate to another transport.
/**
* Base class for forwarding client transports
* Located: io.grpc.internal.ForwardingClientTransport
*/
abstract class ForwardingClientTransport implements ClientTransport {
/**
* Gets the delegate transport
* @return The transport to forward calls to
*/
protected abstract ClientTransport delegate();
@Override
public ClientStream newStream(
MethodDescriptor<?, ?> method,
Metadata headers,
CallOptions callOptions,
ClientStreamTracer[] tracers
) {
return delegate().newStream(method, headers, callOptions, tracers);
}
@Override
public void ping(PingCallback callback, Executor executor) {
delegate().ping(callback, executor);
}
}Example:
// Transport factory creates transport
ClientTransport transport = factory.newClientTransport(serverAddress, options);
// Create stream for RPC
ClientStream stream = transport.newStream(method, headers, callOptions, tracers);
// Use stream for communication
stream.writeMessage(requestMessage);
stream.halfClose();
// Shutdown transport when done
if (transport instanceof ConnectionClientTransport) {
((ConnectionClientTransport) transport).shutdown(Status.OK);
}Example:
// Server transport starts with listener
ServerTransportListener listener = new ServerTransportListener() {
@Override
public void streamCreated(ServerStream stream, String method, Metadata headers) {
// Handle new stream
ServerStreamListener streamListener = serviceRegistry.lookupMethod(method);
stream.setListener(streamListener);
}
@Override
public void transportReady(Attributes attributes) {
// Transport ready for connections
}
};
serverTransport.start(listener);Transport layer error handling:
Install with Tessl CLI
npx tessl i tessl/maven-io-grpc--grpc-core