2012-12-13 163 views

回答

1

我找到了一种方法来检查DAS是否启动,而不是Linux脚本。 通过这种方式,如果我的应用程序和DAS都在同一台机器上或每台机器都安装在同一台计算机上,则无关紧要。

public static boolean isUrlReachable(String host) { 
    String URLName="http://"+host+":4848"; 
    boolean isUp = false; 
    try { 
    HttpURLConnection.setFollowRedirects(false); 
    HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection(); 
    con.setRequestMethod("GET"); 
    isUp = (con.getResponseCode() == HttpURLConnection.HTTP_OK); 
    con.disconnect(); 
    } 
    catch (Exception e) { 
    return isUp; 
    } 

    return isUp; 

}

0

这就是你通常会检查GlassFish是启动并运行:

enter image description here

但既然要programmaticaly运行它,你可以做的就是创建一个脚本并用java代码执行脚本。

try{ 
Runtime.getRuntime().exec("Path to my script"); 
} 
catch(IOException e) { 
System.out.println("exception"); 
} 

如果你在linux中,你也可以创建一个脚本来grep for glassfish进程。

1

您可以使用socket检查,如果你的连接,或根本没有:

我用这种方式跟我这个工作:

private boolean checkConnection(String host, int port) { 
    try { 
     Socket socket = new Socket(host, port); 
     socket.close(); 
     return true; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 

如果创建套接字那么连接就会结束,否则连接就会断开。

相关问题