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:poke.monitor.HeartMonitor.java

License:Apache License

/**
 * create connection to remote server//w  w w.j  a  va  2  s  .  co  m
 * 
 * @return
 */
protected Channel connect() {
    // Start the connection attempt.
    if (channel == null) {
        try {
            handler = new MonitorHandler();
            MonitorInitializer mi = new MonitorInitializer(handler, false);

            Bootstrap b = new Bootstrap();
            // @TODO newFixedThreadPool(2);
            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);

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

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

            // add listeners waiting to be added
            if (listeners.size() > 0) {
                for (MonitorListener ml : listeners)
                    handler.addListener(ml);
                listeners.clear();
            }
        } catch (Exception ex) {
            logger.debug("failed to initialize the heartbeat connection");
            // logger.error("failed to initialize the heartbeat connection",
            // ex);
        }
    }

    if (channel != null && channel.isDone() && channel.isSuccess())
        return channel.channel();
    else
        throw new RuntimeException("Not able to establish connection to server");
}

From source file:poke.monitor.JobBidMonitor.java

License:Apache License

/**
 * create connection to remote server//from w  ww  . j  a va2 s  .c om
 * 
 * @return
 */
public Channel connect() {
    // Start the connection attempt.
    //   while(true){
    if (channel == null) {
        try {
            //if(flag==1)
            //{
            handler = new MonitorHandler();
            MonitorInitializer mi = new MonitorInitializer(handler, false);

            Bootstrap b = new Bootstrap();
            // @TODO newFixedThreadPool(2);
            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);

            // Make the connection attempt.
            logger.info(host + ";" + port + "IN JobBid monitor");
            channel = b.connect(host, port).syncUninterruptibly();
            channel.awaitUninterruptibly(5000l);
            channel.channel().closeFuture().addListener(new MonitorClosedListener(this));
            //flag=0;
            //Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
            if (N == Integer.MAX_VALUE)
                N = 1;
            else
                N++;
            //}
            // add listeners waiting to be added
            if (listeners.size() > 0) {
                for (MonitorListener ml : listeners)
                    handler.addListener(ml);
                listeners.clear();
            }
        } catch (Exception ex) {
            logger.debug("failed to initialize the heartbeat connection");
            // logger.error("failed to initialize the heartbeat connection",
            // ex);
        }
    }

    if (channel != null && channel.isDone() && channel.isSuccess()) {

        logger.info("Channel is Created" + channel);
        return channel.channel();
    } else {
        logger.info("In Error from Establish Connection");
        throw new RuntimeException("Not able to establish connection to server");
    }

}

From source file:poke.monitor.LeaderMonitor.java

License:Apache License

/**
 * create connection to remote server//from   ww  w.j  av  a2  s  . c  om
 * 
 * @return
 */
public Channel connect() {
    // Start the connection attempt.
    //   while(true){
    if (channel == null) {
        try {

            Bootstrap b = new Bootstrap();
            // @TODO newFixedThreadPool(2);
            b.group(group).channel(NioSocketChannel.class).handler(new ServerInitializer(false));
            b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
            b.option(ChannelOption.TCP_NODELAY, true);
            b.option(ChannelOption.SO_KEEPALIVE, true);

            // Make the connection attempt.
            logger.info(host + ";" + port + "IN Leader monitor");
            channel = b.connect(host, port).syncUninterruptibly();
            channel.awaitUninterruptibly(5000l);

        } catch (Exception ex) {
            logger.debug("failed to initialize the heartbeat connection");
            // logger.error("failed to initialize the heartbeat connection",
            // ex);
        }
    }

    if (channel != null && channel.isDone() && channel.isSuccess()) {

        logger.info("Channel is Created" + channel);
        return channel.channel();
    } else {
        logger.info("In Error from Establish Connection");
        throw new RuntimeException("Not able to establish connection to server");
    }

}

From source file:poke.monitor.ResourceMonitor.java

License:Apache License

/**
 * create connection to remote server/*from  w  w  w.j  a v a2s  .  c o  m*/
 * 
 * @return
 */
public Channel connect() {
    // Start the connection attempt.
    if (channel == null) {
        try {

            Bootstrap b = new Bootstrap();
            // @TODO newFixedThreadPool(2);
            b.group(group).channel(NioSocketChannel.class).handler(new ServerInitializer(false));
            b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
            b.option(ChannelOption.TCP_NODELAY, true);
            b.option(ChannelOption.SO_KEEPALIVE, true);

            // Make the connection attempt.
            logger.info(host + ":" + port + "IN Resource monitor");
            channel = b.connect(host, port).syncUninterruptibly();
            channel.awaitUninterruptibly(5000l);

        } catch (Exception ex) {
            logger.debug("failed to initialize the heartbeat connection");

        }
    }

    if (channel != null && channel.isDone() && channel.isSuccess()) {

        logger.info("Channel is Created" + channel);
        return channel.channel();
    } else {
        logger.info("In Error from Establish Connection");
        throw new RuntimeException("Not able to establish connection to server");
    }

}

From source file:poke.server.comm.monitor.CommMonitor.java

License:Apache License

/**
 * create connection to remote server/*  w w w.ja v a  2s .c om*/
 * 
 * @return
 */
