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 which takes only the path of the queue and set umask and
granularity to default values.
|
QueueSimple(java.lang.String queuePath,
int umask)
Constructor which takes the path of the directory queue, its granularity
option and the umask the created folder.
|
QueueSimple(java.lang.String queuePath,
int umask,
int granularity)
Constructor which takes the path of the directory queue, its granularity
option and the umask the created folder.
|
| 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()
Return the granularity value.
|
java.lang.String |
getId() |
java.lang.String |
getPath(java.lang.String name)
Return the path given the name of the element.
|
java.lang.String |
getQueuePath() |
java.util.Iterator<java.lang.String> |
iterator() |
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(int 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(int maxTemp,
int 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.util.Map<java.lang.String,java.lang.Integer> options)
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.
|
void |
setGranularity(int granularity)
Set the granularity property.
|
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.IOExceptionpublic QueueSimple(java.lang.String queuePath,
int umask)
throws java.io.IOException
queuePath - the path of the directory queueumask - umask the umask value to be set during folder creationjava.io.IOExceptionpublic QueueSimple(java.lang.String queuePath,
int umask,
int granularity)
throws java.io.IOException
queuePath - the path of the directory queueumask - the umask value to be set during folder creationgranularity - the granularity of the directory queuejava.io.IOExceptionpublic int getGranularity()
public void setGranularity(int granularity)
granularity - value to be set as granularitypublic java.lang.String getQueuePath()
public java.lang.String add(byte[] data)
throws java.io.IOException
Queuepublic java.lang.String add(java.lang.String data)
throws java.io.IOException
Queuepublic java.lang.String addPath(java.lang.String path)
throws java.io.IOException
Queuepublic java.lang.String get(java.lang.String name)
Queuepublic byte[] getAsByteArray(java.lang.String name)
QueuegetAsByteArray in interface Queuename - the name of the element to be returnedpublic java.lang.String getPath(java.lang.String name)
Queuepublic boolean lock(java.lang.String name)
throws java.io.IOException
Queuepublic boolean lock(java.lang.String name,
boolean permissive)
throws java.io.IOException
Queuepublic boolean unlock(java.lang.String name)
throws java.io.IOException
Queuepublic boolean unlock(java.lang.String name,
boolean permissive)
throws java.io.IOException
Queuepublic void remove(java.lang.String name)
Queuepublic int count()
Queuepublic void purge()
throws java.io.IOException
Queuepublic void purge(java.util.Map<java.lang.String,java.lang.Integer> options)
throws java.io.IOException
Queuepublic void purge(int maxLock)
throws java.io.IOException
Queuepublic void purge(int maxTemp,
int maxLock)
throws java.io.IOException
Queuepurge in interface QueuemaxTemp - maximum time for a temporary element (in seconds, default
300); if set to 0, temporary elements will not be removedmaxLock - maximum time for a locked element (in seconds, default 600);
if set to 0, locked elements will not be unlockedjava.io.IOException - if any file operation failpublic java.util.Iterator<java.lang.String> iterator()
iterator in interface java.lang.Iterable<java.lang.String>