org.apache.excalibur.event.impl
Class DefaultQueue

java.lang.Object
  extended byorg.apache.excalibur.event.impl.AbstractQueue
      extended byorg.apache.excalibur.event.impl.DefaultQueue
All Implemented Interfaces:
Queue, Sink, Source

public final class DefaultQueue
extends AbstractQueue

The default queue implementation is a variable size queue. This queue is thread safe, however the overhead in synchronization costs a few extra milliseconds.

Author:
Avalon Development Team

Field Summary
protected  int m_reserve
           
 
Fields inherited from class org.apache.excalibur.event.impl.AbstractQueue
EMPTY_ARRAY, m_interceptor, m_predicate, m_timeout
 
Fields inherited from interface org.apache.excalibur.event.Queue
ROLE
 
Constructor Summary
DefaultQueue()
          Create an unbounded DefaultQueue.
DefaultQueue(EnqueuePredicate predicate)
           
DefaultQueue(int size)
          Construct a new DefaultQueue with the specified number of elements.
 
Method Summary
 java.lang.Object dequeue()
          Dequeues the next element, or null if there is nothing left on the queue or in case of a timeout while attempting to obtain the mutex
 java.lang.Object[] dequeue(int numElements)
          Dequeues at most num available elements.
 java.lang.Object[] dequeueAll()
          Dequeues all available elements.
 void enqueue(java.lang.Object element)
          Enqueues the given element onto the Sink.
 void enqueue(java.lang.Object[] elements)
          Given an array of elements, atomically enqueues all of the elements in the array.
 int maxSize()
          Return the maximum number of elements that will fit in the Queue.
 PreparedEnqueue prepareEnqueue(java.lang.Object[] elements)
          Support for transactional enqueue.
 int size()
          Return the number of elements currently in the Queue.
 boolean tryEnqueue(java.lang.Object element)
          Tries to enqueue an event, but instead of throwing exceptions, it returns a boolean value of whether the attempt was successful.
 
Methods inherited from class org.apache.excalibur.event.impl.AbstractQueue
block, canAccept, getDequeueInterceptor, getEnqueuePredicate, isFull, setDequeueInterceptor, setEnqueuePredicate, setTimeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_reserve

protected int m_reserve
Constructor Detail

DefaultQueue

public DefaultQueue(int size)
Construct a new DefaultQueue with the specified number of elements. if the number of elements is greater than zero, then the Queue is bounded by that number. Otherwise, the Queue is not bounded at all.

Parameters:
size - The maximum number of elements in the Queue. Any number less than 1 means there is no limit.

DefaultQueue

public DefaultQueue(EnqueuePredicate predicate)

DefaultQueue

public DefaultQueue()
Create an unbounded DefaultQueue.

Method Detail

size

public int size()
Return the number of elements currently in the Queue.

Returns:
int representing the number of elements (including the reserved ones).

maxSize

public int maxSize()
Return the maximum number of elements that will fit in the Queue. A number below 1 indecates an unbounded Queue, which means there is no limit.

Specified by:
maxSize in interface Sink
Overrides:
maxSize in class AbstractQueue
Returns:
int representing the maximum number of elements

prepareEnqueue

public PreparedEnqueue prepareEnqueue(java.lang.Object[] elements)
                               throws SinkException
Description copied from interface: Sink
Support for transactional enqueue.

This method allows a client to provisionally enqueue a number of elements onto the queue, and then later commit the enqueue (with a commitEnqueue call), or abort (with an abortEnqueue call). This mechanism can be used to perform "split-phase" enqueues, where a client first enqueues a set of elements on the queue and then performs some work to "fill in" those elements before performing a commit. This can also be used to perform multi-queue transactional enqueue operations, with an "all-or-nothing" strategy for enqueueing events on multiple Sinks.

This method would generally be used in the following manner:

   PreparedEnqueue enqueue = sink.prepareEnqueue(someElements);
   if (canCommit) {
     enqueue.commit();
   } else {
     enqueue.abort();
   }
 

Note that this method does not protect against "dangling prepares" -- that is, a prepare without an associated commit or abort operation. This method should be used with care. In particular, be sure that all code paths (such as exceptions) after a prepare include either a commit or an abort.

Parameters:
elements - The element array to provisionally enqueue
Returns:
A PreparedEnqueue that may be used to commit or abort the provisional enqueue
Throws:
SinkException
See Also:
PreparedEnqueue

tryEnqueue

public boolean tryEnqueue(java.lang.Object element)
Description copied from interface: Sink
Tries to enqueue an event, but instead of throwing exceptions, it returns a boolean value of whether the attempt was successful.

Parameters:
element - The element to attempt to enqueue
Returns:
true if successful, false if not.

enqueue

public void enqueue(java.lang.Object[] elements)
             throws SinkException
Description copied from interface: Sink
Given an array of elements, atomically enqueues all of the elements in the array. This guarantees that no other thread can interleave its own elements with those being inserted from this array. The implementation must enqueue all of the elements or none of them; if a SinkFullException or SinkClosedException is thrown, none of the elements will have been enqueued.

Parameters:
elements - The element array to enqueue
Throws:
SinkException

enqueue

public void enqueue(java.lang.Object element)
             throws SinkException
Description copied from interface: Sink
Enqueues the given element onto the Sink.

Parameters:
element - The elements to enqueue
Throws:
SinkException

dequeue

public java.lang.Object[] dequeue(int numElements)
Description copied from interface: Source
Dequeues at most num available elements. Returns a zero-sized array in case of a timeout while attempting to obtain the mutex or if there is nothing left on the Source.

Parameters:
numElements - The maximum number of elements to dequeue
Returns:
At most num elements from the Source

dequeueAll

public java.lang.Object[] dequeueAll()
Description copied from interface: Source
Dequeues all available elements. Returns a zero-sized array in case of a timeout while attempting to obtain the mutex or if there is nothing left on the Source.

Returns:
all pending elements on the Source

dequeue

public java.lang.Object dequeue()
Description copied from interface: Source
Dequeues the next element, or null if there is nothing left on the queue or in case of a timeout while attempting to obtain the mutex

Returns:
the next queue element on the Source


Copyright © 1997-2005 The Apache Software Foundation. All Rights Reserved.