检测tomcat服务状态,自动重启Tomcat - - JavaEye技术网站

{dy}种方式:监控请求
?

原理很简单。 在机器上做一个测试的文件。如果我们的程序能够读取到这个文件,则证明运行正常,可简单实现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("");

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

?

?????????? }

?????? }

??? }

?

}

?

第二种方式:监控日志
由于tomcat部署的应用,在客户使用的过程中,每过五六天总会吊死成僵尸进程,查看日志为无效的变元,搜遍网络,尝试了很多办法也没有解决此问题。同时不能更换web容器,因为会增加成本,只好用java写了个监控程序,目前正在测试中

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 : 监控服务器tomcat状态

?* 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("");

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

????????? // 如果in.readLine的内容不为空,则证明当前的tomcat容器可用,不过操作直接返回

????????? 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("------------------开始杀死僵尸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("------------------开始重启---------------------");

??????? 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("------------------开始监控---------------------");

?????? while (true) {

?????????? try {

????????????? TomcatDetector.keepTomcatAlive();

????????????? logger.debug("------------------检测tomcat状态---------------------");

????????????? Thread.sleep(24000);

?????????? } catch (Exception ex) {

?????????? }

?????? }

??? }

?

}

?

?

郑重声明:资讯 【检测tomcat服务状态,自动重启Tomcat - - JavaEye技术网站】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——