1 4 package com.tctest.spring.integrationtests.framework; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 9 import com.tc.util.runtime.Os; 10 import com.tc.util.runtime.ThreadDump; 11 12 import java.util.Timer ; 13 import java.util.TimerTask ; 14 15 public class WatchDog { 16 protected Log logger = LogFactory.getLog(getClass()); 17 18 private Thread threadToWatch; 19 private Timer timer; 20 private TimerTask timerTask; 21 private TimerTask dumpTask; 22 23 private int timeoutInSecs; 24 25 public WatchDog(int timeOutInSecs) { 26 timeoutInSecs = timeOutInSecs; 27 this.threadToWatch = Thread.currentThread(); 28 this.timer = new Timer (); 29 } 30 31 public void startWatching() { 32 logger.debug("Watching thread"); 33 timerTask = new TimerTask () { 34 public void run() { 35 logger.error("Thread timeout..interrupting"); 36 threadToWatch.interrupt(); 37 38 } 39 }; 40 41 dumpTask = new TimerTask () { 42 public void run() { 43 if (Os.isUnix()) { 44 ThreadDump.dumpProcessGroup(); 45 } 46 } 47 }; 48 49 timer.schedule(timerTask, timeoutInSecs * 1000); 50 timer.schedule(dumpTask, (timeoutInSecs - 45) * 1000); 51 } 52 53 public void stopWatching() { 54 logger.debug("watching cancelled.."); 55 timerTask.cancel(); 56 dumpTask.cancel(); 57 timer.cancel(); 58 } 59 60 } 61 | Popular Tags |