import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.net.URL; import java.net.URLConnection; import java.util.Date; public class Detector { private static void keepTomcatAlive() throws Exception { String s; String t = "Tomcat7.exe"; boolean isTomcatAlive = false; BufferedReader in; try { URL url = new URL(PropertiesUtil.getProps("url")); URLConnection con = url.openConnection(); in = new BufferedReader(new InputStreamReader(con.getInputStream())); con.setConnectTimeout(1000); con.setReadTimeout(4000); while ((s = in.readLine()) != null) { if (s.length() > 0) { System.out.println("<" + new Date() + "> Tomcat is alive..."); writeConsoleToFile("<" + new Date() + "> Tomcat is alive..."); return; } } in.close(); } catch (Exception e) { e.printStackTrace(); writeConsoleToFile(e.toString()); } try { Process p = Runtime.getRuntime().exec("tasklist"); in = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((s = in.readLine()) != null) { if (s.startsWith(t)) { isTomcatAlive = true; break; } } in.close(); } catch (Exception e) { e.printStackTrace(); writeConsoleToFile(e.toString()); } if (isTomcatAlive) { System.out.println("<" + new Date() + "> Tomcat is alive but not response!"); writeConsoleToFile("<" + new Date() + "> Tomcat is alive but not response!"); stopTomcat(); } startTomcat(); } public static void stopTomcat() { try { Process p = Runtime.getRuntime().exec("net stop Tomcat7"); BufferedReader in = new BufferedReader(new InputStreamReader( p.getInputStream())); String s; String t = "成功停止"; boolean restart = false; while ((s = in.readLine()) != null) { if (s.indexOf(t) != -1) { restart = true; break; } System.out.println(s); writeConsoleToFile(s); } System.out.println("<" + new Date() + "> Tomcat is stop " + (restart ? "OK" : "ERROR")); writeConsoleToFile("<" + new Date() + "> Tomcat is stop " + (restart ? "OK" : "ERROR")); } catch (Exception e) { e.printStackTrace(); try { writeConsoleToFile(e.toString()); } catch (Exception e1) { e1.printStackTrace(); } } } public static void startTomcat() { try { Process p = Runtime.getRuntime().exec("net start Tomcat7"); BufferedReader in = new BufferedReader(new InputStreamReader( p.getInputStream())); String s; String t = "启动成功"; boolean restart = false; while ((s = in.readLine()) != null) { if (s.indexOf(t) != -1) { restart = true; break; } System.out.println(s); writeConsoleToFile(s); } System.out.println("<" + new Date() + "> Tomcat is start " + (restart ? "OK" : "ERROR")); writeConsoleToFile("<" + new Date() + "> Tomcat is start " + (restart ? "OK" : "ERROR")); } catch (Exception e) { e.printStackTrace(); try { writeConsoleToFile(e.toString()); } catch (Exception e1) { e1.printStackTrace(); } } } public static void writeConsoleToFile(String newStr) throws Exception { File filename = new File(PropertiesUtil.getProps("file")); if (!filename.exists()) { filename.createNewFile(); } String readStr = ""; String read; FileReader fileread = new FileReader(filename); BufferedReader bufread = new BufferedReader(fileread); while ((read = bufread.readLine()) != null) { readStr = readStr + read + "\r\n"; } String filein = newStr + "\r\n" + readStr + "\r\n"; RandomAccessFile mm = new RandomAccessFile(filename, "rw"); mm.writeBytes(filein); if (bufread != null) { bufread.close(); } if (mm != null) { mm.close(); } } public static void main(String[] args) { while (true) { try { Detector.keepTomcatAlive(); Thread.sleep(30000); } catch (Exception e) { e.printStackTrace(); } } } }
最近下载更多
wx19941125 LV12
2019年8月19日
123456dian LV6
2019年7月30日
安安an LV17
2019年4月3日
oldfox LV18
2018年10月10日
yr940115 LV12
2018年9月10日
故事_sun LV26
2018年9月5日
bosimao1988512 LV3
2018年7月4日
shenter LV2
2018年6月1日
jic499 LV27
2018年4月18日
shanshilong LV19
2018年4月12日