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:org.asynchttpclient.netty.channel.ChannelManager.java

License:Open Source License

private Bootstrap newBootstrap(ChannelFactory<? extends Channel> channelFactory, EventLoopGroup eventLoopGroup,
        AsyncHttpClientConfig config) {//from w  w  w . java2 s .  c o m
    @SuppressWarnings("deprecation")
    Bootstrap bootstrap = new Bootstrap().channelFactory(channelFactory).group(eventLoopGroup)//
            // default to PooledByteBufAllocator
            .option(ChannelOption.ALLOCATOR,
                    config.isUsePooledMemory() ? PooledByteBufAllocator.DEFAULT
                            : UnpooledByteBufAllocator.DEFAULT)//
            .option(ChannelOption.TCP_NODELAY, config.isTcpNoDelay())//
            .option(ChannelOption.SO_REUSEADDR, config.isSoReuseAddress())//
            .option(ChannelOption.AUTO_CLOSE, false);

    if (config.getConnectTimeout() > 0) {
        bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout());
    }

    if (config.getSoLinger() >= 0) {
        bootstrap.option(ChannelOption.SO_LINGER, config.getSoLinger());
    }

    if (config.getSoSndBuf() >= 0) {
        bootstrap.option(ChannelOption.SO_SNDBUF, config.getSoSndBuf());
    }

    if (config.getSoRcvBuf() >= 0) {
        bootstrap.option(ChannelOption.SO_RCVBUF, config.getSoRcvBuf());
    }

    for (Entry<ChannelOption<Object>, Object> entry : config.getChannelOptions().entrySet()) {
        bootstrap.option(entry.getKey(), entry.getValue());
    }

    return bootstrap;
}

From source file:org.elasticsearch.transport.netty4.Netty4TcpChannel.java

License:Apache License

@Override
public void setSoLinger(int value) {
    channel.config().setOption(ChannelOption.SO_LINGER, value);
}

From source file:org.jupiter.transport.netty.NettyTcpAcceptor.java

License:Apache License

@Override
protected void setOptions() {
    super.setOptions();

    ServerBootstrap boot = bootstrap();/*  w  ww. jav a  2s.  c o m*/

    // parent options
    NettyConfig.NettyTcpConfigGroup.ParentConfig parent = configGroup.parent();
    boot.option(ChannelOption.SO_BACKLOG, parent.getBacklog());
    boot.option(ChannelOption.SO_REUSEADDR, parent.isReuseAddress());
    if (parent.getRcvBuf() > 0) {
        boot.option(ChannelOption.SO_RCVBUF, parent.getRcvBuf());
    }

    // child options
    NettyConfig.NettyTcpConfigGroup.ChildConfig child = configGroup.child();
    boot.childOption(ChannelOption.SO_REUSEADDR, child.isReuseAddress())
            .childOption(ChannelOption.SO_KEEPALIVE, child.isKeepAlive())
            .childOption(ChannelOption.TCP_NODELAY, child.isTcpNoDelay())
            .childOption(ChannelOption.ALLOW_HALF_CLOSURE, child.isAllowHalfClosure());
    if (child.getRcvBuf() > 0) {
        boot.childOption(ChannelOption.SO_RCVBUF, child.getRcvBuf());
    }
    if (child.getSndBuf() > 0) {
        boot.childOption(ChannelOption.SO_SNDBUF, child.getSndBuf());
    }
    if (child.getLinger() > 0) {
        boot.childOption(ChannelOption.SO_LINGER, child.getLinger());
    }
    if (child.getIpTos() > 0) {
        boot.childOption(ChannelOption.IP_TOS, child.getIpTos());
    }
    int bufLowWaterMark = child.getWriteBufferLowWaterMark();
    int bufHighWaterMark = child.getWriteBufferHighWaterMark();
    if (bufLowWaterMark >= 0 && bufHighWaterMark > 0) {
        WriteBufferWaterMark waterMark = new WriteBufferWaterMark(bufLowWaterMark, bufHighWaterMark);
        boot.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark);
    }
}

From source file:org.jupiter.transport.netty.NettyTcpConnector.java

License:Apache License

