我正在开发一个应用程序,它会定期ping多个网址(服务器),以确保服务器运行正常或是否正常运行。我已经完成了编码部分(java程序)来实现这一点。
以下是代码:
public class PingServers {
public static boolean pingUrl(String address) {
try {
final URL url = new URL("http://" + address);
final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(1000 * 100); // Timeout is in milliseconds
final long startTime = System.currentTimeMillis();
urlConn.connect();
final long endTime = System.currentTimeMillis();
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Time (ms) : " + (endTime - startTime));
System.out.println("Ping to " + address + " was success");
return true;
}
} catch (final MalformedURLException e1) {
System.out.println("inside MalformedURLException");
e1.printStackTrace();
} catch (final Exception e) {
System.out.println("inside Exception");
e.printStackTrace();
System.out.println(address + " is either going down or is an unknownhost.");
}
return false;
}
public static void main(String args[]) throws InterruptedException {
//for periodic execution
for (int i = 0; i < 5; i++) {
System.out.println("Execution in Main Thread...." + i);
Date now = new Date(); // initialize date
System.out.println("Time is :" + now); // Display current time
String url1 = "www.google.com";
String url2 = "www.yahoo.com";
String url3 = "www.invalidurl.com";
pingUrl(url1);
pingUrl(url2);
pingUrl(url3);
Thread.sleep(10000);
if (i == 5) {
System.out.println("Application Terminates");
System.exit(0);
}
}
}
}
它在cmd(控制台)中运行良好。但我需要将其形成为一个将自动在任务栏中运行的应用程序,一旦条件匹配(服务器关闭或未知主机),它将显示弹出窗口。
有人请告诉我从这里开始实现这一目标的最佳方法(一步一步)。
非常值得赞赏。