01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.test.server.appserver.deployment;
05:
06: import org.apache.commons.logging.Log;
07: import org.apache.commons.logging.LogFactory;
08:
09: 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: }
|