1 |
tdb |
1.9 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
tdb |
1.10 |
* http://www.i-scream.org.uk |
4 |
tdb |
1.9 |
* Copyright (C) 2000-2002 i-scream |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2 |
9 |
|
|
* of the License, or (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
22 |
tdb |
1.11 |
package uk.org.iscream.cms.util; |
23 |
tdb |
1.1 |
|
24 |
|
|
//---IMPORTS--- |
25 |
tdb |
1.11 |
//import uk.org.iscream.cms.util.*; |
26 |
tdb |
1.1 |
|
27 |
|
|
/** |
28 |
|
|
* This class monitors a given Queue at regular intervals, |
29 |
|
|
* reporting back to a Queue with XML - this could be the |
30 |
|
|
* same Queue being monitored. |
31 |
|
|
* |
32 |
tdb |
1.9 |
* @author $Author: tdb $ |
33 |
tdb |
1.11 |
* @version $Id: QueueMonitor.java,v 1.10 2002/05/21 16:47:19 tdb Exp $ |
34 |
tdb |
1.1 |
*/ |
35 |
|
|
class QueueMonitor extends Thread { |
36 |
|
|
|
37 |
|
|
//---FINAL ATTRIBUTES--- |
38 |
|
|
|
39 |
|
|
/** |
40 |
|
|
* The current CVS revision of this class |
41 |
|
|
*/ |
42 |
tdb |
1.11 |
public static final String REVISION = "$Revision: 1.10 $"; |
43 |
tdb |
1.1 |
|
44 |
|
|
//---STATIC METHODS--- |
45 |
|
|
|
46 |
|
|
//---CONSTRUCTORS--- |
47 |
|
|
|
48 |
|
|
/** |
49 |
|
|
* Construct a new QueueMonitor. |
50 |
|
|
* |
51 |
|
|
* @param sourceQueue The Queue to monitor |
52 |
|
|
* @param destQueue The Queue to monitor to |
53 |
|
|
* @param interval The interval, in milliseconds, at which to sample |
54 |
|
|
* @param name A name to identify the source Queue with |
55 |
|
|
*/ |
56 |
|
|
public QueueMonitor (Queue sourceQueue, Queue destQueue, long interval, String name) { |
57 |
tdb |
1.6 |
// set the Thread name |
58 |
|
|
setName("util.QueueMonitor"); |
59 |
|
|
|
60 |
tdb |
1.1 |
_sourceQueue = sourceQueue; |
61 |
|
|
_destQueue = destQueue; |
62 |
|
|
_interval = interval; |
63 |
|
|
_srcName = name; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
//---PUBLIC METHODS--- |
67 |
|
|
|
68 |
|
|
/** |
69 |
|
|
* Loops continuosly polling our source Queue at |
70 |
|
|
* the given interval, and then logging the results |
71 |
|
|
* in the destination Queue. |
72 |
|
|
*/ |
73 |
|
|
public void run() { |
74 |
|
|
_run = true; |
75 |
|
|
// so we can stop this thread... |
76 |
|
|
while(_run) { |
77 |
|
|
// first sleep for our interval |
78 |
|
|
try { Thread.sleep(_interval); } catch(Exception e) {} |
79 |
|
|
// check the Queue |
80 |
|
|
String status = _sourceQueue.xmlStatus(); |
81 |
tdb |
1.4 |
// get a hash of our Queue (for identification) |
82 |
|
|
String hashCode = String.valueOf(_sourceQueue.hashCode()); |
83 |
tdb |
1.1 |
// create some XML |
84 |
|
|
String date = new Long(System.currentTimeMillis()/((long) 1000)).toString(); |
85 |
tdb |
1.4 |
String xml = "<packet type=\"queueStat\" date=\""+date+"\" name=\""+_srcName+"\" hashCode=\""+hashCode+"\">" + status + "</packet>"; |
86 |
tdb |
1.1 |
// write XML to destination Queue |
87 |
|
|
_destQueue.add(xml); |
88 |
|
|
} |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
/** |
92 |
|
|
* Shuts down this QueueMonitor |
93 |
|
|
*/ |
94 |
|
|
public void shutdown() { |
95 |
|
|
// this will stop the main loop |
96 |
|
|
_run = false; |
97 |
tdb |
1.5 |
// get a hash of our Queue (for identification) |
98 |
|
|
String hashCode = String.valueOf(_sourceQueue.hashCode()); |
99 |
tdb |
1.3 |
// create some XML |
100 |
|
|
String date = new Long(System.currentTimeMillis()/((long) 1000)).toString(); |
101 |
tdb |
1.5 |
String xml = "<packet type=\"queueStat\" date=\""+date+"\" name=\""+_srcName+"\" hashCode=\""+hashCode+"\" shutdown=\"true\"></packet>"; |
102 |
tdb |
1.3 |
// write XML to destination Queue |
103 |
|
|
_destQueue.add(xml); |
104 |
tdb |
1.1 |
} |
105 |
|
|
|
106 |
|
|
/** |
107 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
108 |
|
|
* method to provide clean logging (every class should have this). |
109 |
|
|
* |
110 |
tdb |
1.8 |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
111 |
tdb |
1.1 |
* to format the toString() |
112 |
|
|
* |
113 |
|
|
* @return the name of this class and its CVS revision |
114 |
|
|
*/ |
115 |
|
|
public String toString() { |
116 |
|
|
return FormatName.getName( |
117 |
|
|
_name, |
118 |
|
|
getClass().getName(), |
119 |
|
|
REVISION); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
//---PRIVATE METHODS--- |
123 |
|
|
|
124 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
125 |
|
|
|
126 |
|
|
//---ATTRIBUTES--- |
127 |
|
|
|
128 |
|
|
/** |
129 |
|
|
* This is the friendly identifier of the |
130 |
|
|
* component this class is running in. |
131 |
|
|
* eg, a Filter may be called "filter1", |
132 |
|
|
* If this class does not have an owning |
133 |
|
|
* component, a name from the configuration |
134 |
|
|
* can be placed here. This name could also |
135 |
|
|
* be changed to null for utility classes. |
136 |
|
|
*/ |
137 |
|
|
private String _name = null; |
138 |
|
|
|
139 |
|
|
/** |
140 |
|
|
* The Queue we're monitoring. |
141 |
|
|
*/ |
142 |
|
|
private Queue _sourceQueue; |
143 |
|
|
|
144 |
|
|
/** |
145 |
|
|
* The Queue we'll output results to. |
146 |
|
|
*/ |
147 |
|
|
private Queue _destQueue; |
148 |
|
|
|
149 |
|
|
/** |
150 |
|
|
* The interval at which we'll check the |
151 |
|
|
* sourceQueue status. |
152 |
|
|
*/ |
153 |
|
|
private long _interval; |
154 |
|
|
|
155 |
|
|
/** |
156 |
|
|
* The name to identify the source Queue |
157 |
|
|
*/ |
158 |
|
|
private String _srcName; |
159 |
|
|
|
160 |
|
|
/** |
161 |
|
|
* Allows us to stop the main loop cleanly. |
162 |
|
|
*/ |
163 |
|
|
private boolean _run; |
164 |
|
|
|
165 |
|
|
//---STATIC ATTRIBUTES--- |
166 |
|
|
|
167 |
|
|
} |