Here you can find the source of waitForLiveServer(String webContainerHostname, int webContainerPort, int timeoutMin)
public static void waitForLiveServer(String webContainerHostname, int webContainerPort, int timeoutMin)
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.net.ConnectException; import java.net.InetSocketAddress; import java.net.Socket; public class Main { public static void waitForLiveServer(String webContainerHostname, int webContainerPort, int timeoutMin) { InetSocketAddress address = new InetSocketAddress(webContainerHostname, webContainerPort); System.out.println("Waiting " + timeoutMin + " min for web server..."); long beginTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis() + (timeoutMin * 60 * 1000); boolean portOpened = false; while ((!portOpened) && (System.currentTimeMillis() < endTime)) { portOpened = trySocket(address); if (portOpened) { System.out.println("Web server has opened the port in " + (System.currentTimeMillis() - beginTime) / 1000.0 / 60.0 + " min."); } else { try { Thread.sleep(2000); // 2 seconds } catch (InterruptedException e) { e.printStackTrace(); }//ww w . j a va 2 s . c o m } } if (!portOpened) { throw new RuntimeException("Timed out waiting for webapp server to initialize"); } } private static boolean trySocket(InetSocketAddress address) { boolean success = false; Socket socket = null; try { socket = new Socket(); socket.connect(address); success = true; } catch (ConnectException e) { // this is expected } catch (Throwable e) { e.printStackTrace(); } finally { if (socket != null && !socket.isClosed()) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } return success; } }