@Override
protected void setOptions() {
    super.setOptions();

    Bootstrap boot = bootstrap();//from  w ww.ja  v a2  s . c  o  m

    NettyConfig.NettyTcpConfigGroup.ChildConfig child = childConfig;

    // child options
    boot.option(ChannelOption.SO_REUSEADDR, child.isReuseAddress())
            .option(ChannelOption.SO_KEEPALIVE, child.isKeepAlive())
            .option(ChannelOption.TCP_NODELAY, child.isTcpNoDelay())
            .option(ChannelOption.ALLOW_HALF_CLOSURE, child.isAllowHalfClosure());
    if (child.getRcvBuf() > 0) {
        boot.option(ChannelOption.SO_RCVBUF, child.getRcvBuf());
    }
    if (child.getSndBuf() > 0) {
        boot.option(ChannelOption.SO_SNDBUF, child.getSndBuf());
    }
    if (child.getLinger() > 0) {
        boot.option(ChannelOption.SO_LINGER, child.getLinger());
    }
    if (child.getIpTos() > 0) {
        boot.option(ChannelOption.IP_TOS, child.getIpTos());
    }
    if (child.getConnectTimeoutMillis() > 0) {
        boot.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, child.getConnectTimeoutMillis());
    }
    int bufLowWaterMark = child.getWriteBufferLowWaterMark();
    int bufHighWaterMark = child.getWriteBufferHighWaterMark();
    if (bufLowWaterMark >= 0 && bufHighWaterMark > 0) {
        WriteBufferWaterMark waterMark = new WriteBufferWaterMark(bufLowWaterMark, bufHighWaterMark);
        boot.option(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark);
    }
}

From source file:org.jupiter.transport.netty.NettyUdtAcceptor.java

License:Apache License

@Override
protected void setOptions() {
    super.setOptions();

    ServerBootstrap boot = bootstrap();//from  w  w  w  . ja va2s  . c o m

    // parent options
    NettyUdtConfigGroup.ParentConfig parent = configGroup.parent();
    boot.option(ChannelOption.SO_BACKLOG, parent.getBacklog());

    // child options
    NettyUdtConfigGroup.ChildConfig child = configGroup.child();
    boot.childOption(ChannelOption.SO_REUSEADDR, child.isReuseAddress());
    if (child.getRcvBuf() > 0) {
        boot.childOption(ChannelOption.SO_RCVBUF, child.getRcvBuf());
    }
    if (child.getSndBuf() > 0) {
        boot.childOption(ChannelOption.SO_SNDBUF, child.getSndBuf());
    }
    if (child.getLinger() > 0) {
        boot.childOption(ChannelOption.SO_LINGER, child.getLinger());
    }
    int bufLowWaterMark = child.getWriteBufferLowWaterMark();
    int bufHighWaterMark = child.getWriteBufferHighWaterMark();
    if (bufLowWaterMark >= 0 && bufHighWaterMark > 0) {
        WriteBufferWaterMark waterMark = new WriteBufferWaterMark(bufLowWaterMark, bufHighWaterMark);
        boot.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark);
    }
}

From source file:org.jupiter.transport.netty.NettyUdtConnector.java

License:Apache License

@Override
protected void setOptions() {
    super.setOptions();

    Bootstrap boot = bootstrap();/*from w  w w  . j  a  v a2  s .  co m*/

    NettyUdtConfigGroup.ChildConfig child = childConfig;

    // child options
    boot.option(ChannelOption.SO_REUSEADDR, child.isReuseAddress());
    if (child.getRcvBuf() > 0) {
        boot.option(ChannelOption.SO_RCVBUF, child.getRcvBuf());
    }
    if (child.getSndBuf() > 0) {
        boot.option(ChannelOption.SO_SNDBUF, child.getSndBuf());
    }
    if (child.getLinger() > 0) {
        boot.option(ChannelOption.SO_LINGER, child.getLinger());
    }
    if (child.getConnectTimeoutMillis() > 0) {
        boot.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, child.getConnectTimeoutMillis());
    }
    int bufLowWaterMark = child.getWriteBufferLowWaterMark();
    int bufHighWaterMark = child.getWriteBufferHighWaterMark();
    if (bufLowWaterMark >= 0 && bufHighWaterMark > 0) {
        WriteBufferWaterMark waterMark = new WriteBufferWaterMark(bufLowWaterMark, bufHighWaterMark);
        boot.option(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark);
    }
}

From source file:org.vertx.java.core.net.impl.TCPSSLHelper.java

License:Open Source License

public void applyConnectionOptions(ServerBootstrap bootstrap) {
    bootstrap.childOption(ChannelOption.TCP_NODELAY, tcpNoDelay);
    if (tcpSendBufferSize != -1) {
        bootstrap.childOption(ChannelOption.SO_SNDBUF, tcpSendBufferSize);
    }//from   w  ww .ja v a 2  s. co  m
    if (tcpReceiveBufferSize != -1) {
        bootstrap.childOption(ChannelOption.SO_RCVBUF, tcpReceiveBufferSize);
        bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR,
                new FixedRecvByteBufAllocator(tcpReceiveBufferSize));
    }

    bootstrap.option(ChannelOption.SO_LINGER, soLinger);
    if (trafficClass != -1) {
        bootstrap.childOption(ChannelOption.IP_TOS, trafficClass);
    }
    bootstrap.childOption(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);

    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, tcpKeepAlive);
    bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
    bootstrap.option(ChannelOption.SO_BACKLOG, acceptBackLog);
}

From source file:org.vertx.java.core.net.impl.TCPSSLHelper.java

