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.text.SimpleDateFormat; import java.util.Date; public class DetectorTow { public static void keepTomcatAlive(String port) throws Exception { String s; String t; URL url; if (port.equals("8080")) { t = "Tomcat7.exe"; url = new URL(PropertiesUtil.getProps("url.8080")); } else { t = "Tomcat7_1.exe"; url = new URL(PropertiesUtil.getProps("url.8081")); } boolean isTomcatAlive = false; BufferedReader in; try { 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..."); return; } } in.close(); } catch (Exception e) { e.printStackTrace(); writeConsoleToFile(e.toString(), port); } 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(), port); } if (isTomcatAlive) { System.out.println("<" + new Date() + "> Tomcat is alive but not response!"); writeConsoleToFile("<" + new Date() + "> Tomcat is alive but not response!", port); stopTomcat(port); } startTomcat(port); } public static void stopTomcat(String port) { Process p; try { if (port.equals("8080")) { p = Runtime.getRuntime().exec("net stop Tomcat7"); } else { p = Runtime.getRuntime().exec("net stop Tomcat7_1"); } 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, port); } System.out.println("<" + new Date() + "> Tomcat is stop " + (restart ? "OK" : "ERROR")); writeConsoleToFile("<" + new Date() + "> Tomcat is stop " + (restart ? "OK" : "ERROR"), port); } catch (Exception e) { e.printStackTrace(); try { writeConsoleToFile(e.toString(), port); } catch (Exception e1) { e1.printStackTrace(); } } } public static void startTomcat(String port) { Process p; try { if (port.equals("8080")) { p = Runtime.getRuntime().exec("net start Tomcat7"); } else { p = Runtime.getRuntime().exec("net start Tomcat7_1"); } 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, port); } System.out.println("<" + new Date() + "> Tomcat is start " + (restart ? "OK" : "ERROR")); writeConsoleToFile("<" + new Date() + "> Tomcat is start " + (restart ? "OK" : "ERROR"), port); } catch (Exception e) { e.printStackTrace(); try { writeConsoleToFile(e.toString(), port); } catch (Exception e1) { e1.printStackTrace(); } } } public static void writeConsoleToFile(String newStr, String port) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String file = PropertiesUtil.getProps("port.file"); String name = format.format(new Date()) + "-" + port + "-console-log"; File filename = new File(file + name); 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(); } } }

wx19941125 LV12
2019年8月19日
123456dian LV6
2019年7月30日
安安an LV17
2019年4月3日
oldfox LV19
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日