Tomcat實現自動重啟,Tomcat實現重啟

來源:互聯網
上載者:User

Tomcat實現自動重啟,Tomcat實現重啟

原文:Tomcat實現自動重啟

原始碼:http://www.zuidaima.com/share/1550463736564736.htm

中秋國慶馬上來臨,由於客戶的伺服器不是很穩定,導致應用經常崩潰,所以寫了個重啟tomcat的程式,拿來給大家分享。 java背景程式 :大概思路是這樣的: 1.監控伺服器端是否能正常訪問; 2.如果不能正常訪問,則根據配置的資訊來重新啟動tomct服務 如果能正常訪問則隔一段時間在監控。。。 其中用到了doc命令,代碼注釋很明確,不明白的可以問我

    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;/** * @describe:TomcatMonitor.java *  * @date 2013-9-18 * @author 小C www.zuidaima.com */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();}}    


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.