JAVA监测tomcat是否死机,控制重启

import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
/**

* @author james
*
*/
public class Detector {
 
private static void keepTomcatAlive() throws NullPointerException {
String s;
String t = new String("tomcat6");
boolean isTomcatAlive = false;
java.io.BufferedReader in;
 
System.setProperty("sun.net.client.defaultConnectTimeout", "8000");   
System.setProperty("sun.net.client.defaultReadTimeout", "10000");  
 
try {
URL url = new URL("http://localhost:8080/test.jsp");
URLConnection con = url.openConnection();
in = new java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream()));
con.setConnectTimeout(1000);
con.setReadTimeout(4000);
while ((s = in.readLine()) != null) {
if (s.length() > 0) {
//accessed page successful.
return;
}
}
in.close();
} catch (Exception ex) {
          //ex.printStackTrace();
}
 
try {
java.lang.Process p = java.lang.Runtime.getRuntime().exec("ps -aux");
in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
while ((s = in.readLine()) != null) {
if (s.startsWith(t)) {
isTomcatAlive = true;
break;
}
}
in.close();
}catch (Exception e) {
e.printStackTrace();
}
 
if (isTomcatAlive) {
System.out.println("<" + new Date() + "> Tomcat is alive but not response!");
stopTomcat();
}
 
startTomcat();
}
 
public static void stopTomcat() {
try {
java.lang.Process p = java.lang.Runtime.getRuntime().exec("/etc/init.d/tomcat6 stop");
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
String s;
String t = "Stopping";
boolean restart = false;
while ((s = in.readLine()) != null) {
if (s.indexOf(t) != -1) {
restart = true;
break;
}
}
System.out.println("<" + new Date() + "> Tomcat is stop " + (restart ? "OK" : "ERROR"));
} catch (Exception e) {
  e.printStackTrace();
}
}
 
public static void startTomcat() {
try {
java.lang.Process p = java.lang.Runtime.getRuntime().exec("/etc/init.d/tomcat6 start");
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
String s;
String t = "Starting";
boolean restart = false;
while ((s = in.readLine()) != null) {
if (s.indexOf(t) != -1) {
restart = true;
break;
}
}
System.out.println("<" + new Date() + "> Tomcat is start " + (restart ? "OK" : "ERROR"));
} catch (Exception e) {
e.printStackTrace();
}
}
 
private static void debug(String msg){
System.out.println("Debug::: "+msg);
}
 
public static void main(String[] args) {
while (true) {
try {   
                                debug("Detect agin <"+new Date()+">");
Detector.keepTomcatAlive();
                                debug("Sleep...");
Thread.sleep(30000);
} catch (Exception ex) {
                           ex.printStackTrace();
}
}
}
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值