该文章仅供参考,实现上有不足之处欢迎各位指教
Windows下测试tomcat监听重启
将Tomcat设置为自动启动的服务
- 进入tomcat的bin目录下
方法一:在cmd下通过cd命令一层一层进入到tomcat的bin文件下
方法二:直接打开toamcat的bin文件夹,在头部输入框输入cmd直接打开
- 输入service install tomcat7Test
- tomcat7Test是服务名,可以随便取值,默认为tomcat7,可以看到服务已经成功安装,接下来取服务里面,看下有没有该服务
- 接下来在服务里将启动类型设置为自动即可
程序监听
1、程序内添加配置文件config.properties
#系统命令 启动
start=net start Tomcat7
#关闭
stop = net stop Tomcat7
#测试连接总次数
testTotalCount=3
#连接失败时,再次检测时间间隔,单位为秒
testIntervalTime=3
#连接超时时间,即多少秒tomcat没响应视为宕机,单位为秒
connectionTimeout=15
#tomcat启动时间,防止在tomcat尚未启动完成的时候,程序又去检验tomcat状态,造成尚未启动完成又重新启动,单位为秒
tomcatStartTime=60
#测试连接地址
testHttp=http://117.158.207.45:8081/index.jsp
#正常情况下,每次检测间隔时间,单位为秒
waitIntervalTime=30
2、添加TomcatMonitor.java文件
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;
import java.*;
/**
* @describe:TomcatMonitor.java
* @author Lee
*/
public class TomcatMonitor implements Runnable{
String start="";
String stop="";
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);
stop=p.getProperty("stop");
start=p.getProperty("start");
testHttp=p.getProperty("testHttp");
testIntervalTime=Integer.parseInt(p.getProperty("testIntervalTime"));
waitIntervalTime=Integer.parseInt(p.getProperty("waitIntervalTime"));
testTotalCount=Integer.parseInt(p.getProperty("testTotalCount"));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("*******************init success*******************");
thread=new Thread(this);
thread.start();
}
public void run() {
System.out.println("Monitoring...");
int testCount=0;
while(true){
testCount=0;
testCount++;
boolean isrun=test();
System.out.println("connecting...,connect count:"+testCount+",result:"+(isrun==false?"fail...":"success!"));
while(!isrun){
if(testCount>=testTotalCount)break;
try {
thread.sleep(testIntervalTime*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
testCount++;
System.out.println("connecting...,connect count:"+testCount+",result:"+(isrun==false?"fail...":"success!"));
isrun=test();
}
if(!isrun){
try{
//关闭tomcat服务
Process proc = Runtime.getRuntime().exec(stop);
thread.sleep(5000);
//启动tomcat服务
System.out.println("connect failed, starting tomcat");
Process p=Runtime.getRuntime().exec(start);
System.out.println("restart tomcat success");
}catch(Exception e){
e.printStackTrace();
System.out.println("restart tomcat failed,please check errInfo.....");
}
}
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();
}
}
3、将项目打war包放入tomcat下,启动tomcat进行测试