java.lang.Objectcom.gaborcselle.persistent.PersistentQueue<E>
public class PersistentQueue<E extends java.io.Serializable>
A concurrent persistent queue. It keeps a copy of its status in a file on disk which is updated every time the queue contents are modified. Therefore, the data in the queue can survive program or system crashes. The name of the status file is given when calling the constructor.
The type of elements held in the queue are determined by the type parameter
E of this class. The type E has to extend the
java.io.Serializable interface so that the entries can be
written to the file underlying each instance.
Defragmentation: When the first element of the queue is deleted, not
the entire file is written. Instead, a PersistentQueueDeleteMarker
is appended to the end of the file to signal that the first element has been
deleted. However, after some number of remove operations (by default, this is 50,
but that can be changed via an optional parameter at instantiation), the entire
file is defragmented: a temporary file is written with all contents of the
queue. It is then renamed to match the name of the original file. The name of
the temporary file is the original filename plus '.temp'.
More information can be found at the author's website.
| Constructor Summary | |
|---|---|
PersistentQueue(java.lang.String filename)
Create a persistent queue. |
|
PersistentQueue(java.lang.String filename,
int defragmentInterval)
Create a persistent queue. |
|
| Method Summary | |
|---|---|
void |
add(E element)
Adds an element to the tail of the queue. |
void |
clear()
Clears the entire queue and forces the underlying file to be rewritten. |
boolean |
isEmpty()
Returns true if the queue contains no elements. |
E |
peek()
Retrieves, but does not remove, the head of this queue, returning null if this queue is empty. |
E |
remove()
Removes and returns the head element of the persistent queue. |
int |
size()
Returns the number of elements in this queue. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public PersistentQueue(java.lang.String filename)
throws java.io.IOException
filename - the file to use for keeping the persistent state
java.io.IOException - if an I/O error occurs
public PersistentQueue(java.lang.String filename,
int defragmentInterval)
throws java.io.IOException
filename.
filename - filename the file to use for keeping the persistent statedefragmentInterval - number of deletes after which defragment operation should start
java.io.IOException - if an I/O error occurs| Method Detail |
|---|
public void clear()
throws java.io.IOException
java.io.IOException - if an I/O error occurspublic boolean isEmpty()
public int size()
public E peek()
null if this queue is empty.
public E remove()
throws java.io.IOException
null if queue is empty.
java.io.IOException - if an I/O error occurs
public void add(E element)
throws java.io.IOException
element - the element to add
java.io.IOException - if an I/O error occurs