Windows下测试tomcat监听重启


该文章仅供参考,实现上有不足之处欢迎各位指教

Windows下测试tomcat监听重启

将Tomcat设置为自动启动的服务

  1. 进入tomcat的bin目录下
    方法一:在cmd下通过cd命令一层一层进入到tomcat的bin文件下
    方法二:直接打开toamcat的bin文件夹,在头部输入框输入cmd直接打开在这里插入图片描述
    在这里插入图片描述
  2. 输入service install tomcat7Test在这里插入图片描述
  3. tomcat7Test是服务名,可以随便取值,默认为tomcat7,可以看到服务已经成功安装,接下来取服务里面,看下有没有该服务
    在这里插入图片描述
  4. 接下来在服务里将启动类型设置为自动即可

程序监听

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进行测试

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值