KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > integrationtests > framework > WatchDog


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
13 import java.util.TimerTask JavaDoc;
14
15 public class WatchDog {
16   protected Log logger = LogFactory.getLog(getClass());
17
18   private Thread JavaDoc threadToWatch;
19   private Timer JavaDoc timer;
20   private TimerTask JavaDoc timerTask;
21   private TimerTask JavaDoc 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 JavaDoc();
29   }
30
31   public void startWatching() {
32     logger.debug("Watching thread");
33     timerTask = new TimerTask JavaDoc() {
34       public void run() {
35         logger.error("Thread timeout..interrupting");
36         threadToWatch.interrupt();
37
38       }
39     };
40
41     dumpTask = new TimerTask JavaDoc() {
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