Example usage for io.netty.bootstrap Bootstrap validate

List of usage examples for io.netty.bootstrap Bootstrap validate

Introduction

In this page you can find the example usage for io.netty.bootstrap Bootstrap validate.

Prototype

@Override
    public Bootstrap validate() 

Source Link

Usage

From source file:org.apache.zookeeper.ClientCnxnSocketNetty.java

License:Apache License

@Override
void connect(InetSocketAddress addr) throws IOException {
    firstConnect = new CountDownLatch(1);

    Bootstrap bootstrap = new Bootstrap().group(eventLoopGroup).channel(NettyUtils.nioOrEpollSocketChannel())
            .option(ChannelOption.SO_LINGER, -1).option(ChannelOption.TCP_NODELAY, true)
            .handler(new ZKClientPipelineFactory(addr.getHostString(), addr.getPort()));
    bootstrap = configureBootstrapAllocator(bootstrap);
    bootstrap.validate();

    connectLock.lock();//from   w  w w . java2 s .  c o m
    try {
        connectFuture = bootstrap.connect(addr);
        connectFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                // this lock guarantees that channel won't be assigned after cleanup().
                connectLock.lock();
                try {
                    if (!channelFuture.isSuccess()) {
                        LOG.info("future isn't success, cause:", channelFuture.cause());
                        return;
                    } else if (connectFuture == null) {
                        LOG.info("connect attempt cancelled");
                        // If the connect attempt was cancelled but succeeded
                        // anyway, make sure to close the channel, otherwise
                        // we may leak a file descriptor.
                        channelFuture.channel().close();
                        return;
                    }
                    // setup channel, variables, connection, etc.
                    channel = channelFuture.channel();

                    disconnected.set(false);
                    initialized = false;
                    lenBuffer.clear();
                    incomingBuffer = lenBuffer;

                    sendThread.primeConnection();
                    updateNow();
                    updateLastSendAndHeard();

                    if (sendThread.tunnelAuthInProgress()) {
                        waitSasl.drainPermits();
                        needSasl.set(true);
                        sendPrimePacket();
                    } else {
                        needSasl.set(false);
                    }
                    LOG.info("channel is connected: {}", channelFuture.channel());
                } finally {
                    connectFuture = null;
                    connectLock.unlock();
                    // need to wake on connect success or failure to avoid
                    // timing out ClientCnxn.SendThread which may be
                    // blocked waiting for first connect in doTransport().
                    wakeupCnxn();
                    firstConnect.countDown();
                }
            }
        });
    } finally {
        connectLock.unlock();
    }
}

From source file:org.elasticsearch.hadoop.transport.netty4.Netty4Transport.java

License:Apache License

private Bootstrap createBootstrap() {
    final Bootstrap bootstrap = new Bootstrap();
    if (TCP_BLOCKING_CLIENT.get(settings)) {
        bootstrap.group(new OioEventLoopGroup(1,
                daemonThreadFactory(settings, TRANSPORT_CLIENT_WORKER_THREAD_NAME_PREFIX)));
        bootstrap.channel(OioSocketChannel.class);
    } else {/* w w w.  ja va  2  s.  com*/
        bootstrap.group(new NioEventLoopGroup(workerCount,
                daemonThreadFactory(settings, TRANSPORT_CLIENT_BOSS_THREAD_NAME_PREFIX)));
        bootstrap.channel(NioSocketChannel.class);
    }

    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast("size", new Netty4SizeHeaderFrameDecoder());
            // using a dot as a prefix means this cannot come from any settings parsed
            ch.pipeline().addLast("dispatcher",
                    new Netty4MessageChannelHandler(Netty4Transport.this, ".client"));
        }

    });

    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.toIntExact(connectTimeout.millis()));
    bootstrap.option(ChannelOption.TCP_NODELAY, TCP_NO_DELAY.get(settings));
    bootstrap.option(ChannelOption.SO_KEEPALIVE, TCP_KEEP_ALIVE.get(settings));

    final ByteSizeValue tcpSendBufferSize = TCP_SEND_BUFFER_SIZE.get(settings);
    if (tcpSendBufferSize.bytes() > 0) {
        bootstrap.option(ChannelOption.SO_SNDBUF, Math.toIntExact(tcpSendBufferSize.bytes()));
    }

    final ByteSizeValue tcpReceiveBufferSize = TCP_RECEIVE_BUFFER_SIZE.get(settings);
    if (tcpReceiveBufferSize.bytes() > 0) {
        bootstrap.option(ChannelOption.SO_RCVBUF, Math.toIntExact(tcpReceiveBufferSize.bytes()));
    }

    bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, recvByteBufAllocator);

    final boolean reuseAddress = TCP_REUSE_ADDRESS.get(settings);
    bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);

    bootstrap.validate();

    return bootstrap;
}

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

License:Apache License

private Bootstrap createBootstrap() {
    final Bootstrap bootstrap = new Bootstrap();
    if (TCP_BLOCKING_CLIENT.get(settings)) {
        bootstrap.group(new OioEventLoopGroup(1,
                daemonThreadFactory(settings, TRANSPORT_CLIENT_WORKER_THREAD_NAME_PREFIX)));
        bootstrap.channel(OioSocketChannel.class);
    } else {//from ww  w .  j a  v a  2  s .co  m
        bootstrap.group(new NioEventLoopGroup(workerCount,
                daemonThreadFactory(settings, TRANSPORT_CLIENT_BOSS_THREAD_NAME_PREFIX)));
        bootstrap.channel(NioSocketChannel.class);
    }

    bootstrap.handler(getClientChannelInitializer());

    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.toIntExact(connectTimeout.millis()));
    bootstrap.option(ChannelOption.TCP_NODELAY, TCP_NO_DELAY.get(settings));
    bootstrap.option(ChannelOption.SO_KEEPALIVE, TCP_KEEP_ALIVE.get(settings));

    final ByteSizeValue tcpSendBufferSize = TCP_SEND_BUFFER_SIZE.get(settings);
    if (tcpSendBufferSize.bytes() > 0) {
        bootstrap.option(ChannelOption.SO_SNDBUF, Math.toIntExact(tcpSendBufferSize.bytes()));
    }

    final ByteSizeValue tcpReceiveBufferSize = TCP_RECEIVE_BUFFER_SIZE.get(settings);
    if (tcpReceiveBufferSize.bytes() > 0) {
        bootstrap.option(ChannelOption.SO_RCVBUF, Math.toIntExact(tcpReceiveBufferSize.bytes()));
    }

    bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, recvByteBufAllocator);

    final boolean reuseAddress = TCP_REUSE_ADDRESS.get(settings);
    bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);

    bootstrap.validate();

    return bootstrap;
}