How to check if Glassfish DAS is running programmatically even if it is deployed on local machine or remote machine?
Using Java6
I have found a way to check if DAS is up other than Linux script. With this way it does not matter if both my application and DAS are at same machine or each installed different machine.
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;
}
This is how you would normally check that glassfish is up and running:
But since you want to run it programmaticaly, what you can do is create an script and execute the script from the code with java.
try{
Runtime.getRuntime().exec("Path to my script");
}
catch(IOException e) {
System.out.println("exception");
}
If you are in linux, you could also create an script to grep for the glassfish process.
You can use socket
to check if your connection up or not :
i use this way this work with me:
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;
}
}
If the socket is created then the connection is up, else the connection is down.