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