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 block 
e.printStackTrace(); 
} 
} 
public static void main(String[] args) { 
restart(); 
} 
} 



/** 
*检测tomcat状态 
* @author wdw 
* @site www.perji.com  
*/ 
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) { 
} 
} 

} 

} 
} 

本文出自 “dwei.mail.ustc.edu.cn” 博客,谢绝转载!

你可能感兴趣的:(java,tomcat,职场,监控,休闲)