1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org.uk |
4 |
+ |
* 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 |
|
//---PACKAGE DECLARATION--- |
22 |
< |
package uk.ac.ukc.iscream.util; |
22 |
> |
package uk.org.iscream.cms.util; |
23 |
|
|
24 |
|
//---IMPORTS--- |
25 |
|
import java.util.LinkedList; |
26 |
|
import java.util.NoSuchElementException; |
27 |
< |
import uk.ac.ukc.iscream.util.*; |
27 |
> |
import java.util.Random; |
28 |
> |
//import uk.org.iscream.cms.util.*; |
29 |
|
|
30 |
|
/** |
31 |
|
* A Queue class designed to operate in a multi-threaded environment, with |
45 |
|
*/ |
46 |
|
public static final String REVISION = "$Revision$"; |
47 |
|
|
48 |
+ |
/** |
49 |
+ |
* Pass to constructor to remove a RANDOM item from |
50 |
+ |
* the Queue upon reaching the maximum limit. |
51 |
+ |
*/ |
52 |
+ |
public static final int RANDOM = 0; |
53 |
+ |
|
54 |
+ |
/** |
55 |
+ |
* Pass to constructor to remove the FIRST item from |
56 |
+ |
* the Queue upon reaching the maximum limit. |
57 |
+ |
*/ |
58 |
+ |
public static final int FIRST = 1; |
59 |
+ |
|
60 |
+ |
/** |
61 |
+ |
* Pass to constructor to remove the LAST item from |
62 |
+ |
* the Queue upon reaching the maximum limit. |
63 |
+ |
*/ |
64 |
+ |
public static final int LAST = 2; |
65 |
+ |
|
66 |
+ |
/** |
67 |
+ |
* Pass to constructor to drop the new item upon reaching |
68 |
+ |
* the maximum Queue limit. |
69 |
+ |
*/ |
70 |
+ |
public static final int DROP = 3; |
71 |
+ |
|
72 |
+ |
/** |
73 |
+ |
* To allow opposite lookups. |
74 |
+ |
*/ |
75 |
+ |
public static final String[] algorithms = {"RANDOM", "FIRST", "LAST", "DROP"}; |
76 |
+ |
|
77 |
|
//---STATIC METHODS--- |
78 |
|
|
79 |
|
//---CONSTRUCTORS--- |
80 |
+ |
|
81 |
+ |
/** |
82 |
+ |
* Constructs a new Queue with a maximum size limit on |
83 |
+ |
* any individual queue. This should be used to stop |
84 |
+ |
* conditions where the Queue cannot be guaranteed to |
85 |
+ |
* be emptied as quick as it's filled. |
86 |
+ |
* |
87 |
+ |
* An algorithm will be used to remove data when new data |
88 |
+ |
* arrives. There may be choices of algorithms later on. |
89 |
+ |
* |
90 |
+ |
* @param maxSize the upper limit for a queue |
91 |
+ |
* @param removeAlgorithm the remove algorithm to use upon reaching the maxSize |
92 |
+ |
*/ |
93 |
+ |
public Queue(int maxSize, int removeAlgorithm) { |
94 |
+ |
_maxSize = maxSize; |
95 |
+ |
_removeAlgorithm = removeAlgorithm; |
96 |
+ |
} |
97 |
+ |
|
98 |
+ |
/** |
99 |
+ |
* Constructs a Queue with no maximum size. |
100 |
+ |
*/ |
101 |
+ |
public Queue() { |
102 |
+ |
_maxSize = -1; |
103 |
+ |
} |
104 |
|
|
105 |
|
//---PUBLIC METHODS--- |
106 |
|
|
114 |
|
for(int i=0; i < _lists.size(); i++) { |
115 |
|
// skip over any gaps left in the list |
116 |
|
if(_lists.get(i) != null) { |
117 |
< |
int s = ((LinkedList) _lists.get(i)).size(); |
118 |
< |
synchronized(this) { |
119 |
< |
// add() does the same thing, but this ensures behaviour |
120 |
< |
((LinkedList) _lists.get(i)).addLast(o); |
121 |
< |
} |
122 |
< |
// if the queue was empty before the add it is possible |
123 |
< |
// that a consumer is waiting... so we notify them |
50 |
< |
if (s == 0) { |
51 |
< |
synchronized(((LinkedList) _lists.get(i))) { |
52 |
< |
((LinkedList) _lists.get(i)).notifyAll(); |
117 |
> |
synchronized(((LinkedList) _lists.get(i))) { |
118 |
> |
// get size before adding to the Queue |
119 |
> |
int s = ((LinkedList) _lists.get(i)).size(); |
120 |
> |
// check whether we need to remove an item from the current Queue |
121 |
> |
if(_maxSize!=-1 && s>=_maxSize && _removeAlgorithm!=DROP) { |
122 |
> |
// we need to remove an item |
123 |
> |
removeQueueItem((LinkedList) _lists.get(i)); |
124 |
|
} |
125 |
+ |
// check if we should add (not if Queue full, and using DROP algorithm) |
126 |
+ |
if(!(s>=_maxSize && _removeAlgorithm==DROP)) { |
127 |
+ |
// add the next item, ensuring we lock |
128 |
+ |
synchronized(this) { |
129 |
+ |
// LinkedList.add() does the same thing, but this ensures behaviour |
130 |
+ |
((LinkedList) _lists.get(i)).addLast(o); |
131 |
+ |
} |
132 |
+ |
// if the queue was empty before the add it is possible |
133 |
+ |
// that a consumer is waiting... so we notify them |
134 |
+ |
//synchronized(((LinkedList) _lists.get(i))) { |
135 |
+ |
((LinkedList) _lists.get(i)).notifyAll(); |
136 |
+ |
//} |
137 |
+ |
} |
138 |
|
} |
139 |
|
} |
140 |
|
} |
156 |
|
throw new InvalidQueueException("Requested queue "+queue+" does not exist"); |
157 |
|
} |
158 |
|
// block if the queue is empty |
159 |
< |
if (((LinkedList) _lists.get(queue)).size() == 0) { |
160 |
< |
synchronized(((LinkedList) _lists.get(queue))) { |
159 |
> |
synchronized(((LinkedList) _lists.get(queue))) { |
160 |
> |
if (((LinkedList) _lists.get(queue)).size() == 0) { |
161 |
|
try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {} |
162 |
|
} |
163 |
|
} |
219 |
|
status += "queue"+i+"=\""+((LinkedList) _lists.get(i)).size()+"\" "; |
220 |
|
} |
221 |
|
else { |
222 |
< |
status += "queue"+i+"=\"null\" "; |
222 |
> |
status += "queue"+i+"=\"[deleted]\" "; |
223 |
|
} |
224 |
|
} |
225 |
< |
status += "total=\""+_count+"\"></queue>"; |
225 |
> |
status += "total=\""+_count+"\""; |
226 |
> |
if(_maxSize != -1) { |
227 |
> |
status += " maxSize=\""+_maxSize+"\""; |
228 |
> |
} |
229 |
> |
status += "></queue>"; |
230 |
|
return status; |
231 |
|
} |
232 |
|
|
248 |
|
|
249 |
|
/** |
250 |
|
* Returns the total numer of elements to have passed |
251 |
< |
* through this queue (ie. a counter on the add method). |
251 |
> |
* through this queue. This is essentially a counter |
252 |
> |
* on the add method. |
253 |
|
* |
254 |
|
* @return the element-ometer. |
255 |
|
*/ |
268 |
|
* |
269 |
|
* @return An integer to be passed to the get() method. |
270 |
|
*/ |
271 |
< |
public int getQueue() { |
271 |
> |
public synchronized int getQueue() { |
272 |
|
int pos = -1; |
273 |
|
for(int i=0; i < _lists.size(); i++) { |
274 |
|
if(_lists.get(i) == null) { |
292 |
|
* |
293 |
|
* @param queue The integer identifier for the queue, given by getQueue(). |
294 |
|
*/ |
295 |
< |
public void removeQueue(int queue) { |
295 |
> |
public synchronized void removeQueue(int queue) { |
296 |
|
_lists.set(queue, null); |
297 |
|
} |
298 |
|
|
353 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
354 |
|
* method to provide clean logging (every class should have this). |
355 |
|
* |
356 |
< |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
356 |
> |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
357 |
|
* to format the toString() |
358 |
|
* |
359 |
|
* @return the name of this class and its CVS revision. |
366 |
|
} |
367 |
|
|
368 |
|
//---PRIVATE METHODS--- |
369 |
< |
|
369 |
> |
|
370 |
> |
/** |
371 |
> |
* This method removes an item from a Queue, using a method |
372 |
> |
* requested at construction. |
373 |
> |
* |
374 |
> |
* @param list The LinkedList from which to remove an item. |
375 |
> |
*/ |
376 |
> |
private void removeQueueItem(LinkedList list) { |
377 |
> |
// look at our algorithm |
378 |
> |
// remove a random item from the list |
379 |
> |
if(_removeAlgorithm==RANDOM) { |
380 |
> |
// new Random, with a good seed |
381 |
> |
Random rand = new Random(System.currentTimeMillis()); |
382 |
> |
int i = rand.nextInt(_maxSize); |
383 |
> |
synchronized(this) { |
384 |
> |
list.remove(i); |
385 |
> |
} |
386 |
> |
} |
387 |
> |
// remove the first item from the list |
388 |
> |
else if(_removeAlgorithm==FIRST) { |
389 |
> |
synchronized(this) { |
390 |
> |
list.removeFirst(); |
391 |
> |
} |
392 |
> |
} |
393 |
> |
// remove the last item from the list |
394 |
> |
else if(_removeAlgorithm==LAST) { |
395 |
> |
synchronized(this) { |
396 |
> |
list.removeLast(); |
397 |
> |
} |
398 |
> |
} |
399 |
> |
} |
400 |
> |
|
401 |
|
//---ACCESSOR/MUTATOR METHODS--- |
402 |
|
|
403 |
|
//---ATTRIBUTES--- |
417 |
|
* A reference to our QueueMonitor, if we have one. |
418 |
|
*/ |
419 |
|
private QueueMonitor _queueMon = null; |
420 |
+ |
|
421 |
+ |
/** |
422 |
+ |
* The maximum size of any Queue. |
423 |
+ |
*/ |
424 |
+ |
private int _maxSize = -1; |
425 |
+ |
|
426 |
+ |
/** |
427 |
+ |
* The remove algorithm to use upon a Queue reaching |
428 |
+ |
* it's maximum size. |
429 |
+ |
*/ |
430 |
+ |
private int _removeAlgorithm = -1; |
431 |
|
|
432 |
|
/** |
433 |
|
* This is the friendly identifier of the |