Example usage for org.apache.hadoop.io IOUtils closeSocket

List of usage examples for org.apache.hadoop.io IOUtils closeSocket

Introduction

In this page you can find the example usage for org.apache.hadoop.io IOUtils closeSocket.

Prototype

public static void closeSocket(Socket sock) 

Source Link

Document

Closes the socket ignoring IOException

Usage

From source file:org.apache.slider.core.zk.MiniZooKeeperCluster.java

License:Apache License

private static boolean waitForServerDown(int port, long timeout) throws InterruptedException {
    long start = System.currentTimeMillis();
    while (true) {
        try {//from   w  w  w  .j  a  v  a 2s.c  o  m
            Socket sock = null;
            try {
                sock = new Socket("localhost", port);
                OutputStream outstream = sock.getOutputStream();
                outstream.write("stat".getBytes());
                outstream.flush();
            } finally {
                IOUtils.closeSocket(sock);
            }
        } catch (IOException e) {
            return true;
        }

        if (System.currentTimeMillis() > start + timeout) {
            break;
        }
        Thread.sleep(250);
    }
    return false;
}

From source file:org.apache.slider.core.zk.MiniZooKeeperCluster.java

License:Apache License

private static boolean waitForServerUp(int port, long timeout) throws InterruptedException {
    long start = System.currentTimeMillis();
    while (true) {
        try {//from  w w w  .j av a2s. c  om
            Socket sock = null;
            sock = new Socket("localhost", port);
            BufferedReader reader = null;
            try {
                OutputStream outstream = sock.getOutputStream();
                outstream.write("stat".getBytes());
                outstream.flush();

                Reader isr = new InputStreamReader(sock.getInputStream());
                reader = new BufferedReader(isr);
                String line = reader.readLine();
                if (line != null && line.startsWith("Zookeeper version:")) {
                    return true;
                }
            } finally {
                IOUtils.closeSocket(sock);
                IOUtils.closeStream(reader);
            }
        } catch (IOException e) {
            // ignore as this is expected
            LOG.debug("server localhost:" + port + " not up " + e);
        }

        if (System.currentTimeMillis() > start + timeout) {
            break;
        }
        Thread.sleep(250);
    }
    return false;
}