Example usage for io.netty.channel ChannelOption CONNECT_TIMEOUT_MILLIS

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

Introduction

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

Prototype

ChannelOption CONNECT_TIMEOUT_MILLIS

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

Click Source Link

Usage

From source file:io.vertx.core.http.impl.HttpClientImpl.java

License:Open Source License

private void applyConnectionOptions(Bootstrap bootstrap) {
    bootstrap.option(ChannelOption.TCP_NODELAY, options.isTcpNoDelay());
    if (options.getSendBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, options.getSendBufferSize());
    }/*from  www  .  j  av a2 s . c  om*/
    if (options.getReceiveBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, options.getReceiveBufferSize());
        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR,
                new FixedRecvByteBufAllocator(options.getReceiveBufferSize()));
    }
    bootstrap.option(ChannelOption.SO_LINGER, options.getSoLinger());
    if (options.getTrafficClass() != -1) {
        bootstrap.option(ChannelOption.IP_TOS, options.getTrafficClass());
    }
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.getConnectTimeout());
    bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, options.isTcpKeepAlive());
    bootstrap.option(ChannelOption.SO_REUSEADDR, options.isReuseAddress());
}

From source file:io.vertx.core.net.impl.NetClientBase.java

License:Open Source License

private void applyConnectionOptions(Bootstrap bootstrap) {
    if (options.getLocalAddress() != null) {
        bootstrap.localAddress(options.getLocalAddress(), 0);
    }//from w w  w .  jav  a  2s. c  o m
    bootstrap.option(ChannelOption.TCP_NODELAY, options.isTcpNoDelay());
    if (options.getSendBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, options.getSendBufferSize());
    }
    if (options.getReceiveBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, options.getReceiveBufferSize());
        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR,
                new FixedRecvByteBufAllocator(options.getReceiveBufferSize()));
    }
    if (options.getSoLinger() != -1) {
        bootstrap.option(ChannelOption.SO_LINGER, options.getSoLinger());
    }
    if (options.getTrafficClass() != -1) {
        bootstrap.option(ChannelOption.IP_TOS, options.getTrafficClass());
    }
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.getConnectTimeout());
    bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, options.isTcpKeepAlive());
}

From source file:io.vertx.core.net.impl.transport.Transport.java

License:Open Source License

public void configure(ClientOptionsBase options, Bootstrap bootstrap) {
    if (options.getLocalAddress() != null) {
        bootstrap.localAddress(options.getLocalAddress(), 0);
    }//  w  w w  . ja v  a2 s  .c o m
    bootstrap.option(ChannelOption.TCP_NODELAY, options.isTcpNoDelay());
    if (options.getSendBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, options.getSendBufferSize());
    }
    if (options.getReceiveBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, options.getReceiveBufferSize());
        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR,
                new FixedRecvByteBufAllocator(options.getReceiveBufferSize()));
    }
    if (options.getSoLinger() != -1) {
        bootstrap.option(ChannelOption.SO_LINGER, options.getSoLinger());
    }
    if (options.getTrafficClass() != -1) {
        bootstrap.option(ChannelOption.IP_TOS, options.getTrafficClass());
    }
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.getConnectTimeout());
    bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, options.isTcpKeepAlive());
    bootstrap.option(ChannelOption.SO_REUSEADDR, options.isReuseAddress());
}

From source file:jgnash.engine.attachment.AttachmentTransferClient.java

License:Open Source License

/**
 * Starts the connection with the lock server
 *
 * @return {@code true} if successful//from   w w  w . j  av a2 s.c  o m
 */
public boolean connectToServer(final String host, final int port, final char[] password) {
    boolean result = false;

    // If a password has been specified, create an EncryptionManager
    if (password != null && password.length > 0) {
        encryptionManager = new EncryptionManager(password);
    }

    final Bootstrap bootstrap = new Bootstrap();

    eventLoopGroup = new NioEventLoopGroup();

    transferHandler = new NettyTransferHandler(tempDirectory, encryptionManager);

    bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new Initializer())
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ConnectionFactory.getConnectionTimeout() * 1000)
            .option(ChannelOption.SO_KEEPALIVE, true);

    try {
        // Start the connection attempt.
        channel = bootstrap.connect(host, port).sync().channel();

        result = true;
        logger.info("Connection made with File Transfer Server");
    } catch (final InterruptedException e) {
        logger.log(Level.SEVERE, "Failed to connect to the File Transfer Server", e);
        disconnectFromServer();
    }

    return result;
}

From source file:jgnash.engine.concurrent.DistributedLockManager.java

License:Open Source License

/**
 * Starts the connection with the lock server
 *
 * @param password connection password/*from  w w  w .j  a v a2  s . co  m*/
 * @return {@code true} if successful
 */
public boolean connectToServer(final char[] password) {
    boolean result = false;

    // If a password has been specified, create an EncryptionManager
    if (password != null && password.length > 0) {
        encryptionManager = new EncryptionManager(password);
    }

    final Bootstrap bootstrap = new Bootstrap();

    eventLoopGroup = new NioEventLoopGroup();

    bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new Initializer())
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ConnectionFactory.getConnectionTimeout() * 1000)
            .option(ChannelOption.SO_KEEPALIVE, true);

    try {
        // Start the connection attempt.
        channel = bootstrap.connect(host, port).sync().channel();

        channel.writeAndFlush(encrypt(UUID_PREFIX + uuid) + EOL_DELIMITER).sync(); // send this channels uuid

        result = true;
        logger.info("Connection made with Distributed Lock Server");
    } catch (final InterruptedException e) {
        logger.log(Level.SEVERE, "Failed to connect to Distributed Lock Server", e);
        disconnectFromServer();
    }

    return result;
}

