Automatic reboot for Tomcat

Source: Internet
Author: User

Original: Tomcat for automatic restart

Source code: Http://www.zuidaima.com/share/1550463736564736.htm

The mid-autumn day is coming, because the customer's server is not very stable, resulting in the application often crashes, so wrote a restart Tomcat program, bring to share. Java daemon: The idea is like this: 1. Monitor whether the server is able to access the normal; 2. If it is not properly accessed, restart the TOMCT service based on the configured information if it is able to access it at regular intervals. The use of the doc command, code comments are very clear, do not understand can ask me

    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 small C www.zuidaima.com */publi C class Tomcatmonitor implements runnable{String procname= "Tomcat6.exe";//Process name//string fullprocname= "c:\\tomcat6\    \bin\\tomcat6.exe "; String fullprocname= "";    Tomcat6.exe Absolute Physical Address//string command= "cmd.exe/c start"; String command= "";    System command//string testhttp= "http://127.0.0.1:8080";  String testhttp= ""; Test Connection Address int testintervaltime=1;//Test connection interval time, in seconds int waitintervaltime=2; Wait for the test interval time, in seconds int testtotalcount=5;    Total number of test connections Thread Thread=null; Public Tomcatmonitor () {InputStream in = TomcatMonitor.class.getResourceAsStream ("config.properties"); Properties P = new properties (); try {p.load (in);p Rocname=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 ("Initialize successfully!");} catch (IOException e) {e.printstacktrace ();} Thread=new Thread (this); Thread.Start ();}        public void Run () {string[] cmd = {"Tasklist"};           SYSTEM.OUT.PRINTLN ("Under monitoring ...");            int Testcount=0;while (true) {testcount=0;testcount++;            Boolean isrun=test (); System.out.println ("Starting the test connection, trying the connection count:" +testcount+ ", the result is:" + (Isrun==false? ")                        Failed. ":" Success! "));            while (!isrun) {if (testcount>=testtotalcount) break; try {thread.sleep (testintervaltime*1000);} catch (Interruptedexception e) {e.printstacktrace ();} testcount++; System.out.println ("Starting the test connection, trying the connection count:" +testcount+ ", the result is:" + (Isrun==false? ") Failed.":" Success! ");            Isrun=test ();            } if (!isrun) {System.out.println ("Test connection failed, rebooting 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 ("Reboot tomcat Success");  }catch (Exception e) {e.printstacktrace (); }} try {Thread.Sleep (waitintervaltime*1000);} catch (Interruptedexception e) {e.printst                        Acktrace ();} 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 ()));                                          Instantiate the input stream and get the page code 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 ();}


Automatic reboot for Tomcat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.