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:server.edges.EdgeMonitor.java

License:Apache License

@Override
public synchronized void onAdd(EdgeInfo ei) {
    try {/*  ww w .ja  v a2s.c  o m*/
        EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap b = new Bootstrap();
        b.handler(new WorkHandler(state));

        b.group(group).channel(NioSocketChannel.class).handler(new WorkInit(state, false));
        b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
        b.option(ChannelOption.TCP_NODELAY, true);
        b.option(ChannelOption.SO_KEEPALIVE, true);

        // Make the connection attempt.
        ChannelFuture cf = b.connect(ei.getHost(), ei.getPort()).syncUninterruptibly();

        ei.setChannel(cf.channel());
        ei.setActive(true);
        cf.channel().closeFuture();
    } catch (Exception ex) {
        ex.printStackTrace();
        // Vinit --- > Nullifying the exception so that it further sends the
        // PingMessage
    }

}

From source file:snapchatproto.client.comm.CommConnection.java

License:Apache License

private void init() {
    // the queue to support client-side surging
    outbound = new LinkedBlockingDeque<com.google.protobuf.GeneratedMessage>();

    group = new NioEventLoopGroup();
    try {/*from w ww.  jav a  2  s .  c o m*/
        handler = new CommHandler();
        CommInitializer ci = new CommInitializer(handler, false);
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(ci);

        b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
        b.option(ChannelOption.TCP_NODELAY, true);
        b.option(ChannelOption.SO_KEEPALIVE, true);

        // Make the connection attempts
        channel = b.connect(host, port).syncUninterruptibly();
        logger.info("CommConnection - Channel connection established");

        // want to monitor the connection to the server s.t. if we loose the
        // connection, we can try to re-establish it.
        ClientClosedListener ccl = new ClientClosedListener(this);
        channel.channel().closeFuture().addListener(ccl);

    } catch (Exception ex) {
        ex.printStackTrace();
        logger.error("CommConnection - Failed to initialize the client connection", ex);

    }
    // start out-bound message processor
    worker = new OutboundWorker(this);
    worker.start();
}

From source file:snapchatproto.servers.data.DataMonitor.java

License:Apache License

/**
 * create connection to remote server/*ww w  .  jav a 2 s.  c  o  m*/
 * 
 * @return
 */
protected Channel connect() {
    // Start the connection attempt.
    if (channel == null) {
        try {
            handler = new DataMonitorHandler();
            DataMonitorInitializer mi = new DataMonitorInitializer(false, handler);

            Bootstrap b = new Bootstrap();
            b.group(group).channel(NioSocketChannel.class).handler(mi);
            b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
            b.option(ChannelOption.TCP_NODELAY, true);
            b.option(ChannelOption.SO_KEEPALIVE, true);
            logger.info("Attempting to connect to " + host + port);

            // Make the connection attempt.
            channel = b.connect(host, port).syncUninterruptibly();
            channel.awaitUninterruptibly(5000l);

            logger.info("Sysout 2 Attempting to connect to " + host + port);

            channel.channel().closeFuture().addListener(new DataMonitorClosedListener(this));

            if (N == Integer.MAX_VALUE)
                N = 1;
            else
                N++;

            if (listeners.size() > 0) {

                for (DataMonitorListener ml : listeners) {
                    handler.addListener(ml);
                    logger.info("The listeners size is " + listeners.size());

                }
                listeners.clear();
            }
        } catch (Exception ex) {
            if (logger.isDebugEnabled())
                logger.debug("DataMonitor: failed to initialize the Databeat connection", ex);
        }
    }

    if (channel != null && channel.isDone() && channel.isSuccess()) {
        logger.info("Data Channel Created");
        return channel.channel();
    } else {
        return channel.channel();
    }
}

From source file:spark.network.netty.FileClient.java

License:Apache License

public void init() {
    bootstrap = new Bootstrap();
    bootstrap.group(new OioEventLoopGroup()).channel(OioSocketChannel.class)
            .option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.TCP_NODELAY, true)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout)
            .handler(new FileClientChannelInitializer(handler));
}

From source file:subterranean.crimson.permajar.stage1.network.Communications.java

License:Open Source License

public static synchronized boolean connect(final String HOST, final int PORT, final boolean SSL) {
    if (SSL) {/* w ww  . j a  v  a 2 s .  co m*/
        Logger.add("Making SSL Connection attempt");
    } else {
        Logger.add("Making Connection attempt");
    }

    handler = new Handler();

    if (SSL) {
        try {
            sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
        } catch (SSLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        sslCtx = null;
    }

    EventLoopGroup group = new NioEventLoopGroup();

    Bootstrap b = new Bootstrap();
    b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Environment.reportTimeout);
    b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) {

            if (sslCtx != null) {
                ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
            }
            ch.pipeline().addLast(new ObjectEncoder(),
                    new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)), handler);

        }
    });

    ChannelFuture future = b.connect(new InetSocketAddress(HOST, PORT));
    if (!future.awaitUninterruptibly().isSuccess()) {
        group.shutdownGracefully();
        return false;
    }

    c = future.channel();

    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return c.isWritable();
}

From source file:tp.Client.java

public Client(String remotehost, int port, ConnectionFeedBack connectionFeedBack) {
    //int processors = Runtime.getRuntime().availableProcessors();
    //pool = Executors.newFixedThreadPool(processors);
    this.connectionFeedBack = connectionFeedBack;
    //uniqueId = new UniqueId();
    this.remotehost = remotehost;
    this.port = port;

    bootstrap = new Bootstrap();
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000);
    replies = new ConcurrentHashMap();
    //ini();//  ww w  .j av  a 2 s  . c  o  m
}