TOMCAT启动(原版网上摘抄)

import java.net.URL;

import java.net.URLConnection;

import java.util.Date;

 

public class Detector {

    private static void keepTomcatAlive() throws NullPointerException {

       String s;

       String t = new String("tomcat5.exe");

       boolean isTomcatAlive = false;

       java.io.BufferedReader in;

       try {

           URL url = new URL("http://localhost/heartbeat.htm");

           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) {

                  // 如果能够读取到页面则证明可用

                  return;

              }

           }

           in.close();

       } catch (Exception ex) {

       }

 

       try {

           java.lang.Process p = java.lang.Runtime.getRuntime().exec(

                  "tasklist");

 

           in = new java.io.BufferedReader(new java.io.InputStreamReader(p

                  .getInputStream()));

 

           while ((s = in.readLine()) != null) {

              if (s.startsWith(t)) {

                  isTomcatAlive = true;

                  break;

              }

           }

           in.close();

       } catch (Exception e) {

           e.printStackTrace();

       }

       if (isTomcatAlive) {

           System.out.println("<" + new Date()

                  + "> Tomcat is alive but not response!");

           stopTomcat();

       }

       startTomcat();

    }

 

    public static void stopTomcat() {

       try {

           java.lang.Process p = java.lang.Runtime.getRuntime().exec(

                  "net stop \"Apache Tomcat\"");

           java.io.BufferedReader in = new java.io.BufferedReader(

                  new java.io.InputStreamReader(p.getInputStream()));

           String s;

           String t = "成功停止";

           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 startTomcat() {

       try {

           java.lang.Process p = java.lang.Runtime.getRuntime().exec(

                  "net stop \"Apache Tomcat\"");

       } catch (Exception e) {

           e.printStackTrace();

       }

       try {

           java.lang.Process p = java.lang.Runtime.getRuntime().exec(

                  "net start \"Apache Tomcat\"");

           java.io.BufferedReader in = new java.io.BufferedReader(

                  new java.io.InputStreamReader(p.getInputStream()));

           String s;

           String t = "启动成功";

           boolean restart = false;

           while ((s = in.readLine()) != null) {

              if (s.indexOf(t) != -1) {

                  restart = true;

                  break;

              }

           }

           System.out.println("<" + new Date() + "> Tomcat is start "

                  + (restart ? "OK" : "ERROR"));

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

 

    public static void main(String[] args) {

       while (true) {

           try {

              Detector.keepTomcatAlive();

              Thread.sleep(30000);

           } catch (Exception ex) {

 

           }

       }

    }

 

}








import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;


 


public class Detector {




//一直保持TOMCAT的方法(原来停止和开始都在这里面调用)
    private static void keepTomcatAlive() throws NullPointerException {


    String start="F://apache-tomcat-7.0.20//bin//startup.bat";
String end="F://apache-tomcat-7.0.20//bin//shutdown.bat";
       String s;


       String t = new String("Tomcat7.exe");


       boolean isTomcatAlive = false;


       java.io.BufferedReader in;


       try {


           URL url = new URL("http://localhost:8080");
           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) {
                  // 如果能够读取到页面则证明可用
                  return;} } in.close(); } catch (Exception ex) {   }
       try {
           java.lang.Process p = java.lang.Runtime.getRuntime().exec("tasklist");//输入命令行清单,看里面有没有tomcat
           in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
           while ((s = in.readLine()) != null) {
              if (s.startsWith(t)) {
                  isTomcatAlive = true;
                  break;
              }
           }
           in.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
       if (isTomcatAlive) {
           System.out.println("<" + new Date() + "> Tomcat is alive but not response!");
           excuteCommand(end);
       }
       excuteCommand(start);}


 
    public static void  excuteCommand(String command)
    {




            try {


            Process  p =  Runtime.getRuntime().exec(command);
                BufferedReader br = new BufferedReader(new InputStreamReader(p
                        .getInputStream()));
                String inline;
                while ((inline = br.readLine()) != null) {
                    System.out.println(inline);
                    
                }
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    }


 
//主方法
    public static void main(String[] args) {


       while (true) {


           try {


              Detector.keepTomcatAlive();


              Thread.sleep(30000);


           } catch (Exception ex) {


 


           }


       }


    }


 


}

你可能感兴趣的:(TOMCAT启动(原版网上摘抄))