不多说了 直接上代码 有问题的 欢迎评论交流
package com.pzx.tomcat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @describe:TomcatMonitor.java
*
* @date 2018/02/01
* @author pzx
*/
public class TomcatMonitor implements Runnable {
// private static final Logger logger = LoggerFactory.getLogger(TomcatMonitor.class.getName());
String start = ""; // 系统命令 启动
String stop = ""; // 系统命令 关闭
String killJava = ""; // 强制关闭java的命令
String testHttp = ""; // 测试连接地址
int testIntervalTime = 1;// 测试连接间隔时间,单位为秒
int waitIntervalTime = 2; // 等待测试间隔时间,单位为秒
int testTotalCount = 5; // 测试连接总次数
Thread thread = null;
public TomcatMonitor() {
InputStream in = TomcatMonitor.class.getResourceAsStream("config.properties");
Properties p = new Properties();
try {
p.load(in);
stop = p.getProperty("stop");
start = p.getProperty("start");
killJava=p.getProperty("killJava");
testHttp = p.getProperty("testHttp");
testIntervalTime = Integer.parseInt(p.getProperty("testIntervalTime"));
waitIntervalTime = Integer.parseInt(p.getProperty("waitIntervalTime"));
testTotalCount = Integer.parseInt(p.getProperty("testTotalCount"));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("*******************初始化成功!*******************");
thread = new Thread(this);
thread.start();
}
public void run() {
System.out.println("正在监控中...");
// logger.debug("正在监控中...");
int testCount = 0;
while (true) {
testCount = 0;
testCount++;
boolean isrun = test();
System.out.println("正在启动测试连接,尝试连接次数为:" + testCount + ",结果为:" + (isrun == false ? "失败." : "成功!")+"监控时间为:"+new Date());
while (!isrun) {
if (testCount >= testTotalCount)
break;
try {
thread.sleep(testIntervalTime * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
testCount++;
System.out.println("正在启动测试连接,尝试连接次数为:" + testCount + ",结果为:" + (isrun == false ? "失败." : "成功!")+"监控时间为:"+new Date());
isrun = test();
}
if (!isrun) {
try {
System.out.println("测试连接失败,正在重启tomcat");
System.out.println("开始停止tomcat");
// 关闭tomcat服务
Process proc = Runtime.getRuntime().exec(stop);
// // 关闭后线程沉睡
thread.sleep(20000);
//强制关闭java.exe(正式环境关闭tomcat很长时间黑窗口才关闭 强制杀掉)
try {
System.out.println("开始强制杀进程java.exe");
Process procJava = Runtime.getRuntime().exec(killJava);
//强制杀线程后沉睡
thread.sleep(5000);
} catch (Exception e) {
//强制杀线程有异常不处理
System.out.println("强制杀进程java.exe异常");
}
System.out.println("开始启动tomcat");
// 启动tomcat服务
Process p = Runtime.getRuntime().exec(start);
System.out.println("重启tomcat成功");
thread.sleep(30000);
//杀掉每次执行命令的黑窗口,默认是不关掉的 不杀的话 会开无限个cmd窗口
killProcess();
} catch (Exception e) {
e.printStackTrace();
System.out.println("重启tomcat异常,请查看先关错误信息。。。。。");
}
}
try {
thread.sleep(waitIntervalTime * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
isrun = test();
}
}
public boolean test() {
URL url = null;
try {
url = new URL(testHttp);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
URLConnection urlConn = url.openConnection();
urlConn.setReadTimeout(10000);
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); // 实例化输入流,并获取网页代码
String s;
while ((s = reader.readLine()) != null) {
return true;
}
} catch (Exception e) {
return false;
}
return false;
}
public static void main(String[] args) throws Exception {
TomcatMonitor tm = new TomcatMonitor();
}
//关闭cmd窗口
public static void killProcess() {
Runtime rt = Runtime.getRuntime();
Process p = null;
try {
rt.exec("cmd.exe /C start wmic process where name='cmd.exe' call terminate");
} catch (IOException e) {
e.printStackTrace();
}
}
}
配置文件如下:
#系统命令 启动
start=cmd /c start D:\\tomcat-standard\\bin\\startup.bat
#start=cmd /c start D:\\mh_qianyi\\tomcat8-minhang\\tomcat8-minhang\\bin\\startup.bat
#关闭
#stop=cmd /c start D:\\mh_qianyi\\tomcat8-minhang\\tomcat8-minhang\\bin\\shutdown.bat
stop=cmd /c start D:\\tomcat-standard\\bin\\shutdown.bat
#强制关闭java的命令
killJava=cmd /c start taskkill /f /t /IM java.exe2
#测试连接总次数
testTotalCount=2
#连接失败时,再次检测时间间隔,单位为秒
testIntervalTime=5
#连接超时时间,即多少秒tomcat没响应视为宕机,单位为秒
connectionTimeout=15
#tomcat启动时间,防止在tomcat尚未启动完成的时候,程序又去检验tomcat状态,造成尚未启动完成又重新启动,单位为秒
tomcatStartTime=600
#测试连接地址
testHttp=http://localhost/mhmscp/login.jsp
#正常情况下,每次检测间隔时间,单位为秒
waitIntervalTime=120