License:Open Source License

public void applyConnectionOptions(Bootstrap bootstrap) {
    bootstrap.option(ChannelOption.TCP_NODELAY, tcpNoDelay);
    if (tcpSendBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, tcpSendBufferSize);
    }// w  ww .  j  ava 2 s . com
    if (tcpReceiveBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, tcpReceiveBufferSize);
        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(tcpReceiveBufferSize));
    }
    bootstrap.option(ChannelOption.SO_LINGER, soLinger);
    if (trafficClass != -1) {
        bootstrap.option(ChannelOption.IP_TOS, trafficClass);
    }
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
    bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, tcpKeepAlive);
}

From source file:reactor.io.net.impl.netty.tcp.NettyTcpClient.java

License:Apache License

/**
 * Creates a new NettyTcpClient that will use the given {@code env} for configuration and the given {@code
 * reactor} to//  w  ww .jav  a2 s.  c  o m
 * 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> 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 dispatcher     The dispatcher used to send events
 * @param connectAddress The address the client will connect to
 * @param options        The configuration options for the client's socket
 * @param sslOptions     The SSL configuration options for the client's socket
 * @param codec          The codec used to encode and decode data
 */
public NettyTcpClient(Environment env, Dispatcher dispatcher, InetSocketAddress connectAddress,
        final ClientSocketOptions options, final SslOptions sslOptions, Codec<Buffer, IN, OUT> codec) {
    super(env, dispatcher, connectAddress, options, sslOptions, codec);
    this.connectAddress = connectAddress;

    if (options instanceof NettyClientSocketOptions) {
        this.nettyOptions = (NettyClientSocketOptions) options;
    } else {
        this.nettyOptions = null;

    }
    if (null != nettyOptions && null != nettyOptions.eventLoopGroup()) {
        this.ioGroup = nettyOptions.eventLoopGroup();
    } else {
        int ioThreadCount = env != null
                ? env.getIntProperty("reactor.tcp.ioThreadCount", Environment.PROCESSORS)
                : Environment.PROCESSORS;
        this.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())
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.AUTO_READ, sslOptions != null)
    //.remoteAddress(this.connectAddress)
    ;

    this.connectionSupplier = new Supplier<ChannelFuture>() {
        @Override
        public ChannelFuture get() {
            if (started.get()) {
                return bootstrap.connect(getConnectAddress());
            } else {
                return null;
            }
        }
    };
}

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

License:Apache License

/**
 * Creates a new NettyTcpClient that will use the given {@code env} for configuration and the given {@code
 * reactor} to/*  w ww  .java  2 s  . c om*/
 * 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> 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 options        The configuration options for the client's socket
 * @param sslOptions     The SSL configuration options for the client's socket
 * @param codec          The codec used to encode and decode data
 * @param consumers      The consumers that will interact with the connection
 */
public NettyTcpClient(@Nonnull Environment env, @Nonnull EventBus reactor,
        @Nonnull InetSocketAddress connectAddress, @Nonnull final ClientSocketOptions options,
        @Nullable final SslOptions sslOptions, @Nullable Codec<Buffer, IN, OUT> codec,
        @Nonnull Collection<Consumer<NetChannel<IN, OUT>>> consumers) {
    super(env, reactor, connectAddress, options, sslOptions, codec, consumers);
    this.connectAddress = connectAddress;

    if (options instanceof NettyClientSocketOptions) {
        this.nettyOptions = (NettyClientSocketOptions) options;
    } else {
        this.nettyOptions = null;

    }
    if (null != nettyOptions && null != nettyOptions.eventLoopGroup()) {
        this.ioGroup = nettyOptions.eventLoopGroup();
    } else {
        int ioThreadCount = env.getProperty("reactor.tcp.ioThreadCount", Integer.class, Environment.PROCESSORS);
        this.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(this.connectAddress)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(final SocketChannel ch) throws Exception {
                    ch.config().setConnectTimeoutMillis(options.timeout());

                    if (null != sslOptions) {
                        SSLEngine ssl = new SSLEngineSupplier(sslOptions, true).get();
                        if (log.isDebugEnabled()) {
                            log.debug("SSL enabled using keystore {}",
                                    (null != sslOptions.keystoreFile() ? sslOptions.keystoreFile()
                                            : "<DEFAULT>"));
                        }
                        ch.pipeline().addLast(new SslHandler(ssl));
                    }
                    if (null != nettyOptions && null != nettyOptions.pipelineConfigurer()) {
                        nettyOptions.pipelineConfigurer().accept(ch.pipeline());
                    }
                    ch.pipeline().addLast(createChannelHandlers(ch));
                }
            });

    this.connectionSupplier = new Supplier<ChannelFuture>() {
        @Override
        public ChannelFuture get() {
            if (!closing) {
                return bootstrap.connect(getConnectAddress());
            } else {
                return null;
            }
        }
    };
}