public Channel connect() {
    // Start the connection attempt.
    if (channel == null) {
        try {
            handler = new CommMonitorHandler();
            CommMonitorInitializer mi = new CommMonitorInitializer(handler, false);

            Bootstrap b = new Bootstrap();
            // @TODO newFixedThreadPool(2);
            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);

            // Make the connection attempt.
            System.out.println("Connecting to Host " + host + " Port " + port);
            channel = b.connect(host, port).syncUninterruptibly();
            channel.awaitUninterruptibly(5000l);
            channel.channel().closeFuture().addListener(new MonitorClosedListener(this));

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

            // add listeners waiting to be added
            if (listeners.size() > 0) {
                for (CommMonitorListener ml : listeners)
                    handler.addListener(ml);
                listeners.clear();
            }
        } catch (Exception ex) {
            if (logger.isDebugEnabled())
                logger.debug("HeartMonitor: failed to initialize the heartbeat connection", ex);
            // logger.error("failed to initialize the heartbeat connection",
            // ex);
        }
    }

    if (channel != null && channel.isDone() && channel.isSuccess())
        return channel.channel();
    else {
        System.out.println("Unable to establish connection");
        return null;
    }

    //throw new RuntimeException("Not able to establish connection to server");
}

From source file:poke.server.management.client.MgmtConnection.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 . j a  v a2  s  .  c  om*/
        handler = new MgmtHandler();
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(handler);
        b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
        b.option(ChannelOption.TCP_NODELAY, true);
        b.option(ChannelOption.SO_KEEPALIVE, true);

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

        // 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) {
        logger.error("failed to initialize the mgmt client connection", ex);

    }

    // start outbound message processor
    worker = new OutboundWorker(this);
    worker.start();
}

From source file:poke.server.management.managers.ElectionManager.java

License:Apache License

/**
 * create connection to remote server//from www .j av a  2  s.  c  o m
 * 
 * @return
 */
protected Channel connect(String host, int port) {
    // Start the connection attempt.
    if (channel == null) {
        try {
            handler = new ManagementHandler();
            ManagementInitializer mi = new ManagementInitializer(false);

            Bootstrap b = new Bootstrap();
            b.group(new NioEventLoopGroup()).channel(NioSocketChannel.class).handler(mi);
            b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
            b.option(ChannelOption.TCP_NODELAY, true);
            b.option(ChannelOption.SO_KEEPALIVE, true);

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

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

        } catch (Exception ex) {
            logger.debug("failed to initialize the heartbeat connection");
        }
    }

    if (channel != null && channel.isDone() && channel.isSuccess())
        return channel.channel();
    else
        throw new RuntimeException("Not able to establish connection to server");
}

From source file:poke.server.monitor.HeartMonitor.java

License:Apache License

/**
 * create connection to remote server/*from   w  w w . j a v  a2s  .  c om*/
 * 
 * @return
 */
protected Channel connect() {
    // Start the connection attempt.
    if (channel == null) {
        try {
            handler = new MonitorHandler();
            MonitorInitializer mi = new MonitorInitializer(handler, false);

            Bootstrap b = new Bootstrap();
            // @TODO newFixedThreadPool(2);
            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);

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

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

            // add listeners waiting to be added
            if (listeners.size() > 0) {
                for (MonitorListener ml : listeners)
                    handler.addListener(ml);
                listeners.clear();
            }
        } catch (Exception ex) {
            if (logger.isDebugEnabled())
                logger.debug("HeartMonitor: failed to initialize the heartbeat connection", ex);
            // logger.error("failed to initialize the heartbeat connection",
            // ex);
        }
    }

    if (channel != null && channel.isDone() && channel.isSuccess())
        return channel.channel();
    else
        throw new RuntimeException("Not able to establish connection to server");
}

From source file:poke.server.ServerCommConnection.java

License:Apache License

/**
 * abstraction of notification in the communication
 * //  www  . j  a  v  a  2s . c  o m
 * @param listener
 */
/*   public void addListener(CommListener listener) {
      // note: the handler should not be null as we create it on construction
        
      try {
 handler.addListener(listener);
      } catch (Exception e) {
 logger.error("failed to add listener", e);
      }
   } */

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

    group = new NioEventLoopGroup();
    try {
        //handler = new ServerHandler();
        ServerInitializer ci = new ServerInitializer(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 attempt.
        channel = b.connect(host, port).syncUninterruptibly();

        // 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) {
        logger.error("failed to initialize the client connection", ex);

    }

    // start outbound message processor
    worker = new OutboundWorker(this);
    worker.start();
}

From source file:qunar.tc.qmq.netty.client.AbstractNettyClient.java

License:Apache License

public synchronized void start(NettyClientConfig config) {
    if (started.get()) {
        return;/*w  w w  . j av a  2  s  .c  om*/
    }
    initHandler();
    Bootstrap bootstrap = new Bootstrap();
    eventLoopGroup = new NioEventLoopGroup(1, new DefaultThreadFactory(clientName + "-boss"));
    eventExecutors = new DefaultEventExecutorGroup(config.getClientWorkerThreads(),
            new DefaultThreadFactory(clientName + "-worker"));
    connectManager = new NettyConnectManageHandler(bootstrap, config.getConnectTimeoutMillis());
    bootstrap.group(this.eventLoopGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
            .option(ChannelOption.SO_KEEPALIVE, false)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeoutMillis())
            .option(ChannelOption.SO_SNDBUF, config.getClientSocketSndBufSize())
            .option(ChannelOption.SO_RCVBUF, config.getClientSocketRcvBufSize())
            .handler(newChannelInitializer(config, eventExecutors, connectManager));
    started.set(true);
}