1.Java方法:CheckTomcat .java
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
public class CheckTomcat {
private static String tomcatroot="";
private static String monitorurl="";
private static void checkTomcatIsAlive(String myurl) throws NullPointerException {
String s;
boolean isTomcatAlive = false;
java.io.BufferedReader in;
try {
System.out.println(">>>>>>检测URL:"+myurl);
URL url = new URL(myurl);
URLConnection con = url.openConnection();
in = new java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream()));
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
while ((s = in.readLine()) != null) {
if (s.length() > 0) {// 如果能够读取到页面则证明可用,tomcat正常,否则继续后面的重启tomcat操作。
System.out.println("运行正常");
return;
}
}
in.close();
}catch (Exception ex) {
//ex.printStackTrace();
System.out.println("*************该URL有误或不可访问!"+ex);
}
/*if (isTomcatAlive) {
System.out.println("<" + new Date()+ "> Tomcat is alive but not response!");
stopTomcat();
}*/
RunTomcat runt=new RunTomcat();
runt.startTomcat(tomcatroot);
}
/*public static void stopTomcat() {
try {
//java.lang.Process p = java.lang.Runtime.getRuntime().exec("net stop /"Apache Tomcat/"");
java.lang.Process p = java.lang.Runtime.getRuntime().exec(tomcatroot+"bin//shutdown.bat");
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
String s;
String t = "Using JRE_HOME";
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 main(String[] args) {
System.out.println("********************************************************");
System.out.println("====本程序自动检测Tomcat运行状况,必要时自动重启Tomcat。====");
System.out.println("********************************************************");
init_config();
if(monitorurl.equals(""))monitorurl="http://localhost:28080/monitor.jsp";
if(tomcatroot.equals(""))tomcatroot="E://apache7.1";
if(!tomcatroot.endsWith("//"))tomcatroot+="//";
while (true) {
try {
String random="?random="+Math.random() * 65535;//=====处理数据缓存问题======
CheckTomcat.checkTomcatIsAlive(monitorurl+random);
Thread.sleep(5000);
System.out.println("========================checking at <"+new Date()+">");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
static private void init_config() {
try{
CheckTomcat me=new CheckTomcat();
String maindir=me.getClass().getResource("/").toURI().getPath();
System.out.println(">>>>>>配置文件目录:"+maindir);
String sLine;
String filename=maindir+"config.xml";
System.out.println(filename);
BufferedReader buffReader = new BufferedReader(new FileReader(filename));
while((sLine = buffReader.readLine())!=null)
{
sLine = sLine.trim();
if(sLine.trim()!="" && !sLine.equals("")){
if(sLine.toLowerCase().startsWith("tomcatroot")){
int npos=sLine.indexOf("tomcatroot");
npos+="tomcatroot".length();
tomcatroot=sLine.substring(npos).trim();
if(tomcatroot.startsWith("="))tomcatroot=tomcatroot.substring(1);
}
else if(sLine.toLowerCase().startsWith("monitorurl")){
int npos=sLine.indexOf("monitorurl");
npos+="monitorurl".length();
monitorurl=sLine.substring(npos).trim();
if(monitorurl.startsWith("="))monitorurl=monitorurl.substring(1);
}
}
}
buffReader=null;
}catch(Exception e){
e.printStackTrace();
System.out.println("********************************************************");
System.out.println("====读取配置文件失败!系统无法运行,请与供应商联系。====");
System.out.println("********************************************************");
System.exit(0);
}
}
}
2.-----------------------------------分割线---------------------------------RunTomcat .java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
public class RunTomcat extends Thread {
private static String tomcatroot="";
public void startTomcat(String root) {
this.tomcatroot=root;
System.out.println(">>>>>>Tomcat即将启动。。。");
System.out.println(">>>>>>Tomcat根目录:"+tomcatroot);
try {
//java.lang.Process p = java.lang.Runtime.getRuntime().exec("net stop /"Apache Tomcat/"");
//java.lang.Process p = java.lang.Runtime.getRuntime().exec(tomcatroot+"bin//shutdown.bat");
java.lang.Process p = java.lang.Runtime.getRuntime().exec(" cmd /c "+tomcatroot+"//bin//shutdown.bat");
final InputStream in = p.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
StringBuilder buf = new StringBuilder();
String line = null;
while((line = br.readLine()) != null)
buf.append(line);
} catch (Exception e) {
e.printStackTrace();
System.out.println("关闭失败==="+e);
}
try {
Thread.sleep(3000);//等待shutdown结束
//RunTomcat me=new RunTomcat();
//String maindir=me.getClass().getResource("/").toURI().getPath();
//java.lang.Process p = java.lang.Runtime.getRuntime().exec(maindir+"checkTomcat.bat");
// java.lang.Process p = java.lang.Runtime.getRuntime().exec(tomcatroot+"bin//startup.bat");
java.lang.Process p = java.lang.Runtime.getRuntime().exec(" cmd /c "+tomcatroot+"//bin//startup.bat");
final InputStream in = p.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
StringBuilder buf = new StringBuilder();
String line = null;
while((line = br.readLine()) != null)
buf.append(line);
System.out.println("输出结果为:" + buf);
// java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
System.out.println("启动中------");
/*String s;
boolean restart = false;
String t = "Server startup in";
while ((s = in.readLine()) != null) {
System.out.println(s);
if (s.indexOf(t) != -1) {
restart = true;
break;
}
}*/
System.out.println(">>>>>>Tomcat start at <" + new Date() + ">");
} catch (Exception e) {
e.printStackTrace();
System.out.println("启动失败"+111);
}
}
}
3.配置文件----------------------------config.xml
tomcatroot=E:/apache7.1
monitorurl=http://localhost:28080/monitor.jsp
4.jsp监测-----------------------------
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.println("path=" + path + "<br>");
out.println("basePath=" + basePath + "<br>");
out.println("ok<br>");
%>
4.最后一步,配置bat------------------
@echo off
rem=========第一步:配置下面的JAVA_HOME为JDK目录==========#
@set JAVA_HOME=E:/Program Files/Java/jdk1.8.0_121
rem=========第二步:配置下面的CATALINA_HOME为Tomcat目录==========#
@set CATALINA_HOME=E:/apache7.1
@set PATH=%JAVA_HOME%/bin/;
@set CLASSPATH=%JAVA_HOME%/lib/rt.jar;%JAVA_HOME%/lib/tools.jar;%CATALINA_HOME%/lib/servlet-api.jar;%CATALINA_HOME%/lib/jsp-api.jar;
java CheckTomcat
pause;