JAVA 代码实现监控Tomcat

在实际项目中经常需要监听tomcat是不是还存活着。。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
 
public class TomcatMonitor implements Runnable{
    String procName="tomcat6.exe"; //进程名称
    //String fullProcName="C:\\Tomcat6\\bin\\tomcat6.exe";
    String fullProcName=""; //tomcat6.exe绝对物理地址
    //String command="cmd.exe /c start";
    String command=""; //系统命令
    //String testHttp="http://127.0.0.1:8080";
    String testHttp="";  //测试连接地址
    int testIntervalTime=1;//测试连接间隔时间,单位为秒
    int waitIntervalTime=2; //等待测试间隔时间,单位为秒
    int testTotalCount=5; //测试连接总次数
     
    Thread thread=null;
     
    public TomcatMonitor(){
        InputStream in = TomcatMonitor.class.getResourceAsStream("config.properties");
        Properties p = new Properties();
        try {
            p.load(in);
            procName=p.getProperty("procName");
            fullProcName=p.getProperty("fullProcName");
            command=p.getProperty("command");
            testHttp=p.getProperty("testHttp");
            testIntervalTime=Integer.parseInt(p.getProperty("testIntervalTime"));
            waitIntervalTime=Integer.parseInt(p.getProperty("waitIntervalTime"));
            testTotalCount=Integer.parseInt(p.getProperty("testTotalCount"));
             
            System.out.println("初始化成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
         
        thread=new Thread(this);
        thread.start();     
    }
     
    public void run() {
        String[] cmd = {"tasklist"};
        System.out.println("正在监控中...");   
        int testCount=0;
        while(true){
            testCount=0;
            testCount++;            
            boolean isrun=test();
            System.out.println("正在启动测试连接,尝试连接次数为:"+testCount+",结果为:"+(isrun==false?"失败.":"成功!"));               
            while(!isrun){
                if(testCount>=testTotalCount)break;
                try {
                    thread.sleep(testIntervalTime*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                testCount++;
                System.out.println("正在启动测试连接,尝试连接次数为:"+testCount+",结果为:"+(isrun==false?"失败.":"成功!"));               
                isrun=test();
            }
             
            if(!isrun){
                System.out.println("测试连接失败,正在重启tomcat");
                try{                    
                    Process proc = Runtime.getRuntime().exec(cmd);
                    BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));                        
                    String string_Temp = in.readLine();
                    while (string_Temp != null)
                    {
                        if(string_Temp.toLowerCase().indexOf(procName)!=-1){
                            Runtime.getRuntime().exec("Taskkill /f /im "+procName);
                            break;
                        }
                        string_Temp = in.readLine();
                    }
                    thread.sleep(5000);
 
                    Process p=Runtime.getRuntime().exec(command+" \"\" \""+fullProcName+"\""); 
                    //Process p=Runtime.getRuntime().exec("net start tomcat6"); 
                    System.out.println("重启tomcat成功");
                }catch(Exception e){
                    e.printStackTrace();
                }               
            }
             
            try {
                thread.sleep(waitIntervalTime*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
             
            isrun=test();
        }       
    }
     
    public boolean test(){
         
        URL url=null;       
        try {
            url = new URL(testHttp);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            URLConnection urlConn=url.openConnection();
            urlConn.setReadTimeout(15000);
            BufferedReader reader = new BufferedReader(new InputStreamReader( urlConn.getInputStream()));            //实例化输入流,并获取网页代码
                   String s;                                       
                   while ((s = reader.readLine()) != null) {
                      return true;   
                   }                        
        } catch (Exception e) {
          return false;
        }
        return false;
    }
     
     
    public static void main(String[] args) throws Exception{
        TomcatMonitor tm=new TomcatMonitor();
    }
     
 
}

start.bat
    java -cp . TomcatMonitor

#进程名称
procName=tomcat6.exe
 
#tomcat6.exe绝对物理地址
fullProcName=D:\\tomcat7\\portalTomcat\\bin\\tomcat7.exe
 
 
#系统命令
command=cmd.exe /c start
 
#测试连接总次数
testTotalCount=3
 
#连接失败时,再次检测时间间隔,单位为秒
testIntervalTime=3
 
#连接超时时间,即多少秒tomcat没响应视为宕机,单位为秒
connectionTimeout=15
 
#tomcat启动时间,防止在tomcat尚未启动完成的时候,程序又去检验tomcat状态,造成尚未启动完成又重新启动,单位为秒
tomcatStartTime=600
 
#测试连接地址
testHttp=http://127.0.0.1:7080/stargate/login.jsp
 
#正常情况下,每次检测间隔时间,单位为秒
waitIntervalTime=30

未经允许不得转载:小贱贱吧 | 分享身边的福利吧 » JAVA 代码实现监控Tomcat

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址