Java 每30分钟自动Ping一次外部服务器

Java 每30分钟自动Ping一次外部服务器,java,jsp,networking,network-programming,Java,Jsp,Networking,Network Programming,我在列表中有一组服务器。我想每30分钟ping一次这些服务器。我如何才能做到这一点。这是一个带有java代码的jsp程序。此代码将在每30分钟ping一次给定ip后执行 public boolean ping(String ip) throws IOException, InterruptedException extends TimerTask { public void run{ boolean isWindows = System.getProp

我在列表中有一组服务器。我想每30分钟ping一次这些服务器。我如何才能做到这一点。这是一个带有java代码的jsp程序。

此代码将在每30分钟ping一次给定ip后执行

    public boolean ping(String ip) throws IOException, InterruptedException  extends TimerTask {
     public void run{       
     boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");

            ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows? "-n" : "-c", "1", ip`enter code here`);
            Process proc = processBuilder.start();
            BufferedReader in =
        new BufferedReader(
        new InputStreamReader(proc.getInputStream()));
        while (true) {
        String line = in.readLine();
        if (line == null)
        break;
        if(line.contains("Destination host unreachable")||line.contains("Request timed out")){

            return false;
            }
        }

            return true;
}

        }
// And From your main() method or any other method
Timer timer = new Timer();
 timer.schedule(new ping(ip), 0, (60*30*1000));

除非您使用本机代码或外部进程,否则您无法执行真正的ping ie,即Java中的ICMP回显请求…使用线程我们不能这样做吗?这个问题似乎离题了,因为OP要求我们为他/她编写代码。我必须手动运行ping…o此代码将在30分钟后自动ping上述代码用于手动,要ping到服务器,但可能需要线程在30分钟后执行。如何使用线程编写代码嘿,伙计,只需阅读如何使用线程,就不可能给出完整的代码。这是不可能的,或者应用像Quartz这样的适当计时工具来为您处理线程。