JAVA實(shí)現(xiàn)監(jiān)測(cè)tomcat是否宕機(jī)及控制重啟的方法
本文實(shí)例講述了JAVA實(shí)現(xiàn)監(jiān)測(cè)tomcat是否宕機(jī)及控制重啟的方法。分享給大家供大家參考。具體如下:
Detector.java:
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();
}
}
}
}
希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。
相關(guān)文章
java抓取網(wǎng)頁(yè)數(shù)據(jù)獲取網(wǎng)頁(yè)中所有的鏈接實(shí)例分享
java抓取網(wǎng)頁(yè)數(shù)據(jù)獲取網(wǎng)頁(yè)中所有的鏈接實(shí)例分享,使用方法,只要實(shí)例化HtmlParser時(shí)傳入網(wǎng)頁(yè)地址就可以了2013-12-12
JAVA實(shí)現(xiàn)Date日期加一天具體方法
這篇文章主要給大家介紹了關(guān)于JAVA實(shí)現(xiàn)Date日期加一天的相關(guān)資料,因?yàn)樵陧?xiàng)目中遇到了需要將日期進(jìn)行加減一些天數(shù)的操作,文中給出了簡(jiǎn)單的代碼示例,需要的朋友可以參考下2023-07-07
SpringBoot實(shí)現(xiàn)文件上傳下載實(shí)時(shí)進(jìn)度條功能(附源碼)
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何實(shí)現(xiàn)文件上傳下載實(shí)時(shí)進(jìn)度條功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2022-10-10
Java對(duì)象以Hash結(jié)構(gòu)存入Redis詳解
這篇文章主要介紹了Java對(duì)象以Hash結(jié)構(gòu)存入Redis詳解,和Java中的對(duì)象非常相似,卻不能按照J(rèn)ava對(duì)象的結(jié)構(gòu)直接存儲(chǔ)進(jìn)Redis的hash中,因?yàn)镴ava對(duì)象中的field是可以嵌套的,而Redis的Hash結(jié)構(gòu)不支持嵌套結(jié)構(gòu),需要的朋友可以參考下2023-08-08

