Testing tomcat service status, automatic restart Tomcat

2010-03-29  来源:本站原创  分类:Java  人气:312 

The first method: Monitor request

Principle is simple. On the machine to do a test file. If our program can read into this file, you proved to work properly, can be simply achieved Tomcat HA.

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

/ / If you can read the page then the proof can be used

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 = "successfully stopped";

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 = "start successfully";

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

)

)

)

)

The second way: Monitoring the log due to tomcat to deploy the application, in the process of customers, each will always hang over 56 days into a zombie process, view the log is an invalid argument, scouring the web, tried many ways to have does not solve this problem. At the same time not change the web container, because it will increase costs, but to use java wrote a monitoring program is currently being tested in the

import java.net.URL;

import java.net.URLConnection;

import java.text.SimpleDateFormat;

import java.util.Date;

import org.apache.log4j.Logger;

import org.apache.log4j.PropertyConfigurator;

import java.util.TimeZone;

/ **

* NAME: tomcat.java

* FUNCTION: monitoring server tomcat status

* PROGRAMMED: TELSTAR / jlsu

* DATE (ORG): 20090109

* PROJECT: TELSTAR

* OS: HP-UNIX

* HISTORY:

* /

public class TomcatDetector (

private long sJKdate;

public TomcatDetector () (

Date now = new Date ();

sJKdate = now.getTime ();

)

private static final Logger logger = Logger.getLogger (TomcatDetector.class);

private static void keepTomcatAlive () throws NullPointerException (

String s;

boolean isTomcatAlive = false;

java.io.BufferedReader in;

Date now = new Date ();

long lnow = now.getTime ();

Date selDate = new Date ();

SimpleDateFormat df = new SimpleDateFormat ( "yyyy-MM-dd HH: mm: ss");

df.setTimeZone (TimeZone.getTimeZone ( "Etc/GMT-8"));

String sSel_date = df.format (selDate);

try (

URL url = new URL ( " http://133.128.4.7:8080/index.jsp ");

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

/ / If in.readLine the content is not empty, then the proof of the current tomcat container can be used, but the operation of a direct return to

return;

)

)

in.close ();

) Catch (Exception ex) (

)

try (

java.lang.Process p = java.lang.Runtime.getRuntime (). exec ( "ps-ef | grep Djava.uti | grep-v tail | grep-v vi | grep-v grep | awk '(print $ 2) ' ");

in = new java.io.BufferedReader (new java.io.InputStreamReader (p.getInputStream ()));

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

isTomcatAlive = true;

break;

)

in.close ();

) Catch (Exception e) (

e.printStackTrace ();

)

if (isTomcatAlive) (

logger.debug (sSel_date + "> Tomcat is alive but not response!");

stopTomcat ();

)

startTomcat ();

)

public static void stopTomcat () (

Date now = new Date ();

long lnow = now.getTime ();

Date selDate = new Date ();

SimpleDateFormat df = new SimpleDateFormat ( "yyyy-MM-dd HH: mm: ss");

df.setTimeZone (TimeZone.getTimeZone ( "Etc/GMT-8"));

String sSel_date = df.format (selDate);

try (

logger.debug ("------------------ start killing zombies tomcat --------------------- " );

java.lang.Process p = java.lang.Runtime.getRuntime (). exec ( "kill -9` ps-ef | grep Djava.uti | grep-v tail | grep-v vi | grep-v grep | awk ' (print $ 2 }'`");

java.lang.Process p1 = java.lang.Runtime.getRuntime (). exec ( "ps-ef | grep java | grep-v vi | grep-v tail | grep-v grep");

java.io.BufferedReader in = new java.io.BufferedReader (new java.io.InputStreamReader (p1.getInputStream ()));

String s;

String t = "Djava";

boolean restart = false;

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

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

restart = true;

break;

)

)

logger.debug (sSel_date + "> Tomcat is stop" + (restart? "OK": "ERROR"));

) Catch (Exception e) (

e.printStackTrace ();

)

)

