Example usage for io.netty.channel ChannelOption SO_LINGER

List of usage examples for io.netty.channel ChannelOption SO_LINGER

Introduction

In this page you can find the example usage for io.netty.channel ChannelOption SO_LINGER.

Prototype

ChannelOption SO_LINGER

To view the source code for io.netty.channel ChannelOption SO_LINGER.

Click Source Link

Usage

From source file:reactor.ipc.netty.options.ServerOptions.java

License:Open Source License

static void defaultServerOptions(ServerBootstrap bootstrap) {
    bootstrap.localAddress(LOCALHOST_AUTO_PORT).option(ChannelOption.SO_REUSEADDR, true)
            .option(ChannelOption.SO_BACKLOG, 1000)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.SO_RCVBUF, 1024 * 1024).childOption(ChannelOption.SO_SNDBUF, 1024 * 1024)
            .childOption(ChannelOption.AUTO_READ, false).childOption(ChannelOption.SO_KEEPALIVE, true)
            .childOption(ChannelOption.SO_LINGER, 0).childOption(ChannelOption.TCP_NODELAY, true)
            .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000);
}

From source file:reactor.tcp.netty.NettyTcpClient.java

License:Open Source License

/**
 * Creates a new NettyTcpClient that will use the given {@code env} for configuration and the given
 * {@code reactor} to send events. The number of IO threads used by the client is configured by
 * the environment's {@code reactor.tcp.ioThreadCount} property. In its absence the number of
 * IO threads will be equal to the {@link Environment#PROCESSORS number of available processors}.
 * </p>//w w  w .  j  ava  2 s  .c  om
 * The client will connect to the given {@code connectAddress}, configuring its socket using the
 * given {@code opts}. The given {@code codec} will be used for encoding and decoding of data.
 *
 * @param env The configuration environment
 * @param reactor The reactor used to send events
 * @param connectAddress The address the client will connect to
 * @param opts The configuration options for the client's socket
 * @param codec The codec used to encode and decode data
 */
public NettyTcpClient(@Nonnull Environment env, @Nonnull Reactor reactor,
        @Nonnull InetSocketAddress connectAddress, ClientSocketOptions opts,
        @Nullable Codec<Buffer, IN, OUT> codec) {
    super(env, reactor, connectAddress, codec);
    this.eventsReactor = reactor;
    this.options = opts;

    int ioThreadCount = env.getProperty("reactor.tcp.ioThreadCount", Integer.class, Environment.PROCESSORS);
    ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-tcp-io"));

    this.bootstrap = new Bootstrap().group(ioGroup).channel(NioSocketChannel.class)
            .option(ChannelOption.SO_RCVBUF, options.rcvbuf()).option(ChannelOption.SO_SNDBUF, options.sndbuf())
            .option(ChannelOption.SO_KEEPALIVE, options.keepAlive())
            .option(ChannelOption.SO_LINGER, options.linger())
            .option(ChannelOption.TCP_NODELAY, options.tcpNoDelay()).remoteAddress(connectAddress)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(final SocketChannel ch) throws Exception {
                    ch.config().setConnectTimeoutMillis(options.timeout());
                    ch.pipeline().addLast(createChannelHandlers(ch));

                    ch.closeFuture().addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            close(ch);
                        }
                    });
                }
            });
}