View Javadoc

1   /*
2    * $Id: PollingMonitor.java,v 1.1 2004/03/17 21:18:02 johndavidtaylor Exp $
3    * Created on Feb 20, 2004 by John Taylor jdt@roe.ac.uk .
4    */
5   package org.abraracourcix.alipes.monitors;
6   import org.abraracourcix.alipes.listeners.Listener;
7   /***
8    * adds the threaded stuff required to do the polling of the resource Clients
9    * of subclasses need to call start() to kick off the class.
10   * @author jdt
11   * 
12   */
13  public abstract class PollingMonitor
14      extends AbstractMonitor
15      implements Runnable {
16      /***
17       * Conversioon between seconds and milliseconds
18       */
19      private static final int MILLIS_PER_SEC = 1000;
20      /***
21       * Commons-logging logger
22       */
23      private static org.apache.commons.logging.Log log =
24          org.apache.commons.logging.LogFactory.getLog(PollingMonitor.class);
25      /***
26       * Time in milliseconds between polls
27       */
28      private long waitTime;
29      /***
30       * Constructor
31       * 
32       * @param listener
33       *            who's listening to me?
34       * @param pollingFrequency
35       *            in seconds
36       */
37      public PollingMonitor(
38          final Listener listener,
39          final long pollingFrequency) {
40          super(listener);
41          setPollingFrequency(pollingFrequency);
42      }
43      /***
44       * Start polling. Should be called after the class is created. Cannot stick
45       * this in the ctor, because it calls nonfinal methods (EJS rule #81) and
46       * has the potential to access partially constructed classes.
47       */
48      public final void start() {
49          new Thread(this).start();
50      }
51      /***
52       * For subclasses to implement. Executed every pollingFrequency seconds.
53       *  
54       */
55      public abstract void pole();
56      /***
57       * Setter
58       * 
59       * @param seconds
60       *            between polls
61       */
62      public final void setPollingFrequency(final long seconds) {
63          log.debug("Setting polling frequency to " + seconds);
64          waitTime = seconds * MILLIS_PER_SEC;
65      }
66      /***
67       * getter
68       * 
69       * @return polling frequency in seconds
70       */
71      public final long getPollingFrequency() {
72          return waitTime / MILLIS_PER_SEC;
73      }
74      /***
75       * Standard Thread run method
76       */
77      public final void run() {
78          while (true) {
79              log.debug("Polling");
80              pole();
81              try {
82                  log.debug("Waiting");
83                  Thread.sleep(waitTime);
84              } catch (InterruptedException ie) {
85                  log.debug("Interrupted ", ie);
86              }
87          }
88      }
89  }
90  /*
91   * $Log: PollingMonitor.java,v $
92   * Revision 1.1  2004/03/17 21:18:02  johndavidtaylor
93   * Copied across from incubation in beanpeeler
94   *
95   * Revision 1.6  2004/03/14 18:49:15  johndavidtaylor
96   * corrected comments
97   *
98   * Revision 1.5  2004/03/11 15:46:34  johndavidtaylor
99   * Applied coding standards
100  * Revision 1.4 2004/03/11 11:33:46
101  * johndavidtaylor Refactored the way that the PollingMonitor worked.
102  * Previously it got started in its ctor, but this allowed access to subclass
103  * resources that hadn't yet been initialised. Bad. Now needs to be started
104  * explicitly post-construction.
105  * 
106  * Revision 1.3 2004/03/09 22:29:33 johndavidtaylor Added stuff for file
107  * wildcards/regexps
108  * 
109  * Revision 1.2 2004/03/08 20:16:45 johndavidtaylor refactored to new package
110  * name
111  * 
112  * Revision 1.1 2004/03/08 19:58:25 johndavidtaylor Initial commit -transfer
113  * from previous repository
114  * 
115  * Revision 1.1 2004/03/02 23:04:37 jdt new
116  *  
117  */