1
2
3
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117