--- projects/cms/source/util/uk/org/iscream/cms/util/Queue.java 2001/01/18 19:13:57 1.4 +++ projects/cms/source/util/uk/org/iscream/cms/util/Queue.java 2001/02/25 23:29:06 1.13 @@ -13,7 +13,7 @@ import uk.ac.ukc.iscream.util.*; * actually contains some elements. * * @author $Author: tdb $ - * @version $Id: Queue.java,v 1.4 2001/01/18 19:13:57 tdb Exp $ + * @version $Id: Queue.java,v 1.13 2001/02/25 23:29:06 tdb Exp $ */ public class Queue { @@ -22,7 +22,7 @@ public class Queue { /** * The current CVS revision of this class */ - public static final String REVISION = "$Revision: 1.4 $"; + public static final String REVISION = "$Revision: 1.13 $"; //---STATIC METHODS--- @@ -62,6 +62,7 @@ public class Queue { * This method returns an object from the front of a given queue. * It will block until data exists in the queue if required. * + * @param The queue to retrieve data from. * @return The object from the front of the queue. * @throws InvalidQueueException if the queue does not exist. */ @@ -88,30 +89,56 @@ public class Queue { } return o; } - + /** - * This method returns a textual status of the queues. It - * is merely for observation, and would most likely be used - * by a larger "monitoring" component. Information returned - * includes the current size of each queue, and the total - * items passed through. + * This method releases a get() method that's currently + * waiting on an empty queue. This was designed for + * shutdown() type methods that may have problems closing + * if the thread of control is waiting on a queue. * - * @return A String message containing status information. + * @param queue the queue to release. */ - public String status() { - String status = ""; + public void releaseQueue(int queue) { + synchronized(((LinkedList) _lists.get(queue))) { + ((LinkedList) _lists.get(queue)).notifyAll(); + } + } + + /** + * This method erases the contents of a given queue. This + * method should be used with care. It can only empty one + * internal queue, not all of them. This must be called + * multiple times to empty all queues. + * + * @param queue the queue to empty. + */ + public void clearQueue(int queue) { + synchronized(this) { + ((LinkedList) _lists.get(queue)).clear(); + } + } + + /** + * This method returns an XML textual status of the queues. + * It is merely for observation, and would most likely be + * used by a larger "monitoring" component. Information + * returned includes the current size of each queue, and + * the total items passed through. + * + * @return A String message containing status information in XML format + */ + public String xmlStatus() { + String status = ""; return status; } @@ -122,22 +149,23 @@ public class Queue { * * @param queue The queue number to query. * @return the current size of the queue. + * @throws InvalidQueueException if the queue does not exist. */ public int queueSize(int queue) throws InvalidQueueException { - if (queue >= _lists.size() || _lists.get(queue) == null) { + if (queue >= _lists.size() || _lists.get(queue) == null) { throw new InvalidQueueException("Requested queue "+queue+" does not exist"); } - return ((LinkedList) _lists.get(queue)).size(); + return ((LinkedList) _lists.get(queue)).size(); } /** * Returns the total numer of elements to have passed * through this queue (ie. a counter on the add method). * - * @return the element-ometer + * @return the element-ometer. */ public int elementCount() { - return _count; + return _count; } /** @@ -180,13 +208,66 @@ public class Queue { } /** + * Start a monitor on our own Queue. This will log XML + * statistics about our Queue to a given Queue (could be + * the one being monitored). + * + * @param interval The long interval, in milliseconds, at which to take samples + * @param destQueue The queue to monitor to + * @param name A name to identify this Queue with + * @return whether we succeeded + */ + public boolean startMonitor(long interval, Queue destQueue, String name) { + if(_queueMon == null) { + // start a monitor + _queueMon = new QueueMonitor(this, destQueue, interval, name); + _queueMon.start(); + return true; + } + else { + // already have a monitor running + return false; + } + } + + /** + * Start a monitor on our own Queue. This will log XML + * statistics about our Queue to this Queue. + * + * @param interval The long interval, in milliseconds, at which to take samples + * @param name A name to identify this Queue with + * @return whether we succeeded + */ + public boolean startMonitor(long interval, String name) { + return startMonitor(interval, this, name); + } + + /** + * Stop a monitor on our Queue if we have on running. + * + * @return whether we succeeded + */ + public boolean stopMonitor() { + if(_queueMon != null) { + // stop a monitor + _queueMon.shutdown(); + _queueMon = null; + return true; + } + else { + // no monitor running + return false; + } + } + + /** * Overrides the {@link java.lang.Object#toString() Object.toString()} * method to provide clean logging (every class should have this). * * This uses the uk.ac.ukc.iscream.util.FormatName class * to format the toString() * - * @return the name of this class and its CVS revision + * @return the name of this class and its CVS revision. */ public String toString() { return FormatName.getName( @@ -211,6 +292,11 @@ public class Queue { * passed through, for statistical purposes. */ private int _count = 0; + + /** + * A reference to our QueueMonitor, if we have one. + */ + private QueueMonitor _queueMon = null; /** * This is the friendly identifier of the