记录、分享

检测Tomcat运行状态,自动重启

检测Tomcat运行状态,自动重启

http://blog.csdn.net/huangjl2000w/article/details/6338997

先是主程序:


import java.io.BufferedReader;
import java.io.FileReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

public class CheckTomcat {
 private static String tomcatroot="";
 private static String monitorurl="";
 private static void checkTomcatIsAlive(String myurl) throws NullPointerException {
  String s;
  boolean isTomcatAlive = false;
  java.io.BufferedReader in;
  try {
   System.out.println(">>>>>>检测URL:"+myurl);
   URL url = new URL(myurl);
   URLConnection con = url.openConnection();
   in = new java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream()));
   con.setConnectTimeout(1000);
   con.setReadTimeout(4000);
   while ((s = in.readLine()) != null) {
    if (s.length() > 0) {// 如果能够读取到页面则证明可用,tomcat正常,否则继续后面的重启tomcat操作。
     return;
     }
    }
   in.close();
  }catch (Exception ex) {
   //ex.printStackTrace();
   System.out.println("*************该URL有误或不可访问!");
  }
  
  /*if (isTomcatAlive) {
   System.out.println("<" + new Date()+ "> Tomcat is alive but not response!");
   stopTomcat();
  }*/
  RunTomcat runt=new RunTomcat();
  runt.startTomcat(tomcatroot);
 }
 
 /*public static void stopTomcat() {
  try {
   //java.lang.Process p = java.lang.Runtime.getRuntime().exec("net stop /"Apache Tomcat/"");
   java.lang.Process p = java.lang.Runtime.getRuntime().exec(tomcatroot+"bin//shutdown.bat");
   java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
   String s;
   String t = "Using JRE_HOME";
   boolean restart = false;
   while ((s = in.readLine()) != null) {
    if (s.indexOf(t) != -1) {
     restart = true;
     break;
    }
   }
   System.out.println("<" + new Date() + "> Tomcat is stop "+ (restart ? "OK" : "ERROR"));
  } catch (Exception e) {
   e.printStackTrace();
  }
 }*/
 
 public static void main(String[] args) {
  System.out.println("********************************************************");
  System.out.println("====本程序自动检测Tomcat运行状况,必要时自动重启Tomcat。====");
  System.out.println("********************************************************");
  
  init_config();
  if(monitorurl.equals(""))monitorurl="http://localhost:8080/ExchangeWeb/checkTomcat/monitor.jsp";
  if(tomcatroot.equals(""))tomcatroot="F://tomcat-6.0.20//";
  if(!tomcatroot.endsWith("//"))tomcatroot+="//";
  while (true) {
   try {
    String random="?random="+Math.random() * 65535;//=====处理数据缓存问题======
    CheckTomcat.checkTomcatIsAlive(monitorurl+random);
    Thread.sleep(5000);
    System.out.println("========================checking at <"+new Date()+">");
   } catch (Exception ex) {
    ex.printStackTrace();
   }
  }
 }
 
 static private void init_config() {
  try{
   CheckTomcat me=new CheckTomcat();
   String maindir=me.getClass().getResource("/").toURI().getPath();
   System.out.println(">>>>>>配置文件目录:"+maindir);
   String sLine;
   String filename=maindir+"config.xml";
   BufferedReader buffReader = new BufferedReader(new FileReader(filename));
   while((sLine = buffReader.readLine())!=null)
   { 
    sLine = sLine.trim();
    if(sLine.trim()!="" && !sLine.equals("")){
     if(sLine.toLowerCase().startsWith("tomcatroot")){
      int npos=sLine.indexOf("tomcatroot");
      npos+="tomcatroot".length();
      tomcatroot=sLine.substring(npos).trim();
      if(tomcatroot.startsWith("="))tomcatroot=tomcatroot.substring(1);
     }
     else if(sLine.toLowerCase().startsWith("monitorurl")){
      int npos=sLine.indexOf("monitorurl");
      npos+="monitorurl".length();
      monitorurl=sLine.substring(npos).trim();
      if(monitorurl.startsWith("="))monitorurl=monitorurl.substring(1);
     }
    }
   }
   buffReader=null;
  }catch(Exception e){
   e.printStackTrace();
   System.out.println("********************************************************");
   System.out.println("====读取配置文件失败!系统无法运行,请与供应商联系。====");
   System.out.println("********************************************************");
   System.exit(0);
  }
 }
}

 

再是自动重启Tomcat线程类:

import java.util.Date;


public class RunTomcat extends Thread {

 private static String tomcatroot="";
 
 public void startTomcat(String root) {
  this.tomcatroot=root;
  
  System.out.println(">>>>>>Tomcat即将启动。。。");
  System.out.println(">>>>>>Tomcat根目录:"+tomcatroot);
  try {
   //java.lang.Process p = java.lang.Runtime.getRuntime().exec("net stop /"Apache Tomcat/"");
   java.lang.Process p = java.lang.Runtime.getRuntime().exec(tomcatroot+"bin//shutdown.bat");
  } catch (Exception e) {
    e.printStackTrace();
  }
  
  try {
   Thread.sleep(3000);//等待shutdown结束
   //RunTomcat me=new RunTomcat();
   //String maindir=me.getClass().getResource("/").toURI().getPath();
   //java.lang.Process p = java.lang.Runtime.getRuntime().exec(maindir+"checkTomcat.bat");
   java.lang.Process p = java.lang.Runtime.getRuntime().exec(tomcatroot+"bin//startup.bat");
   java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
   
   /*String s;
   boolean restart = false;
   String t = "Server startup in";
   while ((s = in.readLine()) != null) {
    System.out.println(s);
    if (s.indexOf(t) != -1) {
     restart = true;
     break;
    }
   }*/
   System.out.println(">>>>>>Tomcat start at <" + new Date() + ">");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

}

接着是检测tomcat是否活动monitor.jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.println("path=" + path + "<br>");
out.println("basePath=" + basePath + "<br>");
out.println("ok<br>");
%>

 

然后是config.xml配置文件:

tomcatroot=F:/tomcat-6.0.20/
monitorurl=http://localhost:8080/ExchangeWeb/checkTomcat/monitor.jsp

 

最后是checkTomcat.bat批处理文件:

@echo off

rem=========第一步:配置下面的JAVA_HOME为JDK目录==========#
@set JAVA_HOME=C:/Program Files/Java/jdk1.6.0_14

rem=========第二步:配置下面的CATALINA_HOME为Tomcat目录==========#
@set CATALINA_HOME=F:/tomcat-6.0.20

@set PATH=%JAVA_HOME%/bin/;
@set CLASSPATH=%JAVA_HOME%/lib/rt.jar;%JAVA_HOME%/lib/tools.jar;%CATALINA_HOME%/lib/servlet-api.jar;%CATALINA_HOME%/lib/jsp-api.jar;


java CheckTomcat

 

测试时,只要双击执行checkTomcat.bat文件即可。

posted on 2012-05-24 10:44 张生 阅读(8857) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2012年5月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