1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.util; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import uk.ac.ukc.iscream.util.*; |
6 |
import uk.ac.ukc.iscream.componentmanager.*; |
7 |
|
8 |
/** |
9 |
* This class monitors a given Queue at regular intervals, |
10 |
* reporting back to a Queue with XML - this could be the |
11 |
* same Queue being monitored. |
12 |
* |
13 |
* @author $Author$ |
14 |
* @version $Id$ |
15 |
*/ |
16 |
class QueueMonitor extends Thread { |
17 |
|
18 |
//---FINAL ATTRIBUTES--- |
19 |
|
20 |
/** |
21 |
* The current CVS revision of this class |
22 |
*/ |
23 |
public static final String REVISION = "$Revision: 1.1 $"; |
24 |
|
25 |
//---STATIC METHODS--- |
26 |
|
27 |
//---CONSTRUCTORS--- |
28 |
|
29 |
/** |
30 |
* Construct a new QueueMonitor. |
31 |
* |
32 |
* @param sourceQueue The Queue to monitor |
33 |
* @param destQueue The Queue to monitor to |
34 |
* @param interval The interval, in milliseconds, at which to sample |
35 |
* @param name A name to identify the source Queue with |
36 |
*/ |
37 |
public QueueMonitor (Queue sourceQueue, Queue destQueue, long interval, String name) { |
38 |
_sourceQueue = sourceQueue; |
39 |
_destQueue = destQueue; |
40 |
_interval = interval; |
41 |
_srcName = name; |
42 |
} |
43 |
|
44 |
//---PUBLIC METHODS--- |
45 |
|
46 |
/** |
47 |
* Loops continuosly polling our source Queue at |
48 |
* the given interval, and then logging the results |
49 |
* in the destination Queue. |
50 |
*/ |
51 |
public void run() { |
52 |
_run = true; |
53 |
// so we can stop this thread... |
54 |
while(_run) { |
55 |
// first sleep for our interval |
56 |
try { Thread.sleep(_interval); } catch(Exception e) {} |
57 |
// check the Queue |
58 |
String status = _sourceQueue.xmlStatus(); |
59 |
// create some XML |
60 |
String date = new Long(System.currentTimeMillis()/((long) 1000)).toString(); |
61 |
String xml = "<packet type=\"queueStat\" date=\""+date+"\" name=\""+_srcName+"\">" + status + "</packet>"; |
62 |
// write XML to destination Queue |
63 |
_destQueue.add(xml); |
64 |
} |
65 |
} |
66 |
|
67 |
/** |
68 |
* Shuts down this QueueMonitor |
69 |
*/ |
70 |
public void shutdown() { |
71 |
// this will stop the main loop |
72 |
_run = false; |
73 |
} |
74 |
|
75 |
/** |
76 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
77 |
* method to provide clean logging (every class should have this). |
78 |
* |
79 |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
80 |
* to format the toString() |
81 |
* |
82 |
* @return the name of this class and its CVS revision |
83 |
*/ |
84 |
public String toString() { |
85 |
return FormatName.getName( |
86 |
_name, |
87 |
getClass().getName(), |
88 |
REVISION); |
89 |
} |
90 |
|
91 |
//---PRIVATE METHODS--- |
92 |
|
93 |
//---ACCESSOR/MUTATOR METHODS--- |
94 |
|
95 |
//---ATTRIBUTES--- |
96 |
|
97 |
/** |
98 |
* This is the friendly identifier of the |
99 |
* component this class is running in. |
100 |
* eg, a Filter may be called "filter1", |
101 |
* If this class does not have an owning |
102 |
* component, a name from the configuration |
103 |
* can be placed here. This name could also |
104 |
* be changed to null for utility classes. |
105 |
*/ |
106 |
private String _name = null; |
107 |
|
108 |
/** |
109 |
* The Queue we're monitoring. |
110 |
*/ |
111 |
private Queue _sourceQueue; |
112 |
|
113 |
/** |
114 |
* The Queue we'll output results to. |
115 |
*/ |
116 |
private Queue _destQueue; |
117 |
|
118 |
/** |
119 |
* The interval at which we'll check the |
120 |
* sourceQueue status. |
121 |
*/ |
122 |
private long _interval; |
123 |
|
124 |
/** |
125 |
* The name to identify the source Queue |
126 |
*/ |
127 |
private String _srcName; |
128 |
|
129 |
/** |
130 |
* Allows us to stop the main loop cleanly. |
131 |
*/ |
132 |
private boolean _run; |
133 |
|
134 |
//---STATIC ATTRIBUTES--- |
135 |
|
136 |
} |