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