This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes check app server and databse server are up or down Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "check app server and databse server are up or down" Watch "check app server and databse server are up or down" New topic
Author

check app server and databse server are up or down

Madhu Sudhana
Ranch Hand

Joined: Apr 16, 2006
Posts: 127
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


"And the trouble is, if you don't risk anything, you risk even more." -- Erica Jong.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
Don't get me started about those stupid light bulbs.
 
subject: check app server and databse server are up or down