windows下的tomcat监控器

最近写了一下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);

	}
 

  这里还需要一个资源文件(txt格式),从这个文件里读取执行命令,如:停止执行、开始执行

  读取文件的主要代码
    /**
	 * 读取文本文件内容
	 * @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;
	}
	
 
这是一个web应用,大家可以把附件下下来看看。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值