ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/util/uk/org/iscream/cms/util/QueueMonitor.java
Revision: 1.6
Committed: Tue Mar 13 02:19:49 2001 UTC (23 years, 1 month ago) by tdb
Branch: MAIN
Changes since 1.5: +5 -2 lines
Log Message:
Given all the classes that extend Thread a name using Thread.setName(). It is
only representative as far as it will tell us which class the Thread is, but
this will go some way to aiding debugging. If time permitted, more effort could
be taken to name each thread according to what it was dealing with.

File Contents

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