public static void startTomcat () (

Date now = new Date ();

long lnow = now.getTime ();

Date selDate = new Date ();

SimpleDateFormat df = new SimpleDateFormat ( "yyyy-MM-dd HH: mm: ss");

df.setTimeZone (TimeZone.getTimeZone ( "Etc/GMT-8"));

String sSel_date = df.format (selDate);

try (

java.lang.Process p = java.lang.Runtime.getRuntime (). exec ( "kill -9` ps-ef | grep Djava.uti | grep-v tail | grep-v vi | grep-v grep | awk ' (print $ 2 }'`");

) Catch (Exception e) (

e.printStackTrace ();

)

try (

logger.debug ("------------------ start restart ---------------------");

java.lang.Process p1 = java.lang.Runtime.getRuntime (). exec ( "/ telstar / tomcat / bin /. / startup.sh &");

java.io.BufferedReader in = new java.io.BufferedReader (new java.io.InputStreamReader (p1.getInputStream ()));

String s;

String t = "tomcat";

boolean restart = false;

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

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

restart = true;

break;

)

)

logger.debug (sSel_date + "> Tomcat is start" + (restart? "OK": "ERROR"));

) Catch (Exception e) (

e.printStackTrace ();

)

)

public static void main (String [] args) (

Date now = new Date ();

long lnow = now.getTime ();

Date selDate = new Date ();

SimpleDateFormat df = new SimpleDateFormat ( "yyyy-MM-dd HH: mm: ss");

df.setTimeZone (TimeZone.getTimeZone ( "Etc/GMT-8"));

String sSel_date = df.format (selDate);

PropertyConfigurator.configure ( "/ telstar / bin / tomcatJK / logtomcatJK.properties");

logger.debug ("------------------ start monitoring ---------------------");

while (true) (

try (

TomcatDetector.keepTomcatAlive ();

logger.debug ("------------------ test tomcat status ---------------------");

Thread.sleep (24000);

) Catch (Exception ex) (

)

)

)

)

