xmlBlaster 2.2.0 API

org.xmlBlaster.util
Class SimpleXbQueue

java.lang.Object
  extended by org.xmlBlaster.util.SimpleXbQueue

public class SimpleXbQueue
extends java.lang.Object

This queue (FIFO) implementation may serve as your base class or as a member variable.
It is based on a linked list.

Example:

   int size = 3;
   Queue queue = new Queue("Test", size);
   queue.setModeToDiscardOldest(); // if queue is full, oldest entry is removed
   try {
       queue.push("Hello ");
       queue.push("world.");
   }
   catch (XmlBlasterException e) {
   }
   System.out.println(queue.pull()); // prints "Hello "
   System.out.println(queue.pull()); // prints "world."
 
TODO: Allow persistence store e.g. via JDBC bridge into Oracle with some smart caching

Version:
$Revision: 1.8 $
Author:
ruff@swand.lake.de

Field Summary
private  boolean discard
          Throw the message away if queue is full - the message is silently lost!
private  boolean discardOldest
          Default is false, and you get an Exception if queue is full
private  int MAX_ENTRIES
           
private  java.lang.String name
           
private  long numLost
           
private  java.util.LinkedList queueList
          The queue is implemented with a linked list.
 
Constructor Summary
SimpleXbQueue(java.lang.String name, int maxEntries)
          Constructs an empty FIFO queue.

 
Method Summary
 long getNumLost()
          Counter for lost messages in 'discard' or 'discardOldest' mode
private  void init()
          Allocates a new LinkedList.
 boolean isFull()
          Check if the queue is filled up.

static void main(java.lang.String[] args)
          This method is for testing only.

To start this test type:
java org.xmlBlaster.util.SimpleXbQueue

 java.lang.Object pull()
          Pull the next unit from the end of the queue.

 void push(java.lang.Object obj)
          Add a unit to the beginning of the queue.

 java.util.ListIterator queueIterator(int index)
          Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.
 void setModeToDiscard()
          Default is that an Exception is thrown if the queue is full.
 void setModeToDiscardOldest()
          Default is that an Exception is thrown if the queue is full.
 int size()
          How many objects are in the queue.

 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

name

private java.lang.String name

MAX_ENTRIES

private final int MAX_ENTRIES

discardOldest

private boolean discardOldest
Default is false, and you get an Exception if queue is full


discard

private boolean discard
Throw the message away if queue is full - the message is silently lost!


numLost

private long numLost

queueList

private java.util.LinkedList queueList
The queue is implemented with a linked list. I believe ArrayList is more expensive in this case

Constructor Detail

SimpleXbQueue

public SimpleXbQueue(java.lang.String name,
                     int maxEntries)
Constructs an empty FIFO queue.

Parameters:
name - A nice name, for error reporting only.
maxEntries - The maximum number of nodes for this queue.
Method Detail

init

private void init()
Allocates a new LinkedList.


isFull

public final boolean isFull()
Check if the queue is filled up.

Returns:
'true' if space for at least on more entry does exist, 'false' if quota is exceeded.

pull

public final java.lang.Object pull()
Pull the next unit from the end of the queue.

Returns:
The last object of the queue or null if no element is in the queue.

push

public final void push(java.lang.Object obj)
                throws XmlBlasterException
Add a unit to the beginning of the queue.

Parameters:
obj - The object which should be added to the beginning of the queue.
Throws:
JUtilsException - This exception is thrown of no more space is available.
XmlBlasterException

queueIterator

public final java.util.ListIterator queueIterator(int index)
                                           throws java.lang.IndexOutOfBoundsException
Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.

TODO: Discuss thread safety when using this list.

Parameters:
index - Position of the first element to be returned from the list-iterator (by a call to next).
Returns:
A ListIterator of the elements in this list (in proper sequence), starting at the specified position in the list.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range (index < 0 || index > size()).

setModeToDiscardOldest

public void setModeToDiscardOldest()
Default is that an Exception is thrown if the queue is full. Calling this method changes the behavior that if the queue is filled, the supernumerary entries are falling out at the end and are lost.


setModeToDiscard

public void setModeToDiscard()
Default is that an Exception is thrown if the queue is full. Calling this method changes the behavior that if the queue is filled, the supernumerary entries are falling out at the end and are lost.


getNumLost

public long getNumLost()
Counter for lost messages in 'discard' or 'discardOldest' mode


size

public final int size()
How many objects are in the queue.

Returns:
The number of objects in this queue.

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
This method is for testing only.

To start this test type:
java org.xmlBlaster.util.SimpleXbQueue

Throws:
java.lang.Exception

xmlBlaster 2.2.0 API

Copyright © 1999-2014 The xmlBlaster.org contributers.