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.4 2001/02/28 19:02:22 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.4 $"; |
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 |
// get a hash of our Queue (for identification) |
59 |
String hashCode = String.valueOf(_sourceQueue.hashCode()); |
60 |
// create some XML |
61 |
String date = new Long(System.currentTimeMillis()/((long) 1000)).toString(); |
62 |
String xml = "<packet type=\"queueStat\" date=\""+date+"\" name=\""+_srcName+"\" hashCode=\""+hashCode+"\">" + status + "</packet>"; |
63 |
// write XML to destination Queue |
64 |
_destQueue.add(xml); |
65 |
} |
66 |
} |
67 |
|
68 |
/** |
69 |
* Shuts down this QueueMonitor |
70 |
*/ |
71 |
public void shutdown() { |
72 |
// this will stop the main loop |
73 |
_run = false; |
74 |
// create some XML |
75 |
String date = new Long(System.currentTimeMillis()/((long) 1000)).toString(); |
76 |
String xml = "<packet type=\"queueStat\" date=\""+date+"\" name=\""+_srcName+"\" shutdown=\"true\"></packet>"; |
77 |
// write XML to destination Queue |
78 |
_destQueue.add(xml); |
79 |
} |
80 |
|
81 |
/** |
82 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
83 |
* method to provide clean logging (every class should have this). |
84 |
* |
85 |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
86 |
* to format the toString() |
87 |
* |
88 |
* @return the name of this class and its CVS revision |
89 |
*/ |
90 |
public String toString() { |
91 |
return FormatName.getName( |
92 |
_name, |
93 |
getClass().getName(), |
94 |
REVISION); |
95 |
} |
96 |
|
97 |
//---PRIVATE METHODS--- |
98 |
|
99 |
//---ACCESSOR/MUTATOR METHODS--- |
100 |
|
101 |
//---ATTRIBUTES--- |
102 |
|
103 |
/** |
104 |
* This is the friendly identifier of the |
105 |
* component this class is running in. |
106 |
* eg, a Filter may be called "filter1", |
107 |
* If this class does not have an owning |
108 |
* component, a name from the configuration |
109 |
* can be placed here. This name could also |
110 |
* be changed to null for utility classes. |
111 |
*/ |
112 |
private String _name = null; |
113 |
|
114 |
/** |
115 |
* The Queue we're monitoring. |
116 |
*/ |
117 |
private Queue _sourceQueue; |
118 |
|
119 |
/** |
120 |
* The Queue we'll output results to. |
121 |
*/ |
122 |
private Queue _destQueue; |
123 |
|
124 |
/** |
125 |
* The interval at which we'll check the |
126 |
* sourceQueue status. |
127 |
*/ |
128 |
private long _interval; |
129 |
|
130 |
/** |
131 |
* The name to identify the source Queue |
132 |
*/ |
133 |
private String _srcName; |
134 |
|
135 |
/** |
136 |
* Allows us to stop the main loop cleanly. |
137 |
*/ |
138 |
private boolean _run; |
139 |
|
140 |
//---STATIC ATTRIBUTES--- |
141 |
|
142 |
} |