日期:2014-05-17 浏览次数:20759 次
最近写了一下windows下监控tomcat的小应用,主要功能就是当发现tomcat挂掉的时候就会去重启服务,可以省了很多事,但是在linux下还不行,等我研究通了再发上来吧,呵呵。。
下面是主要的代码块
? ? ? ? ??//线程
public void run() { //启动被监控程序 try { File file = new File(""); String path=file.getAbsolutePath().replaceAll("\\\\","//"); readTxt(path+"//bin//test.txt",""); if ("true".equals(flag.trim())) { URL url = new URL(myUrl); conn = (HttpURLConnection) url.openConnection(); conn.getResponseCode(); System.out.println("页面已经启动了......."); }else{ System.out.println("flag=false,关闭。。。。。。"); time.cancel();//取消监控 } } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { System.out.println("页面已经挂掉了......."); try { Runtime.getRuntime().exec(tomcatCloseUrl); System.out.println("tomcat关闭......."); Runtime.getRuntime().exec(tomcatOpenUrl); System.out.println("tomcat打开......."); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } //启动方法 public static void startThread(int time1, int time2) { time = new Timer(); ReadSourceAndRun w = new ReadSourceAndRun(); time.schedule(w, time1, time2); }?
* 读取文本文件内容 * @param filePathAndName 带有完整绝对路径的文件名 * @param encoding 文本文件打开的编码方式 * @return 返回文本文件的内容 */ public static String readTxt(String filePathAndName, String encoding) throws IOException { encoding = encoding.trim(); StringBuffer str = new StringBuffer(""); String st = ""; try { FileInputStream fs = new FileInputStream(filePathAndName); InputStreamReader isr; if (encoding.equals("")) { isr = new InputStreamReader(fs); } else { isr = new InputStreamReader(fs, encoding); } BufferedReader br = new BufferedReader(isr); try { String data = ""; while ((data = br.readLine()) != null) { str.append(data + " "); if(data.startsWith("url=")){ myUrl=data.substring(data.indexOf("http"));; } if(data.startsWith("tomcatCloseUrl=")){ tomcatCloseUrl=(data.substring(data.indexOf("tomcatUrl")+16)); } if(data.startsWith("tomcatOpenUrl=")){ tomcatOpenUrl=(data.substring(data.indexOf("tomcatOpenUrl")+14)); } if(data.startsWith("flag=")){ flag=data.substring(data.indexOf("flag")+5); } fs.close(); isr.close(); } } catch (Exception e) { str.append(e.toString()); } st = str.toString(); } catch (IOException es) { st = ""; } return st; }?
?