public class QueueSimple extends java.lang.Object implements Queue
// sample producer
QueueSimple dirq = new QueueSimple("/tmp/test");
for (int i=0; i < 100; i++) {
String name = dirq.add("element " + i);
System.out.println("# added element " + i + " as " + name);
}
// sample consumer
dirq = QueueSimple('/tmp/test');
for (String name:dirq) {
if (! dirq.lock(name)) {
continue;
}
System.out.println("# reading element " + name);
String data = dirq.get(name);
// one could use dirq.unlock(name) to only browse the queue...
dirq.remove(name);
}
Queue
for general information about
directory queues.Modifier and Type | Field and Description |
---|---|
static java.util.regex.Pattern |
DIRECTORY_REGEXP |
static java.util.regex.Pattern |
ELEMENT_REGEXP |
static java.lang.String |
LOCKED_SUFFIX |
static java.lang.String |
TEMPORARY_SUFFIX |
Constructor and Description |
---|
QueueSimple(java.lang.String queuePath)
Constructor creating a simple directory queue given a path.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
add(byte[] data)
Add data as byte array to the queue.
|
java.lang.String |
add(java.lang.String data)
Add data as a string to the queue.
|
java.lang.String |
addPath(java.lang.String path)
Add the given file (identified by its path) to the queue and return the
corresponding element name, the file must be on the same filesystem and
will be moved to the queue.
|
int |
count()
Return the number of elements in the queue, locked or not (but not
temporary).
|
java.lang.String |
get(java.lang.String name)
Get locked element as a string.
|
byte[] |
getAsByteArray(java.lang.String name)
Get locked element as a byte array.
|
int |
getGranularity()
Get the granularity.
|
java.lang.String |
getId()
Return the queue id.
|
int |
getMaxLock()
Get the default maxLock for purge().
|
int |
getMaxTemp()
Get the default maxTemp for purge().
|
java.lang.String |
getPath(java.lang.String name)
Return the path given the name of the element.
|
java.lang.String |
getQueuePath()
Get the queue path.
|
int |
getRndHex()
Get the random hexadecimal digit.
|
int |
getUmask()
Get the umask.
|
java.util.Iterator<java.lang.String> |
iterator()
Iterator over QueueSimple implementation.
|
boolean |
lock(java.lang.String name)
Lock an element in permissive mode.
|
boolean |
lock(java.lang.String name,
boolean permissive)
Lock an element.
|
void |
purge()
Purge the queue by removing unused intermediate directories, removing too
old temporary elements and unlocking too old locked elements (aka staled
locks); note: this can take a long time on queues with many elements.
|
void |
purge(java.lang.Integer maxLock)
Purge the queue by removing unused intermediate directories, removing too
old temporary elements and unlocking too old locked elements (aka staled
locks); note: this can take a long time on queues with many elements.
|
void |
purge(java.lang.Integer maxLock,
java.lang.Integer maxTemp)
Purge the queue by removing unused intermediate directories, removing too
old temporary elements and unlocking too old locked elements (aka staled
locks); note: this can take a long time on queues with many elements.
|
void |
remove(java.lang.String name)
Remove a locked element from the queue.
|
QueueSimple |
setGranularity(int granularity)
Set the granularity.
|
QueueSimple |
setMaxLock(int maxLock)
Set the default maxLock for purge().
|
QueueSimple |
setMaxTemp(int maxTemp)
Set the default maxTemp for purge().
|
QueueSimple |
setRndHex(int rndHex)
Set the random hexadecimal digit.
|
QueueSimple |
setUmask(int umask)
Set the umask.
|
boolean |
unlock(java.lang.String name)
Unlock an element in non-permissive mode.
|
boolean |
unlock(java.lang.String name,
boolean permissive)
Unlock an element.
|
public static final java.lang.String TEMPORARY_SUFFIX
public static final java.lang.String LOCKED_SUFFIX
public static final java.util.regex.Pattern DIRECTORY_REGEXP
public static final java.util.regex.Pattern ELEMENT_REGEXP
public QueueSimple(java.lang.String queuePath) throws java.io.IOException
queuePath
- the path of the directory queuejava.io.IOException
public java.lang.String getId()
Queue
public java.lang.String getQueuePath()
public int getGranularity()
public QueueSimple setGranularity(int granularity)
granularity
- to be set (in seconds)public int getUmask()
public QueueSimple setUmask(int umask)
umask
- to be set (numerical)public int getMaxLock()
public QueueSimple setMaxLock(int maxLock)
maxLock
- maximum lock time (in seconds)public int getMaxTemp()
public QueueSimple setMaxTemp(int maxTemp)
maxTemp
- maximum temporary time (in seconds)public int getRndHex()
public QueueSimple setRndHex(int rndHex)
rndHex
- hexadecimal digit to be set (numerical)public java.lang.String add(java.lang.String data) throws java.io.IOException
Queue
public java.lang.String add(byte[] data) throws java.io.IOException
Queue
public java.lang.String addPath(java.lang.String path) throws java.io.IOException
Queue
public java.lang.String get(java.lang.String name)
Queue
public byte[] getAsByteArray(java.lang.String name)
Queue
getAsByteArray
in interface Queue
name
- the name of the element to be returnedpublic java.lang.String getPath(java.lang.String name)
Queue
public boolean lock(java.lang.String name) throws java.io.IOException
Queue
public boolean lock(java.lang.String name, boolean permissive) throws java.io.IOException
Queue
public boolean unlock(java.lang.String name) throws java.io.IOException
Queue
public boolean unlock(java.lang.String name, boolean permissive) throws java.io.IOException
Queue
public void remove(java.lang.String name)
Queue
public int count()
Queue
public void purge() throws java.io.IOException
Queue
public void purge(java.lang.Integer maxLock) throws java.io.IOException
Queue
public void purge(java.lang.Integer maxLock, java.lang.Integer maxTemp) throws java.io.IOException
Queue
purge
in interface Queue
maxLock
- maximum time for a locked element (in seconds);
if set to 0, locked elements will not be unlocked;
if set to null, the object's default value will be usedmaxTemp
- maximum time for a temporary element (in seconds);
if set to 0, temporary elements will not be removed
if set to null, the object's default value will be usedjava.io.IOException
- if any file operation failpublic java.util.Iterator<java.lang.String> iterator()
iterator
in interface java.lang.Iterable<java.lang.String>