java监控tomcat_java监控tomcat运行代码

/***关闭tomcat* @author wdw*/package com.tomcat;public class TomcatShutDown {public static boolean stopTomcat() {try {String commandstr = "/usr/tomcat/bin/shutdown.sh";Process p;System.out.print("start ShutDown\n");p = Runtime.getRuntime().exec(commandstr);if (p.waitFor() == 0){System.out.print("ShutDown over\n");return true;}else{System.out.print("ShutDown fail\n");return false;}} catch (Exception e) {e.printStackTrace();return false;}}public static void main(String[] args) {stopTomcat() ;}}/***启动tomcat* @author wdw*/package com.tomcat;public class TomcatStartup {public static boolean startTomcat() {try {String commandstr = "/tomcat/bin/startup.sh";Process p;System.out.print("start start\n");p = Runtime.getRuntime().exec(commandstr);if (p.waitFor() == 0){System.out.print("start over\n");return true;}else{System.out.print("start fail\n");return false;}} catch (Exception e) {e.printStackTrace();return false;}}public static void main(String[] args) {startTomcat() ;}}/***重新启动tomcat* @author wdw*/package com.tomcat;import java.util.Date;public class TomcatRestart {public static void restart(){TomcatShutDown.stopTomcat() ;TomcatStartup.startTomcat();System.out.println("run restart " + new Date());try {Thread.sleep(30000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args) {restart();}}/***检测tomcat状态* @author wdw* @site*/package com.tomcat;import java.net.URL;import java.net.URLConnection;import java.util.Date;public class Detector {private static boolean keepTomcatAlive() throws NullPointerException {String s;String t = new String("/tomcat/bin/shutdown.sh");java.io.BufferedReader in;try {URL url = new URL("http://www.mowker.com/");URLConnection con = url.openConnection();in = new java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream()));con.setConnectTimeout(2000);con.setReadTimeout(6000);while ((s = in.readLine()) != null) {if (s.length() > 0) {return true;}}in.close();} catch (Exception ex) {}TomcatRestart.restart();return false;}public static void main(String[] args) {long lasttime = System.currentTimeMillis();long maxRunTime = 60*60*2000;while (true) {if( System.currentTimeMillis() -lasttime >maxRunTime){TomcatRestart.restart();lasttime = System.currentTimeMillis();}else{try {Detector.keepTomcatAlive();Thread.sleep(10000);} catch (Exception ex) {}}}}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值