ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/util/uk/org/iscream/cms/util/Queue.java
Revision: 1.23
Committed: Mon Dec 10 22:20:23 2001 UTC (22 years, 4 months ago) by tdb
Branch: MAIN
Branch point for: SERVER_PIRCBOT
Changes since 1.22: +4 -3 lines
Log Message:
Some minor javadoc tweaks. The first sentence is now more obvious to the
javadoc parser.

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2 tdb 1.22 package uk.org.iscream.cms.server.util;
3 tdb 1.1
4     //---IMPORTS---
5     import java.util.LinkedList;
6     import java.util.NoSuchElementException;
7 tdb 1.14 import java.util.Random;
8 tdb 1.22 import uk.org.iscream.cms.server.util.*;
9 tdb 1.1
10     /**
11     * A Queue class designed to operate in a multi-threaded environment, with
12     * added support for multiple "consumer" threads. Also offers blocking on
13     * the get() methods, which ensures the consumer waits until the queue
14     * actually contains some elements.
15     *
16     * @author $Author: tdb1 $
17 tdb 1.23 * @version $Id: Queue.java,v 1.22 2001/05/29 17:02:35 tdb1 Exp $
18 tdb 1.1 */
19 tdb 1.2 public class Queue {
20 tdb 1.1
21     //---FINAL ATTRIBUTES---
22    
23     /**
24     * The current CVS revision of this class
25     */
26 tdb 1.23 public static final String REVISION = "$Revision: 1.22 $";
27 tdb 1.14
28     /**
29     * Pass to constructor to remove a RANDOM item from
30     * the Queue upon reaching the maximum limit.
31     */
32     public static final int RANDOM = 0;
33    
34     /**
35     * Pass to constructor to remove the FIRST item from
36     * the Queue upon reaching the maximum limit.
37     */
38     public static final int FIRST = 1;
39    
40     /**
41     * Pass to constructor to remove the LAST item from
42     * the Queue upon reaching the maximum limit.
43     */
44     public static final int LAST = 2;
45    
46     /**
47     * Pass to constructor to drop the new item upon reaching
48     * the maximum Queue limit.
49     */
50     public static final int DROP = 3;
51 tdb 1.17
52     /**
53     * To allow opposite lookups.
54     */
55     public static final String[] algorithms = {"RANDOM", "FIRST", "LAST", "DROP"};
56 tdb 1.1
57     //---STATIC METHODS---
58    
59     //---CONSTRUCTORS---
60 tdb 1.14
61     /**
62     * Constructs a new Queue with a maximum size limit on
63     * any individual queue. This should be used to stop
64     * conditions where the Queue cannot be guaranteed to
65     * be emptied as quick as it's filled.
66     *
67     * An algorithm will be used to remove data when new data
68     * arrives. There may be choices of algorithms later on.
69     *
70     * @param maxSize the upper limit for a queue
71     * @param removeAlgorithm the remove algorithm to use upon reaching the maxSize
72     */
73     public Queue(int maxSize, int removeAlgorithm) {
74     _maxSize = maxSize;
75     _removeAlgorithm = removeAlgorithm;
76     }
77    
78     /**
79     * Constructs a Queue with no maximum size.
80     */
81     public Queue() {
82     _maxSize = -1;
83     }
84 tdb 1.1
85     //---PUBLIC METHODS---
86    
87     /**
88     * This method adds a given object to every queue. It will notify
89     * any waiting consumers (on an empty queue) during this process.
90     *
91     * @param o An Object to be added to the queues.
92     */
93     public void add(Object o) {
94     for(int i=0; i < _lists.size(); i++) {
95     // skip over any gaps left in the list
96     if(_lists.get(i) != null) {
97 tdb 1.21 synchronized(((LinkedList) _lists.get(i))) {
98     // get size before adding to the Queue
99     int s = ((LinkedList) _lists.get(i)).size();
100     // check whether we need to remove an item from the current Queue
101     if(_maxSize!=-1 && s>=_maxSize && _removeAlgorithm!=DROP) {
102     // we need to remove an item
103     removeQueueItem((LinkedList) _lists.get(i));
104     }
105     // check if we should add (not if Queue full, and using DROP algorithm)
106     if(!(s>=_maxSize && _removeAlgorithm==DROP)) {
107     // add the next item, ensuring we lock
108     synchronized(this) {
109     // LinkedList.add() does the same thing, but this ensures behaviour
110     ((LinkedList) _lists.get(i)).addLast(o);
111     }
112     // if the queue was empty before the add it is possible
113     // that a consumer is waiting... so we notify them
114     //synchronized(((LinkedList) _lists.get(i))) {
115     ((LinkedList) _lists.get(i)).notifyAll();
116     //}
117 tdb 1.14 }
118 tdb 1.1 }
119     }
120     }
121     // we keep track of the total additions for the status() method
122     _count++;
123     }
124    
125     /**
126     * This method returns an object from the front of a given queue.
127     * It will block until data exists in the queue if required.
128     *
129 tdb 1.8 * @param The queue to retrieve data from.
130 tdb 1.1 * @return The object from the front of the queue.
131     * @throws InvalidQueueException if the queue does not exist.
132     */
133     public Object get(int queue) throws InvalidQueueException {
134     // make sure queue exists
135     if (queue >= _lists.size() || _lists.get(queue) == null) {
136     throw new InvalidQueueException("Requested queue "+queue+" does not exist");
137     }
138     // block if the queue is empty
139 tdb 1.18 synchronized(((LinkedList) _lists.get(queue))) {
140     if (((LinkedList) _lists.get(queue)).size() == 0) {
141 tdb 1.1 try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {}
142     }
143     }
144     // get an item, it should never be null due to the blocking above
145     Object o = null;
146     synchronized(this) {
147     try {
148     o = ((LinkedList) _lists.get(queue)).removeFirst();
149     }
150     catch (NoSuchElementException e) {
151     // This should never happen !
152     }
153     }
154     return o;
155     }
156 tdb 1.6
157     /**
158     * This method releases a get() method that's currently
159     * waiting on an empty queue. This was designed for
160     * shutdown() type methods that may have problems closing
161     * if the thread of control is waiting on a queue.
162     *
163 tdb 1.8 * @param queue the queue to release.
164 tdb 1.6 */
165     public void releaseQueue(int queue) {
166     synchronized(((LinkedList) _lists.get(queue))) {
167     ((LinkedList) _lists.get(queue)).notifyAll();
168 tdb 1.7 }
169     }
170    
171     /**
172     * This method erases the contents of a given queue. This
173     * method should be used with care. It can only empty one
174     * internal queue, not all of them. This must be called
175     * multiple times to empty all queues.
176     *
177     * @param queue the queue to empty.
178     */
179     public void clearQueue(int queue) {
180     synchronized(this) {
181     ((LinkedList) _lists.get(queue)).clear();
182 tdb 1.6 }
183     }
184    
185 tdb 1.1 /**
186 tdb 1.9 * This method returns an XML textual status of the queues.
187     * It is merely for observation, and would most likely be
188     * used by a larger "monitoring" component. Information
189     * returned includes the current size of each queue, and
190     * the total items passed through.
191 tdb 1.1 *
192 tdb 1.9 * @return A String message containing status information in XML format
193 tdb 1.1 */
194 tdb 1.9 public String xmlStatus() {
195     String status = "<queue ";
196 tdb 1.1 for(int i=0; i < _lists.size(); i++) {
197     // check for null entries
198     if(_lists.get(i) != null) {
199 tdb 1.9 status += "queue"+i+"=\""+((LinkedList) _lists.get(i)).size()+"\" ";
200 tdb 1.1 }
201     else {
202 tdb 1.13 status += "queue"+i+"=\"[deleted]\" ";
203 tdb 1.1 }
204     }
205 tdb 1.14 status += "total=\""+_count+"\"";
206     if(_maxSize != -1) {
207     status += " maxSize=\""+_maxSize+"\"";
208     }
209 tdb 1.15 status += "></queue>";
210 tdb 1.1 return status;
211 tdb 1.4 }
212    
213     /**
214     * Returns the size of a given queue. A consumer can use
215     * this to see how big their queue is at any given time.
216     * they should use their queue number as the parameter.
217     *
218     * @param queue The queue number to query.
219     * @return the current size of the queue.
220 tdb 1.8 * @throws InvalidQueueException if the queue does not exist.
221 tdb 1.4 */
222     public int queueSize(int queue) throws InvalidQueueException {
223 tdb 1.5 if (queue >= _lists.size() || _lists.get(queue) == null) {
224 tdb 1.4 throw new InvalidQueueException("Requested queue "+queue+" does not exist");
225     }
226 tdb 1.5 return ((LinkedList) _lists.get(queue)).size();
227 tdb 1.4 }
228    
229     /**
230     * Returns the total numer of elements to have passed
231 tdb 1.23 * through this queue. This is essentially a counter
232     * on the add method.
233 tdb 1.4 *
234 tdb 1.8 * @return the element-ometer.
235 tdb 1.4 */
236     public int elementCount() {
237 tdb 1.5 return _count;
238 tdb 1.1 }
239    
240     /**
241     * This method assigns a queue to a consumer. The idea behind
242     * this is to ensure that only 1 consumer can be associated with
243     * a given queue, otherwise the whole "blocking" thing fails
244     * miserably. Queues are created upon requested.
245     *
246     * It is IMPORTANT that removeQueue() is used when the queue is
247     * no longer required.
248     *
249     * @return An integer to be passed to the get() method.
250     */
251 tdb 1.19 public synchronized int getQueue() {
252 tdb 1.1 int pos = -1;
253     for(int i=0; i < _lists.size(); i++) {
254     if(_lists.get(i) == null) {
255     // found a gap, re-use it
256     pos = i;
257     _lists.set(i, new LinkedList());
258     }
259     }
260     if(pos == -1) {
261     //we didn't find a gap, add at end
262     pos = _lists.size();
263     _lists.add(pos, new LinkedList());
264     }
265     return pos;
266     }
267    
268     /**
269     * This method sets a entry to null in the list. This ensures
270     * that it will no longer be added to after it is no longer
271     * required be a consumer.
272     *
273     * @param queue The integer identifier for the queue, given by getQueue().
274     */
275 tdb 1.19 public synchronized void removeQueue(int queue) {
276 tdb 1.1 _lists.set(queue, null);
277     }
278    
279     /**
280 tdb 1.9 * Start a monitor on our own Queue. This will log XML
281     * statistics about our Queue to a given Queue (could be
282     * the one being monitored).
283     *
284     * @param interval The long interval, in milliseconds, at which to take samples
285     * @param destQueue The queue to monitor to
286     * @param name A name to identify this Queue with
287     * @return whether we succeeded
288     */
289     public boolean startMonitor(long interval, Queue destQueue, String name) {
290     if(_queueMon == null) {
291     // start a monitor
292     _queueMon = new QueueMonitor(this, destQueue, interval, name);
293     _queueMon.start();
294     return true;
295     }
296     else {
297     // already have a monitor running
298     return false;
299     }
300     }
301    
302     /**
303     * Start a monitor on our own Queue. This will log XML
304     * statistics about our Queue to this Queue.
305     *
306     * @param interval The long interval, in milliseconds, at which to take samples
307     * @param name A name to identify this Queue with
308     * @return whether we succeeded
309     */
310     public boolean startMonitor(long interval, String name) {
311     return startMonitor(interval, this, name);
312     }
313    
314     /**
315     * Stop a monitor on our Queue if we have on running.
316     *
317     * @return whether we succeeded
318     */
319     public boolean stopMonitor() {
320     if(_queueMon != null) {
321     // stop a monitor
322     _queueMon.shutdown();
323     _queueMon = null;
324     return true;
325     }
326     else {
327     // no monitor running
328     return false;
329     }
330     }
331    
332     /**
333 tdb 1.1 * Overrides the {@link java.lang.Object#toString() Object.toString()}
334     * method to provide clean logging (every class should have this).
335     *
336 tdb 1.22 * This uses the uk.org.iscream.cms.server.util.FormatName class
337 tdb 1.1 * to format the toString()
338     *
339 tdb 1.8 * @return the name of this class and its CVS revision.
340 tdb 1.1 */
341 tdb 1.3 public String toString() {
342 tdb 1.1 return FormatName.getName(
343     _name,
344     getClass().getName(),
345     REVISION);
346 tdb 1.3 }
347 tdb 1.1
348     //---PRIVATE METHODS---
349 tdb 1.14
350     /**
351     * This method removes an item from a Queue, using a method
352     * requested at construction.
353     *
354     * @param list The LinkedList from which to remove an item.
355     */
356     private void removeQueueItem(LinkedList list) {
357     // look at our algorithm
358     // remove a random item from the list
359     if(_removeAlgorithm==RANDOM) {
360     // new Random, with a good seed
361     Random rand = new Random(System.currentTimeMillis());
362     int i = rand.nextInt(_maxSize);
363     synchronized(this) {
364     list.remove(i);
365     }
366     }
367     // remove the first item from the list
368     else if(_removeAlgorithm==FIRST) {
369     synchronized(this) {
370     list.removeFirst();
371     }
372     }
373     // remove the last item from the list
374     else if(_removeAlgorithm==LAST) {
375     synchronized(this) {
376     list.removeLast();
377     }
378     }
379     }
380    
381 tdb 1.1 //---ACCESSOR/MUTATOR METHODS---
382    
383     //---ATTRIBUTES---
384    
385     /**
386     * The LinkedLists of queues.
387     */
388     private LinkedList _lists = new LinkedList();
389    
390     /**
391     * A counter so we know how many data items have been
392     * passed through, for statistical purposes.
393     */
394     private int _count = 0;
395 tdb 1.9
396     /**
397     * A reference to our QueueMonitor, if we have one.
398     */
399     private QueueMonitor _queueMon = null;
400 tdb 1.14
401     /**
402     * The maximum size of any Queue.
403     */
404     private int _maxSize = -1;
405    
406     /**
407     * The remove algorithm to use upon a Queue reaching
408     * it's maximum size.
409     */
410     private int _removeAlgorithm = -1;
411 tdb 1.1
412     /**
413     * This is the friendly identifier of the
414     * component this class is running in.
415     * eg, a Filter may be called "filter1",
416     * If this class does not have an owning
417     * component, a name from the configuration
418     * can be placed here. This name could also
419     * be changed to null for utility classes.
420     */
421 tdb 1.3 private String _name = null;
422 tdb 1.1
423     //---STATIC ATTRIBUTES---
424    
425     }