Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

check app server and databse server are up or down

 
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);
switchParam=httpUrlConnection.getResponseCode();
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) {
System.out.println(contents);
}

switch(switchParam){
case 200:
System.out.println("in the case 200............................");
break;
case 500:
message="Internal Server Error";
sendMail(message);
break;
default :
message="Internal Server Error";
sendMail(message);
}
//

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

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

}
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("From:"+From);
System.out.println("To:"+To);
System.out.println("Subject:"+Subject);
System.out.println("Message:"+Message);

SendMail.sendMailHealthCheck(From,To,Subject,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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic