Java : Ping Server Status

The problem described in this post is a common use case in any web related program. There are several instances when you need to check if a server is up and running or not. Programming language provide their API’s to accomplish this task. Java is no exception. It has a suite of classes related to socket programming. We faced a similar situation with Allzhere when we wanted to know whether our site was up and running or not. However, the requirement here was specific and so was the solution.

Java_Ping_Server_Status

Problem Statement:

  1. Ping http://allzhere.in for its server status.
  2. Schedule this task to run every 30 minutes (Configurable).
  3. Configure maximum attempts for server status check. This is a specific requirement which comes in handy if the server is hosted externally and has a shared bandwidth. In this case, the server is unreachable at one moment but is reached few seconds later. Hence, we cannot conclude that the server is not running. So, we ping server for a maximum attempts before confirming server problem.
  4. Take appropriate notification action if problem found with server response.

Solution Description:

  1. Create a Thread (T) to run every 30 minutes (Configurable).
  2. When this thread executes, it creates an interim thread (I).
  3. This thread pings server for status.
  4. However, when thread “I” runs, it will sleep after 1 minute for a total of 5 times (All variables configurable).
  5. Each time of its execution; it will ping the server i.e. http://allzhere.in for status.
  6. Now, if at the end of execution of thread “I” over a period of 5 minutes, we see that the server did not respond and threw “ConnectException”. Hence, we can conclude that there is something wrong.
  7. Program will then call notification code to send alert regarding the status.
  8. The maximum attempts variable is configurable.
  9. The sleep time for thread “I” is also configurable.

Code snippet for simple check is as follows. You can check the complete program files at the github link at the end of post:

private static int sendGet() throws Exception {
  URL obj = new URL(url);
  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  // optional default is GET
  con.setRequestMethod(requestMethod);
  System.out.println(con.getInputStream());
  int responseCode = con.getResponseCode();
  System.out.println("Response Code : " + responseCode);
  return responseCode;
}

In case you are behind a proxy server, which is mostly the case in corporate houses, you can set the proxy server to avoid “java.net.UnknownHostException”. Following is the code snippet for setting proxy:

System.setProperty("http.proxyHost", "yourproxy.com");
System.setProperty("http.proxyPort", "yourport"); // default port is 8080

Program contains a business logic that is processed from catch block. Though it’s not the best practice but was unavoidable here. Further, as far as I know, “ConnectException” is the only way to know if server is running or not.

Code snippets have been committed at the gihub repository link. Following are files associated:

  1. DialServer.java
  2. SleepCheck.java
  3. YouThere.java
  4. YouSleeping.java

Please provide your feedback.

@Facebook

Subscribe to Blog via Email

Quick Enquiry

Leave a Reply

Your email address will not be published. Required fields are marked *