From source file:jgnash.engine.message.MessageBusClient.java

License:Open Source License

public boolean connectToServer(final char[] password) {
    boolean result = false;

    // If a password has been specified, create an EncryptionManager
    if (password != null && password.length > 0) {
        encryptionManager = new EncryptionManager(password);
    }//from   w  w w .  j a  va  2  s  .  c om

    eventLoopGroup = new NioEventLoopGroup();

    final Bootstrap bootstrap = new Bootstrap();

    bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new MessageBusClientInitializer())
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getConnectionTimeout() * 1000)
            .option(ChannelOption.SO_KEEPALIVE, true);

    try {
        // Start the connection attempt.
        channel = bootstrap.connect(host, port).sync().channel();

        result = true;
        logger.info("Connected to remote message server");
    } catch (final InterruptedException e) {
        logger.log(Level.SEVERE, "Failed to connect to remote message bus", e);
        disconnectFromServer();
    }

    return result;
}

From source file:me.ferrybig.javacoding.teamspeakconnector.TeamspeakApi.java

License:Open Source License

private ChannelFuture openChannel(SocketAddress addr, ChannelInitializer<? extends SocketChannel> ch,
        int timneout) {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.remoteAddress(addr);/* w w w . j  a v  a  2s.c  om*/
    bootstrap.handler(ch);
    bootstrap.group(group);
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
    bootstrap.option(ChannelOption.TCP_NODELAY, true); // We send relatively small packets, we benefit more from delayed ack
    return bootstrap.connect();
}

From source file:net.epsilony.utils.codec.modbus.ModbusClientMaster.java

License:Open Source License

protected ChannelFuture genConnectFuture() {

    initializer = new SimpModbusMasterChannelInitializer();
    initializer.setRequestLifeTime(requestLifeTime);
    bootstrap = new Bootstrap();
    bootstrap.group(group).channelFactory(channelFactory).handler(initializer)
            .option(ChannelOption.TCP_NODELAY, tcpNoDelay).option(ChannelOption.SO_KEEPALIVE, keepAlive)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout)
            .option(ChannelOption.SO_TIMEOUT, socketTimeout);
    return bootstrap.connect(innetAddress, inetPort);
}

From source file:net.kuujo.copycat.io.transport.NettyClient.java

License:Apache License

@Override
public CompletableFuture<Connection> connect(Address address) {
    Assert.notNull(address, "address");
    Context context = getContext();
    CompletableFuture<Connection> future = new ComposableFuture<>();

    LOGGER.info("Connecting to {}", address);

    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(eventLoopGroup).channel(
            eventLoopGroup instanceof EpollEventLoopGroup ? EpollSocketChannel.class : NioSocketChannel.class)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override/*from  w  ww  .ja va  2s . c o  m*/
                protected void initChannel(SocketChannel channel) throws Exception {
                    ChannelPipeline pipeline = channel.pipeline();
                    pipeline.addLast(FIELD_PREPENDER);
                    pipeline.addLast(new LengthFieldBasedFrameDecoder(1024 * 32, 0, 2, 0, 2));
                    pipeline.addLast(new ClientHandler(connections, future::complete, context));
                }
            });

    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);
    bootstrap.option(ChannelOption.ALLOCATOR, ALLOCATOR);

    bootstrap.connect(address.socketAddress()).addListener(channelFuture -> {
        if (channelFuture.isSuccess()) {
            LOGGER.info("Connected to {}", address);
        } else {
            context.execute(() -> future.completeExceptionally(channelFuture.cause()));
        }
    });
    return future;
}

From source file:net.kuujo.copycat.netty.NettyTcpProtocolClient.java

License:Apache License

@Override
public CompletableFuture<Void> connect() {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    if (channel != null) {
        future.complete(null);//  w ww  . j av a2 s  .  c om
        return future;
    }

    final SslContext sslContext;
    if (protocol.isSsl()) {
        try {
            sslContext = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
        } catch (SSLException e) {
            future.completeExceptionally(e);
            return future;
        }
    } else {
        sslContext = null;
    }

    group = new NioEventLoopGroup(protocol.getThreads());
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel channel) throws Exception {
            ChannelPipeline pipeline = channel.pipeline();
            if (sslContext != null) {
                pipeline.addLast(sslContext.newHandler(channel.alloc(), host, port));
            }
            pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4));
            pipeline.addLast("bytesDecoder", new ByteArrayDecoder());
            pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
            pipeline.addLast("bytesEncoder", new ByteArrayEncoder());
            pipeline.addLast("handler", channelHandler);
        }
    });

    if (protocol.getSendBufferSize() > -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, protocol.getSendBufferSize());
    }

    if (protocol.getReceiveBufferSize() > -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, protocol.getReceiveBufferSize());
    }

    if (protocol.getTrafficClass() > -1) {
        bootstrap.option(ChannelOption.IP_TOS, protocol.getTrafficClass());
    }

    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.SO_LINGER, protocol.getSoLinger());
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, protocol.getConnectTimeout());

    bootstrap.connect(host, port).addListener((ChannelFutureListener) channelFuture -> {
        if (channelFuture.isSuccess()) {
            channel = channelFuture.channel();
            future.complete(null);
        } else {
            future.completeExceptionally(channelFuture.cause());
        }
    });
    return future;
}