A shaded version of the Netty transport for gRPC-Java that includes relocated Netty dependencies to avoid version conflicts
—
Server-side gRPC server creation and configuration using Netty transport. The NettyServerBuilder provides comprehensive options for server setup, connection management, SSL/TLS configuration, and performance tuning.
Create NettyServerBuilder instances for different server configurations.
/**
* Create a server builder for a specific port
* @param port The port to listen on
* @return NettyServerBuilder for configuration
*/
public static NettyServerBuilder forPort(int port);
/**
* Create a server builder for a port with credentials
*/
public static NettyServerBuilder forPort(int port, ServerCredentials creds);
/**
* Create a server builder for a socket address
* @param address The socket address to bind to
* @return NettyServerBuilder for configuration
*/
public static NettyServerBuilder forAddress(SocketAddress address);
/**
* Create a server builder for a socket address with credentials
*/
public static NettyServerBuilder forAddress(SocketAddress address, ServerCredentials creds);Usage Examples:
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
import io.grpc.InsecureServerCredentials;
// Basic server
NettyServerBuilder builder = NettyServerBuilder.forPort(9090);
// Server with credentials
NettyServerBuilder secureBuilder = NettyServerBuilder.forPort(
9090,
InsecureServerCredentials.create()
);
// Server on specific address
NettyServerBuilder addressBuilder = NettyServerBuilder.forAddress(
new InetSocketAddress("0.0.0.0", 8080)
);Configure additional listen addresses for the server.
/**
* Add an additional address for the server to listen on
* @param listenAddress Additional socket address to bind
* @return This builder for chaining
*/
public NettyServerBuilder addListenAddress(SocketAddress listenAddress);Usage Example:
import java.net.InetSocketAddress;
NettyServerBuilder builder = NettyServerBuilder
.forPort(9090)
.addListenAddress(new InetSocketAddress("127.0.0.1", 9091))
.addListenAddress(new InetSocketAddress("::1", 9092));Configure the underlying Netty server channel type and factory.
/**
* Set the server channel type
* @param channelType The Netty server channel class to use
* @return This builder for chaining
*/
public NettyServerBuilder channelType(Class<? extends ServerChannel> channelType);
/**
* Set a custom server channel factory
* @param channelFactory Factory for creating server channels
* @return This builder for chaining
*/
public NettyServerBuilder channelFactory(ChannelFactory<? extends ServerChannel> channelFactory);Usage Example:
import io.grpc.netty.shaded.io.netty.channel.socket.nio.NioServerSocketChannel;
NettyServerBuilder builder = NettyServerBuilder
.forPort(9090)
.channelType(NioServerSocketChannel.class);Configure Netty channel options for parent and child channels.
/**
* Set a Netty channel option for the server channel
* @param option The channel option to set
* @param value The value for the option
* @return This builder for chaining
*/
public <T> NettyServerBuilder withOption(ChannelOption<T> option, T value);
/**
* Set a Netty channel option for child channels (client connections)
* @param option The channel option to set
* @param value The value for the option
* @return This builder for chaining
*/
public <T> NettyServerBuilder withChildOption(ChannelOption<T> option, T value);Usage Example:
import io.grpc.netty.shaded.io.netty.channel.ChannelOption;
NettyServerBuilder builder = NettyServerBuilder
.forPort(9090)
.withOption(ChannelOption.SO_BACKLOG, 128)
.withChildOption(ChannelOption.SO_KEEPALIVE, true)
.withChildOption(ChannelOption.TCP_NODELAY, true);Configure Netty event loop groups for the server.
/**
* Set the boss event loop group (accepts connections)
* @param group The EventLoopGroup for accepting connections
* @return This builder for chaining
*/
public NettyServerBuilder bossEventLoopGroup(EventLoopGroup group);
/**
* Set the worker event loop group (handles connections)
* @param group The EventLoopGroup for handling connections
* @return This builder for chaining
*/
public NettyServerBuilder workerEventLoopGroup(EventLoopGroup group);Usage Example:
import io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoopGroup;
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(4);
NettyServerBuilder builder = NettyServerBuilder
.forPort(9090)
.bossEventLoopGroup(bossGroup)
.workerEventLoopGroup(workerGroup);Configure SSL/TLS and transport security.
/**
* Set a custom SSL context
* @param sslContext The Netty SSL context
* @return This builder for chaining
*/
public NettyServerBuilder sslContext(SslContext sslContext);
/**
* Configure transport security with certificate files
* @param certChain Certificate chain file
* @param privateKey Private key file
* @return This builder for chaining
*/
public NettyServerBuilder useTransportSecurity(File certChain, File privateKey);
/**
* Configure transport security with certificate streams
* @param certChain Certificate chain input stream
* @param privateKey Private key input stream
* @return This builder for chaining
*/
public NettyServerBuilder useTransportSecurity(InputStream certChain, InputStream privateKey);Usage Example:
import java.io.File;
NettyServerBuilder builder = NettyServerBuilder
.forPort(9090)
.useTransportSecurity(
new File("server.crt"),
new File("server.key")
);Configure connection limits and behavior.
/**
* Set the maximum number of concurrent calls per connection
* @param maxCalls Maximum concurrent calls per connection
* @return This builder for chaining
*/
public NettyServerBuilder maxConcurrentCallsPerConnection(int maxCalls);
/**
* Set the maximum connection idle time
* @param maxConnectionIdle Maximum idle time before closing connection
* @param timeUnit Time unit for maxConnectionIdle
* @return This builder for chaining
*/
public NettyServerBuilder maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit);
/**
* Set the maximum connection age
* @param maxConnectionAge Maximum age before closing connection
* @param timeUnit Time unit for maxConnectionAge
* @return This builder for chaining
*/
public NettyServerBuilder maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit);
/**
* Set the maximum connection age grace period
* @param maxConnectionAgeGrace Grace period after max age
* @param timeUnit Time unit for maxConnectionAgeGrace
* @return This builder for chaining
*/
public NettyServerBuilder maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit);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 NettyServerBuilder initialFlowControlWindow(int initialFlowControlWindow);
/**
* Set the flow control window size
* @param flowControlWindow Window size in bytes
* @return This builder for chaining
*/
public NettyServerBuilder flowControlWindow(int flowControlWindow);Configure size limits for inbound messages and metadata.
/**
* Set the maximum inbound message size
* @param bytes Maximum message size in bytes
* @return This builder for chaining
*/
public NettyServerBuilder maxInboundMessageSize(int bytes);
/**
* Set the maximum inbound metadata size
* @param bytes Maximum metadata size in bytes
* @return This builder for chaining
*/
public NettyServerBuilder maxInboundMetadataSize(int bytes);
/**
* Set soft and hard limits for inbound metadata size
*/
public NettyServerBuilder maxInboundMetadataSize(int soft, int max);
/**
* Set the maximum message size (deprecated)
* @param maxMessageSize Maximum message size in bytes
* @return This builder for chaining
* @deprecated Use maxInboundMessageSize(int) instead
*/
@Deprecated
public NettyServerBuilder maxMessageSize(int maxMessageSize);
/**
* 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 NettyServerBuilder maxHeaderListSize(int maxHeaderListSize);Configure server-side keep-alive behavior and policies.
/**
* Set the keep-alive time (server sends pings)
* @param keepAliveTime Time between keep-alive pings
* @param timeUnit Time unit for keepAliveTime
* @return This builder for chaining
*/
public NettyServerBuilder 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 NettyServerBuilder keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit);
/**
* Set the minimum allowed keep-alive time from clients
* @param keepAliveTime Minimum time between client pings
* @param timeUnit Time unit for keepAliveTime
* @return This builder for chaining
*/
public NettyServerBuilder permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit);
/**
* Allow/disallow keep-alive pings without active calls
* @param permit Whether to allow keep-alive without calls
* @return This builder for chaining
*/
public NettyServerBuilder permitKeepAliveWithoutCalls(boolean permit);Usage Example:
import java.util.concurrent.TimeUnit;
NettyServerBuilder builder = NettyServerBuilder
.forPort(9090)
.keepAliveTime(60, TimeUnit.SECONDS)
.keepAliveTimeout(10, TimeUnit.SECONDS)
.permitKeepAliveTime(5, TimeUnit.SECONDS)
.permitKeepAliveWithoutCalls(false);Configure rate limiting for RST frames to prevent abuse.
/**
* Set the maximum number of RST frames per window
* @param maxRstStream Maximum RST frames allowed
* @param secondsPerWindow Time window in seconds
* @return This builder for chaining
*/
public NettyServerBuilder maxRstFramesPerWindow(int maxRstStream, int secondsPerWindow);Usage Example:
NettyServerBuilder builder = NettyServerBuilder
.forPort(9090)
.maxRstFramesPerWindow(100, 60); // Max 100 RST frames per minute/**
* Default flow control window size (1 MiB)
*/
public static final int DEFAULT_FLOW_CONTROL_WINDOW = 1024 * 1024;
/**
* Value indicating max connection idle is disabled
*/
static final long MAX_CONNECTION_IDLE_NANOS_DISABLED = Long.MAX_VALUE;
/**
* Value indicating max connection age is disabled
*/
static final long MAX_CONNECTION_AGE_NANOS_DISABLED = Long.MAX_VALUE;
/**
* Value indicating infinite connection age grace period
*/
static final long MAX_CONNECTION_AGE_GRACE_NANOS_INFINITE = Long.MAX_VALUE;
/**
* Value indicating RST count limiting is disabled
*/
static final int MAX_RST_COUNT_DISABLED = 0;Install with Tessl CLI
npx tessl i tessl/maven-io-grpc--grpc-netty-shaded