• Post Reply Bookmark Topic Watch Topic
  • New Topic

check app server and databse server are up or down  RSS feed

Madhu Sudhana
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have webapplication which is built on struts

I have a standalone progrmme which has to check whether the app server(JBoss) as well as database server are running are not....

I am able to check app server is down or not

but How can I check the database server is running or not

this is the standalone app which I have

package com.jps.healthcheck;

import java.net.URL;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ConnectException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.jps.utils.ConfigReader;
import com.jps.utils.DbBean;
import com.jps.Mail.SendMail;

public class HealthChecker {

public static void main (String[] args) {

URL url;
HttpURLConnection httpUrlConnection;

InputStream inputStream = null;
DataInputStream dataInputStream;
String contents;

int switchParam=0;
String message="";
try {
url = new URL("http://localhost:8080/NewAplusStruts/HealthCheckAction.do");
httpUrlConnection = (HttpURLConnection)url.openConnection();
httpUrlConnection .connect();
System.out.println("httpUrlConnection .getResponseMessage():"+httpUrlConnection .getResponseMessage());
System.out.println("urlConnection.getResponseCode():"+httpUrlConnection .getResponseCode()+" HttpURLConnection.HTTP_OK:"+HttpURLConnection.HTTP_OK);
if (httpUrlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println(" Server is running succesfully.............");

inputStream = url.openStream();
dataInputStream = new DataInputStream(new BufferedInputStream(inputStream));
while ((contents = dataInputStream.readLine()) != null) {

case 200:
System.out.println("in the case 200............................");
case 500:
message="Internal Server Error";
default :
message="Internal Server Error";

} catch (MalformedURLException mue) {
System.out.println("Ouch - a MalformedURLException happened.");

catch(ConnectException ce){
System.out.println("Not able to connect to the Application server...........");
message=" Server is not running";
catch (IOException ioe) {
System.out.println("Oops- an IOException happened.");
message="Either the Application Server or Database server is not running";

public static void sendMail(String message){
ConfigReader configReader=new ConfigReader();
String From=configReader.getpropertyinfo("from");
String To=configReader.getpropertyinfo("to");
String Subject=configReader.getpropertyinfo("subject");
String Message=message;
//String Message=configReader.getpropertyinfo("message");

System.out.println("Mail sent");

// end of main
} // end of class definition
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the database just connect, do a simple query and disconnect.

I have a little site checker like this with a few other features you are free to borrow. It reports errors for: the page comes back but takes longer than a max time, the page contains any of a set of "must not find" strings, the page does not contain any of a set of "must find" strings.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!