相关文章
  • Testing tomcat service status, automatic restart Tomcat 2010-03-29

    The first method: Monitor request Principle is simple. On the machine to do a test file. If our program can read into this file, you proved to work properly, can be simply achieved Tomcat HA. import java.net.URL; import java.net.URLConnection; import

  • class automatic restart tomcat update 2008-11-26

    In the D: \ Tomcat 6.0 \ conf \ server.xml in the <Host name = "localhost" appBase = "webapps" unpackWARs = "true" autoDeploy = "true" xmlValidation = "false" xmlNamespaceAware = "false"> &

  • Automatic restart tomcat 2011-03-14

    #! / Bin / sh G = "check.log" PID = "check.pid" HOME = "/ home / ls /" TOMCAT = "/ etc/init.d/tomcat5.sh" case "$ 1" in start) if [-f $ HOME / $ PID]; then if [-d / proc / `cat $ HOME / $ PID`]; then echo

  • under the tomcat service linux start. Close and error tracking 2010-04-01

    under the tomcat service linux startup, shutdown and error tracking, remote connection to the server later, usually closed by the following ways start tomcat service: 1). Start the tomcat service Into the tomcat bin directory under your home director

  • Linux, start tomcat service. Close and error tracking 2010-07-30

    Linux services under the tomcat startup, shutdown and error tracking, remote connection to the server later, usually closed by the following ways start tomcat service: 1). Start the tomcat service Into the tomcat bin directory under your home directo

  • Restart Tomcat batch 2010-04-26

    @ Echo off @ Echo restart the Tomcat service @ Echo ================================================ ======== @ Net stop "Apache Tomcat" @ Net start "Apache Tomcat" @ Pause

  • Ubuntu is installed JDK configure Tomcat Service 2010-10-15

    Ubuntu is installed JDK configure Tomcat Service Ubuntu is installed JDK configure Tomcat Service 1, first install the JDK Installed jdk-6u17-linux-i586.bin version. First, switch to an administrator account root007 @ bill021: / # sudo su Then go to

  • [Reprinted] monitoring Tomcat downtime and restart Tomcat 2010-03-29

    The following code from the Internet is, and a little note of the points. /** * Monitor Tomcat is downtime if downtime will automatically restart */ package cn.ipcat; import java.net.URL; import java.net.URLConnection; import java.util.Date; public c

  • To solve a single machine run multiple Tomcat Service 2010-04-26

    Reprinted from: http://www.cnblogs.com/itolssy/archive/2008/08/27/1278041.html If you do not add any changes on a single server to run two Tomcat service is clear port conflicts will occur. Suppose now been installed in accordance with the normal way

  • Ubuntu installed JDK configure Tomcat service 2010-05-24

    1. First install JDK Install the jdk-6u13-linux-i586.bin version. First switch to the administrator account root @ bill021: / # sudo su Then go to the jdk installation directory are put directly to the Executive: root @ bill021: / # sh ./jdk-6u13-lin

  • Restart tomcat and session 2010-09-15

    Close and restart Tomcat 5 When , tomcat will try to serialize the session resources exist. If sessions related to the object does not implement serializable interface will appear Cannot serialize session attribute XXX for abnormal java.io.NotSeriali

  • Windows, installed on the same machine multiple Tomcat service 2010-11-16

    Windows, installed on the same machine multiple Tomcat service to Tomcat5.5 this example to explain how to install multiple Tomcat Windows service. 1. Software Download: http://ftp.kddilabs.jp/infosystems/apache/tomcat/tomcat-5/v5.5.27/bin/apache-tom

  • liferay how to use the tomcat service items made of 2010-12-03

    liferay how to use the tomcat service items made of! liferay tomcat to use the method in accordance with the general service items can not be made! you pointers about Oh!

  • Linux also forced to close open ssh service tomcat service 2011-01-04

    weboff file contents: cd / apps/tomcat-6.0.29/bin . / Shutdown.sh sleep 4 / Killtomcat.sh sleep 1 / Sbin / service sshd start cd webon file contents: service sshd stop sleep 4 cd / apps/tomcat-6.0.29/bin / . / Startup.sh cd killtomcat.sh file content

  • The service can not start Tomcat, the original is no jvm.dll 2011-01-04

    Another problem in the verification today when he suddenly found himself not start a tomcat. TOMCAT comes through the Configure Tomcat can not start the service through my computer - "Administrative -> Services and Applications -> Service can n

  • SVN configuration tomcat service configuration and some of the problems 2009-04-25

    JSP has two months to learn fast, did not learn how to write notes. Mainly to school, there need to build class sites, plus a large living side employment information network, it is busy. Today is Saturday, a little tomcat server configuration and sv

  • Linux open simultaneously forced to close the ssh service tomcat service 2011-01-04

    weboff contents of the file: cd / apps/tomcat-6.0.29/bin . / Shutdown.sh sleep 4 / Killtomcat.sh sleep 1 / Sbin / service sshd start cd webon contents of the file: service sshd stop sleep 4 cd / apps/tomcat-6.0.29/bin / . / Startup.sh cd killtomcat.s

  • linux can be more convenient to start | off | restart tomcat, to be a script 2011-03-21

    Script path / etc / init.d / tomcat First implementation: vi / etc / init.d / tomcat According to a compilation, the following content according to your own circumstances (mainly modify / usr/local/tomcat6/bin /) Revised copy to the edit box, esc to

  • 将Tomcat Service化 2013-12-12

    按照<Tomcat IIS HowTo>一文还是需要手工启动tomcat,这非常烦人的说 我们可以借助一些工具是的tomcat作为windows nt 的一个service,在windows启动时自动装载,步骤如下: 1,下载工具,我用的是gservany - Utility to run standard applications as services on NT. http://www.advok.com/gservany.html 下载,将zip文件解压缩,将gservany.exe放

  • tomcat service.xml文件的配置 2012-12-23

    解读tomcat服务器server.xml文件2009-11-06 10:551. <Server port="8005" shutdown="SHUTDOWN"> 2. <!--属性说明 3. port:指定一个端口,这个端口负责监听关闭Tomcat的请求 4. shutdown:向以上端口发送的关闭服务器的命令字符串 5. --> 6. <Listener className="org.apache.catalina.co