Monit tomcat run or not on Windows with Java

2014年1月21日 14:36

StreamGobbler.java
package cn.sotips.www;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
class StreamGobbler extends Thread
{
    InputStream is;
    String type;

    StreamGobbler(InputStream is, String type)
    {
        this.is = is;
        this.type = type;
    }

    public void run()
    {
        try
        {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
            {
                System.out.println(type + ">" + line);    
            }
        } catch (IOException ioe){
            ioe.printStackTrace();  
        }
    }
}
Monitor.java
package cn.sotips.www;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class Monitor {
    private static final String TOMCAT_PATH = getProperty("TOMCAT_PATH");
    private static final String START_PATH = getProperty("START_PATH");
    private static final String STOP_PATH = getProperty("STOP_PATH");
    private static final String REQUEST_URL = getProperty("REQUEST_URL");
    private static int SLEEP_SECOND = Integer.parseInt(getProperty("SLEEP_SECOND"));

    private static String getProperty(String key){
        InputStream inputstream = null;
        try {
            inputstream =  new FileInputStream(System.getProperty("user.dir")+File.separator+"config.properties");
            Properties properties = new Properties();
            properties.load(inputstream);//从输入流中读取属性文件的内容
            return properties.getProperty(key);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(inputstream!=null){
                try {
                    inputstream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "0";
    }

    //每隔1分钟扫描一遍,看jsp是否能访问,如果不能,则重启tomcat
    public void startMonitor(){
        new Thread(){
            public void run() {
                while(true){
                    try {
                        URL url = new URL(REQUEST_URL);
                        HttpURLConnection uConnection = (HttpURLConnection) url.openConnection();
                        uConnection.connect();
                        int code = uConnection.getResponseCode();
                        if(code!=200){
                            System.out.println(newDate("yyyy-MM-dd HH:mm:ss") + ":网页不能访问,准备重启;"+"HTTP_CODE:"+code);
                            restart();
                        }else{
                            System.out.println(newDate("yyyy-MM-dd HH:mm:ss") + ":tomcat 运行正常");
                        }

                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        System.out.println(newDate("yyyy-MM-dd HH:mm:ss") + ":网页不能访问,准备重启;"+"IOException");
                        restart();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally{
                        try {
                            Thread.sleep(1000*SLEEP_SECOND);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
        }.start();
    }

    private void restart(){
        try {
            String stopCommand[] = { "cmd.exe", "/c", STOP_PATH };
            String startCommand[] = { "cmd.exe", "/c", START_PATH };
            runProcess(stopCommand);
            runProcess(startCommand);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String newDate(String format) {
        java.text.DateFormat df = new SimpleDateFormat(format);
        return df.format(new Date());
    }

    private static void runProcess(String[] command) throws Exception {
        Process pro = Runtime.getRuntime().exec(command, null, new File(TOMCAT_PATH));

        // any error message?
        StreamGobbler errorGobbler = new 
            StreamGobbler(pro.getErrorStream(), "ERROR");

        // any output?
        StreamGobbler outputGobbler = new 
            StreamGobbler(pro.getInputStream(), "OUTPUT");

        // kick them off
        errorGobbler.start();
        outputGobbler.start();

        // any error???
        int exitVal = pro.waitFor();
        System.out.println(command + " exitValue() " + exitVal);
    }

    public static void main(String[] args) {
        System.out.println("TOMCAT_PATH="+Monitor.TOMCAT_PATH);
        System.out.println("REQUEST_URL="+Monitor.REQUEST_URL);
        System.out.println("SLEEP_SECOND="+Monitor.SLEEP_SECOND);
        System.out.println("START_PATH="+Monitor.START_PATH);
        System.out.println("STOP_PATH="+Monitor.STOP_PATH);
        new Monitor().startMonitor();
    }
}

config.properties
TOMCAT_PATH=D:\\tomcat8282
START_PATH=D:\\tomcat8282\\bin\\8282.bat
STOP_PATH=D:\\tomcat8282\\bin\\shutdown.bat
REQUEST_URL=http://localhost:8282/LotteryHome3/run.jsp
SLEEP_SECOND=60