vue项目接口域名动态的获取方法
196
2023-07-29
JAVA实现监测tomcat是否宕机及控制重启的方法
本文实例讲述了java实现监测tomcat是否宕机及控制重启的方法。分享给大家供大家参考。具体如下:
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 -auxopuiYZ");
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)opuiYZ {
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("/ethttp://c/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();
}
}
}
}
希望本文所述对大家的java程序设计有所帮助。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~