public class QueueSimple extends Queue
A port of Perl module Directory::Queue::Simple http://search.cpan.org/~lcons/Directory-Queue/
The documentation from Directory::Queue::Simple module was adapted for Java.
// 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);
}
This module is very similar to normal dirq, but uses a different way to store data in the filesystem, using less directories. Its API is almost identical.
Compared to normal dirq, this module:
The toplevel directory contains intermediate directories that contain
the stored elements, each of them in a file.
The names of the intermediate directories are time based: the element
insertion time is used to create a 8-digits long hexadecimal number.
The granularity (see the constructor) is used to limit the number of
new directories. For instance, with a granularity of 60 (the default),
new directories will be created at most once per minute.
Since there is usually a filesystem limit in the number of directories a directory can hold, there is a trade-off to be made. If you want to support many added elements per second, you should use a low granularity to keep small directories. However, in this case, you will create many directories and this will limit the total number of elements you can store.
The elements themselves are stored in files (one per element) with a 14-digits long hexadecimal name SSSSSSSSMMMMMR where:
A temporary element (being added to the queue) will have a .tmp suffix.
A locked element will have a hard link with the same name and the .lck suffix.
Please refer to Queue for general information about
directory queues.
| Modifier and Type | Class and Description |
|---|---|
class |
QueueSimple.DirFilter
Used to filter directories while listing files.
|
class |
QueueSimple.QueueSimpleIterator
Iterator over QueueSimple implementation.
|
| Modifier and Type | Field and Description |
|---|---|
static String |
LOCKED_SUFFIX |
static String |
TEMPORARY_SUFFIX |
DirectoryRegexp, ElementRegexp| Constructor and Description |
|---|
QueueSimple(String path)
Constructor which takes only the path of the queue and set umask
and granularity to default values.
|
QueueSimple(String path,
int umask)
Constructor which takes the path of the directory queue,
its granularity option and the umask the created folder.
|
QueueSimple(String path,
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 |
|---|---|
String |
add(byte[] data)
Add data as byte array to the queue.
|
String |
add(String data)
Add data as a string to the queue.
|
String |
addPath(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).
|
String |
get(String name)
Get locked element as a string.
|
byte[] |
getAsByteArray(String name)
Get locked element as a byte array.
|
int |
getGranularity()
Return the granularity value.
|
String |
getPath(String name)
Return the path given the name of the element.
|
Iterator<String> |
iterator()
Return the queue iterator.
|
boolean |
lock(String name,
boolean permissive)
Lock an element.
|
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 |
remove(String name)
Remove a locked element from the queue.
|
void |
setGranularity(int granularity)
Set the granularity property.
|
boolean |
unlock(String name,
boolean permissive)
Unlock an element.
|
public static final String TEMPORARY_SUFFIX
public static final String LOCKED_SUFFIX
public QueueSimple(String path) throws QueueException
path - the path of the directory queueQueueExceptionpublic QueueSimple(String path, int umask) throws QueueException
path - the path of the directory queueumask - umask the umask value to be set during folder creationQueueExceptionpublic QueueSimple(String path, int umask, int granularity) throws QueueException
path - the path of the directory queueumask - the umask value to be set during folder creationgranularity - the granularity of the directory queueQueueExceptionpublic int getGranularity()
public void setGranularity(int granularity)
granularity - value to be set as granularitypublic String add(byte[] data) throws QueueException
Queueadd in class Queuedata - data to be added to the queueQueueExceptionpublic String add(String data) throws QueueException
Queueadd in class Queuedata - data to be added to the queueQueueExceptionpublic String addPath(String path) throws QueueException
QueueaddPath in class Queuepath - the path of the file to be addedQueueExceptionpublic String get(String name)
Queuepublic byte[] getAsByteArray(String name)
QueuegetAsByteArray in class Queuename - the name of the element to be returnedpublic String getPath(String name)
Queuepublic boolean lock(String name, boolean permissive) throws QueueException
Queuelock in class Queuename - name of the element to be lockedpermissive - work in permissive modetrue on success, false if
the element could not be lockedQueueExceptionpublic boolean unlock(String name, boolean permissive) throws QueueException
Queueunlock in class Queuename - name of the element to be lockedpermissive - work in permissive modetrue on success, false if
the element could not be unlockedQueueExceptionpublic void remove(String name)
Queuepublic int count()
Queuepublic void purge(int maxTemp,
int maxLock)
throws QueueException
Queuepurge in class 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 unlockedQueueExceptionCopyright © 2012. All Rights Reserved.