A shaded version of the Netty transport for gRPC-Java that includes relocated Netty dependencies to avoid version conflicts
—
Client-side channel creation and configuration using Netty transport. The NettyChannelBuilder provides comprehensive options for connection tuning, security configuration, and advanced Netty features.
Create NettyChannelBuilder instances for different target types.
/**
* Create a channel builder for a specific host and port
* @param host The target host
* @param port The target port
* @return NettyChannelBuilder for configuration
*/
public static NettyChannelBuilder forAddress(String host, int port);
/**
* Create a channel builder for a host and port with credentials
*/
public static NettyChannelBuilder forAddress(String host, int port, ChannelCredentials creds);
/**
* Create a channel builder for a socket address
* @param serverAddress The target socket address
* @return NettyChannelBuilder for configuration
*/
public static NettyChannelBuilder forAddress(SocketAddress serverAddress);
/**
* Create a channel builder for a socket address with credentials
*/
public static NettyChannelBuilder forAddress(SocketAddress serverAddress, ChannelCredentials creds);
/**
* Create a channel builder for a target string (supports service discovery)
* @param target The target string (e.g., "dns:///example.com:443")
* @return NettyChannelBuilder for configuration
*/
public static NettyChannelBuilder forTarget(String target);
/**
* Create a channel builder for a target string with credentials
*/
public static NettyChannelBuilder forTarget(String target, ChannelCredentials creds);Usage Examples:
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
import io.grpc.InsecureChannelCredentials;
// Basic channel
NettyChannelBuilder builder = NettyChannelBuilder.forAddress("localhost", 9090);
// Channel with credentials
NettyChannelBuilder secureBuilder = NettyChannelBuilder.forAddress(
"secure-service.example.com", 443,
InsecureChannelCredentials.create()
);
// Service discovery
NettyChannelBuilder discoveryBuilder = NettyChannelBuilder.forTarget("dns:///service.local:8080");Configure the underlying Netty channel type and factory.
/**
* Set the channel type for the client connection
* @param channelType The Netty channel class to use
* @return This builder for chaining
*/
public NettyChannelBuilder channelType(Class<? extends Channel> channelType);
/**
* Set the channel type with transport socket type
*/
public NettyChannelBuilder channelType(
Class<? extends Channel> channelType,
Class<? extends SocketAddress> transportSocketType);
/**
* Set a custom channel factory
* @param channelFactory Factory for creating channels
* @return This builder for chaining
*/
public NettyChannelBuilder channelFactory(ChannelFactory<? extends Channel> channelFactory);
/**
* Set a custom channel factory with transport socket type
*/
public NettyChannelBuilder channelFactory(
ChannelFactory<? extends Channel> channelFactory,
Class<? extends SocketAddress> transportSocketType);Configure Netty event loop groups for the channel.
/**
* Set the event loop group for the channel
* @param eventLoopGroup The EventLoopGroup to use (null for default)
* @return This builder for chaining
*/
public NettyChannelBuilder eventLoopGroup(EventLoopGroup eventLoopGroup);Usage Example:
import io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoopGroup;
import io.grpc.netty.shaded.io.netty.channel.socket.nio.NioSocketChannel;
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);
NettyChannelBuilder builder = NettyChannelBuilder
.forAddress("localhost", 9090)
.channelType(NioSocketChannel.class)
.eventLoopGroup(eventLoopGroup);Configure Netty channel options.
/**
* Set a Netty channel option
* @param option The channel option to set
* @param value The value for the option
* @return This builder for chaining
*/
public <T> NettyChannelBuilder withOption(ChannelOption<T> option, T value);Usage Example:
import io.grpc.netty.shaded.io.netty.channel.ChannelOption;
NettyChannelBuilder builder = NettyChannelBuilder
.forAddress("localhost", 9090)
.withOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
.withOption(ChannelOption.SO_KEEPALIVE, true);Configure security and protocol negotiation.
/**
* Set the negotiation type for the connection
* @param type The negotiation type to use
* @return This builder for chaining
*/
public NettyChannelBuilder negotiationType(NegotiationType type);
/**
* Set a custom SSL context
* @param sslContext The Netty SSL context
* @return This builder for chaining
*/
public NettyChannelBuilder sslContext(SslContext sslContext);
/**
* Use plaintext connection (no encryption)
* @return This builder for chaining
*/
public NettyChannelBuilder usePlaintext();
/**
* Use transport security (TLS encryption)
* @return This builder for chaining
*/
public NettyChannelBuilder useTransportSecurity();Configure HTTP/2 flow control settings.
/**
* Set the initial flow control window size
* @param initialFlowControlWindow Initial window size in bytes
* @return This builder for chaining
*/
public NettyChannelBuilder initialFlowControlWindow(int initialFlowControlWindow);
/**
* Set the flow control window size
* @param flowControlWindow Window size in bytes
* @return This builder for chaining
*/
public NettyChannelBuilder flowControlWindow(int flowControlWindow);Usage Example:
NettyChannelBuilder builder = NettyChannelBuilder
.forAddress("localhost", 9090)
.initialFlowControlWindow(1024 * 1024) // 1 MB
.flowControlWindow(2 * 1024 * 1024); // 2 MBConfigure size limits for messages and metadata.
/**
* Set the maximum inbound metadata size
* @param bytes Maximum metadata size in bytes
* @return This builder for chaining
*/
public NettyChannelBuilder maxInboundMetadataSize(int bytes);
/**
* Set soft and hard limits for inbound metadata size
*/
public NettyChannelBuilder maxInboundMetadataSize(int soft, int max);
/**
* Set the maximum inbound message size
* @param max Maximum message size in bytes
* @return This builder for chaining
*/
public NettyChannelBuilder maxInboundMessageSize(int max);
/**
* Set the maximum header list size (deprecated)
* @param maxHeaderListSize Maximum header list size in bytes
* @return This builder for chaining
* @deprecated Use maxInboundMetadataSize(int) instead
*/
@Deprecated
public NettyChannelBuilder maxHeaderListSize(int maxHeaderListSize);Configure connection keep-alive behavior.
/**
* Set the keep-alive time
* @param keepAliveTime Time between keep-alive pings
* @param timeUnit Time unit for keepAliveTime
* @return This builder for chaining
*/
public NettyChannelBuilder keepAliveTime(long keepAliveTime, TimeUnit timeUnit);
/**
* Set the keep-alive timeout
* @param keepAliveTimeout Timeout for keep-alive response
* @param timeUnit Time unit for keepAliveTimeout
* @return This builder for chaining
*/
public NettyChannelBuilder keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit);
/**
* Enable/disable keep-alive without active calls
* @param enable Whether to send keep-alive when no calls are active
* @return This builder for chaining
*/
public NettyChannelBuilder keepAliveWithoutCalls(boolean enable);Usage Example:
import java.util.concurrent.TimeUnit;
NettyChannelBuilder builder = NettyChannelBuilder
.forAddress("localhost", 9090)
.keepAliveTime(30, TimeUnit.SECONDS)
.keepAliveTimeout(5, TimeUnit.SECONDS)
.keepAliveWithoutCalls(true);Advanced configuration options for specialized use cases.
/**
* Set a custom local socket picker
* @param localSocketPicker Picker for local socket addresses
* @return This builder for chaining
*/
public NettyChannelBuilder localSocketPicker(LocalSocketPicker localSocketPicker);
/**
* Local socket picker for custom socket address selection
*/
public static class LocalSocketPicker {
/**
* Create a local socket address for the given remote address
* @param remoteAddress The remote address being connected to
* @param attrs Connection attributes
* @return Local socket address or null for default
*/
@Nullable
public SocketAddress createSocketAddress(
SocketAddress remoteAddress,
@EquivalentAddressGroup.Attr Attributes attrs);
}/**
* Default flow control window size (1 MiB)
*/
public static final int DEFAULT_FLOW_CONTROL_WINDOW = 1024 * 1024;Install with Tessl CLI
npx tessl i tessl/maven-io-grpc--grpc-netty-shaded