KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > model > PeriodicWork


1 package hudson.model;
2
3 import java.util.TimerTask JavaDoc;
4 import java.util.logging.Level JavaDoc;
5 import java.util.logging.Logger JavaDoc;
6
7 /**
8  * Abstract base class for a periodic work.
9  *
10  * @author Kohsuke Kawaguchi
11  */

12 public abstract class PeriodicWork extends TimerTask JavaDoc {
13
14     /**
15      * PluginName of the work.
16      */

17     private final String JavaDoc name;
18     private Thread JavaDoc thread;
19
20     protected final Logger JavaDoc logger = Logger.getLogger(getClass().getName());
21
22     protected PeriodicWork(String JavaDoc name) {
23         this.name = name;
24     }
25
26     /**
27      * Schedules this periodic work now in a new thread, if one isn't already running.
28      */

29     public final void run() {
30         try {
31             if(thread!=null && thread.isAlive()) {
32                 logger.log(Level.INFO, name+" thread is still running. Execution aborted.");
33                 return;
34             }
35             thread = new Thread JavaDoc(new Runnable JavaDoc() {
36                 public void run() {
37                     logger.log(Level.INFO, "Started "+name);
38                     long startTime = System.currentTimeMillis();
39
40                     execute();
41
42                     logger.log(Level.INFO, "Finished "+name+". "+
43                         (System.currentTimeMillis()-startTime)+" ms");
44                 }
45             },name+" thread");
46             thread.start();
47         } catch (Throwable JavaDoc t) {
48             logger.log(Level.SEVERE, name+" thread failed with error", t);
49         }
50     }
51
52     protected abstract void execute();
53 }
54
Popular Tags