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