手写tomcat宕机自动重启

#TOMCAT启动路径
startup.bat=D:\\dev-soft\\apache-tomcat-8.5.28\\bin\\startup.bat
#TOMCAT关闭路径
shutdown.bat=D:\\dev-soft\\apache-tomcat-8.5.28\\bin\\shutdown.bat
#测试连接总次数
testTotalCount=3
#测试连接间隔时间,单位为秒
testIntervalTime=3

testHttp=http://127.0.0.1:8080

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * ProjectLaunchUtil
 *
 * @author weixiang.wu
 * @date 2018 -05-04 17:08
 */
public class TomcatMonitorUtil {
	private static String startupBat = "";
	private static String shutdownBat = "";
	private static String testHttp = "";
	private static int testIntervalTime = 1;
	private static int testTotalCount = 5;

	static {
		InputStream in = TomcatMonitorUtil.class.getResourceAsStream("config.properties");
		Properties p = new Properties();
		try {
			p.load(in);
			startupBat = p.getProperty("startup.bat");
			shutdownBat = p.getProperty("shutdown.bat");
			testHttp = p.getProperty("testHttp");
			testIntervalTime = Integer.parseInt(p.getProperty("testIntervalTime"));
			testTotalCount = Integer.parseInt(p.getProperty("testTotalCount"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private TomcatMonitorUtil() {
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
				fixedThreadPool.execute(new Runnable() {

					@Override
					public void run() {
						System.out.println("正在监控中...");
						while (true) {
							int testCount = 0;
							// 测试tomcat是否需要重新启动
							boolean isRun = test();
							while (!isRun) {
								testCount++;
								System.out.println("尝试连接第 [" + testCount + "] 次失败");
								if (testCount >= testTotalCount) {
									break;
								}
								isRun = test();
							}

							if (!isRun) {
								try {
									//关闭tomcat服务
									Runtime.getRuntime().exec(shutdownBat);
									System.out.println("关闭tomcat服务,稍等5秒钟");
									Thread.sleep(5000);
									//启动tomcat服务
									System.out.println("测试连接失败,正在重启tomcat");
									Process process = Runtime.getRuntime().exec(startupBat);
									final InputStream in = process.getInputStream();
									BufferedReader br = new BufferedReader(new InputStreamReader(in));
									StringBuilder buf = new StringBuilder();
									String line = null;
									while ((line = br.readLine()) != null) {
										buf.append(line);
									}
									System.out.println("输出结果为:" + buf);
									System.out.println("重启tomcat成功");
								} catch (Exception e) {
									e.printStackTrace();
									System.out.println("重启tomcat异常,请查看错误信息。。。。。");

								}
							}

							try {
								System.out.println("连接状态正常");
								Thread.sleep(testIntervalTime * 1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				});
			}
			// 执行任务前的延迟时间    执行周期
		}, 1000, Integer.MAX_VALUE);
	}

	private boolean test() {
		URL url = null;
		try {
			url = new URL(testHttp);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			assert url != null;
			URLConnection urlConn = url.openConnection();
			urlConn.setReadTimeout(15000);
			//实例化输入流,并获取网页代码
			BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
			String s;
			while ((s = reader.readLine()) != null) {
				return true;
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	/**
	 * The entry point of application.
	 *
	 * @param args the input arguments
	 */
	public static void main(String[] args) {
		/* 通过args搞事 */
		doSomthing(args);

		/* TOMCAT启动监控 */
		new TomcatMonitorUtil();
	}

	private static void doSomthing(String[] args) {
		try {
			switch (args[0]) {
				/* 关闭tomcat的参数 */
				case "close":
					Runtime.getRuntime().exec(shutdownBat);
					break;
				default:
					break;
			}
		} catch (Exception e) {
			System.err.println("您可以通过发送参数来进行某些操作");
		}
	}
}

猜你喜欢

转载自my.oschina.net/wuweixiang/blog/1807029