Java HTTP Port Find waitForService(int port)

Here you can find the source of waitForService(int port)

Description

wait For Service

License

Open Source License

Declaration

public static boolean waitForService(int port) 

Method Source Code

//package com.java2s;
/*/*from www .jav  a 2  s  .co  m*/
 * QCRI, NADEEF LICENSE
 * NADEEF is an extensible, generalized and easy-to-deploy data cleaning platform built at QCRI.
 * NADEEF means "Clean" in Arabic
 *
 * Copyright (c) 2011-2013, Qatar Foundation for Education, Science and Community Development (on
 * behalf of Qatar Computing Research Institute) having its principle place of business in Doha,
 * Qatar with the registered address P.O box 5825 Doha, Qatar (hereinafter referred to as "QCRI")
 *
 * NADEEF has patent pending nevertheless the following is granted.
 * NADEEF is released under the terms of the MIT License, (http://opensource.org/licenses/MIT).
 */

import java.io.IOException;
import java.net.Socket;

public class Main {
    public static boolean waitForService(int port) {
        final int MAX_TRY_COUNT = 10;
        int tryCount = 0;
        while (true) {
            if (isPortOccupied(port)) {
                return true;
            }

            if (tryCount == MAX_TRY_COUNT) {
                System.err.println("Waiting for port " + port + " time out.");
                return false;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // ignore
            }
            tryCount++;
        }
    }

    public static boolean isPortOccupied(int port) {
        try (Socket ignored = new Socket("localhost", port)) {
            return true;
        } catch (IOException ignored) {
            return false;
        }
    }
}

Related

  1. waitForLiveServer(String webContainerHostname, int webContainerPort, int timeoutMin)
  2. waitForPort(String host, int port)
  3. waitForResponseCode(int code, String name, String host, int port)
  4. waitForRespose(String name, String host, int port)
  5. waitForServerDown(String host, int port, long timeout)
  6. writeContactFile(int port, String appName)