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);
}
.tmp
suffix.
.lck
suffix.
Queue
for general information about
directory queues.Modifier and Type | Class and Description |
---|---|
private static class |
QueueSimple.DotElementFF
FileFilter class to iterate over temporary or locked elements.
|
private static class |
QueueSimple.ElementFF
FileFilter class to iterate over (normal) elements.
|
private static class |
QueueSimple.IntermediateDirectoryFF
FileFilter class to iterate over intermediate directories.
|
private static class |
QueueSimple.QueueSimpleIterator
Iterator for the simple directory queue (private).
|
Modifier and Type | Field and Description |
---|---|
private static int |
DEFAULT_GRANULARITY |
private static int |
DEFAULT_MAXLOCK |
private static int |
DEFAULT_MAXTEMP |
static java.util.regex.Pattern |
DIRECTORY_REGEXP |
private java.util.Set<java.nio.file.attribute.PosixFilePermission> |
directoryPermissions |
private static java.io.FileFilter |
DOT_ELEMENT_FF |
private static java.io.FileFilter |
ELEMENT_FF |
static java.util.regex.Pattern |
ELEMENT_REGEXP |
private java.util.Set<java.nio.file.attribute.PosixFilePermission> |
filePermissions |
private int |
granularity |
private static java.io.FileFilter |
INTERMEDIATE_DIRECTORY_FF |
static java.lang.String |
LOCKED_SUFFIX |
private static org.slf4j.Logger |
logger |
private static int |
MAX_DIRECTORY_UMASK |
private static int |
MAX_FILE_UMASK |
private static long |
MAX_MICRO |
private static int |
MAX_RNDHEX |
private static int |
MAX_UMASK |
private static long |
NANO2MICRO |
private int |
qMaxLock |
private int |
qMaxTemp |
private java.lang.String |
queueId |
private java.lang.String |
queuePath |
private static java.util.Random |
rand |
private int |
rndHex |
private static long |
SECOND |
static java.lang.String |
TEMPORARY_SUFFIX |
private int |
umask |
Constructor and Description |
---|
QueueSimple(java.lang.String path)
Constructor creating a simple directory queue from the given path.
|
QueueSimple(java.lang.String path,
int numask)
Constructor creating a simple directory queue from the given path and umask.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
add(byte[] data)
Add byte array data to the queue.
|
java.lang.String |
add(java.lang.String data)
Add String data to the queue.
|
private java.nio.file.Path |
addDataHelper(java.lang.String dir,
byte[] data) |
private java.nio.file.Path |
addDataHelper(java.lang.String dir,
java.lang.String data) |
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.
|
private java.lang.String |
addPathHelper(java.nio.file.Path tmp,
java.lang.String dir) |
int |
count()
Return the number of elements in the queue.
|
private java.nio.file.Path |
createFile(java.lang.String path) |
private java.lang.String |
directoryName() |
private static java.util.Set<java.nio.file.attribute.PosixFilePermission> |
directoryPerms(int numask) |
private static java.lang.String |
elementName(int rnd) |
private void |
ensureDirectory(java.nio.file.Path path) |
private static java.util.Set<java.nio.file.attribute.PosixFilePermission> |
filePerms(int numask) |
java.lang.String |
get(java.lang.String name)
Get the given locked element as String data.
|
byte[] |
getAsByteArray(java.lang.String name)
Get the given locked element as byte array data.
|
int |
getGranularity()
Get the granularity.
|
java.lang.String |
getId()
Return a unique identifier for the queue.
|
int |
getMaxLock()
Get the default maxLock for purge().
|
int |
getMaxTemp()
Get the default maxTemp for purge().
|
private java.nio.file.Path |
getNewPath(java.lang.String dir) |
java.lang.String |
getPath(java.lang.String name)
Get the path of the given locked element.
|
java.lang.String |
getQueuePath()
Return the path of the queue.
|
int |
getRndHex()
Get the random hexadecimal digit.
|
int |
getUmask()
Get the umask.
|
java.util.Iterator<java.lang.String> |
iterator()
Iterator for the simple directory queue.
|
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 maxLock,
int 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 value)
Set the granularity.
|
QueueSimple |
setMaxLock(int value)
Set the default maxLock for purge().
|
QueueSimple |
setMaxTemp(int value)
Set the default maxTemp for purge().
|
QueueSimple |
setRndHex(int value)
Set the random hexadecimal digit.
|
QueueSimple |
setUmask(int value)
Set the umask.
|
private boolean |
touchFile(java.io.File file) |
boolean |
unlock(java.lang.String name)
Unlock an element in non-permissive mode.
|
boolean |
unlock(java.lang.String name,
boolean permissive)
Unlock an element.
|
private static final org.slf4j.Logger logger
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
private static final int DEFAULT_GRANULARITY
private static final int DEFAULT_MAXLOCK
private static final int DEFAULT_MAXTEMP
private static final int MAX_RNDHEX
private static final int MAX_UMASK
private static final int MAX_DIRECTORY_UMASK
private static final int MAX_FILE_UMASK
private static final long SECOND
private static final long NANO2MICRO
private static final long MAX_MICRO
private static final java.io.FileFilter INTERMEDIATE_DIRECTORY_FF
private static final java.io.FileFilter ELEMENT_FF
private static final java.io.FileFilter DOT_ELEMENT_FF
private static java.util.Random rand
private int granularity
private int qMaxLock
private int qMaxTemp
private int rndHex
private int umask
private java.lang.String queueId
private java.lang.String queuePath
private java.util.Set<java.nio.file.attribute.PosixFilePermission> directoryPermissions
private java.util.Set<java.nio.file.attribute.PosixFilePermission> filePermissions
public QueueSimple(java.lang.String path) throws java.io.IOException
path
- path of the directory queuejava.io.IOException
- if any file operation failspublic QueueSimple(java.lang.String path, int numask) throws java.io.IOException
path
- path of the directory queuenumask
- numerical umask of the directory queuejava.io.IOException
- if any file operation failspublic java.lang.String getQueuePath()
Queue
getQueuePath
in interface Queue
public java.lang.String getId()
Queue
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) throws java.io.IOException
Queue
public byte[] getAsByteArray(java.lang.String name) throws java.io.IOException
Queue
getAsByteArray
in interface Queue
name
- name of the element to be retrievedjava.io.IOException
public 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) throws java.io.IOException
Queue
public int count()
Queue
public void purge() throws java.io.IOException
Queue
public void purge(int maxLock) throws java.io.IOException
Queue
public void purge(int maxLock, int 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 failspublic int getGranularity()
public QueueSimple setGranularity(int value)
value
- granularity to be set (in seconds)public int getUmask()
public QueueSimple setUmask(int value)
value
- umask to be set (numerical)public int getMaxLock()
public QueueSimple setMaxLock(int value)
value
- maximum lock time (in seconds)public int getMaxTemp()
public QueueSimple setMaxTemp(int value)
value
- maximum temporary time (in seconds)public int getRndHex()
public QueueSimple setRndHex(int value)
value
- hexadecimal digit to be set (numerical)private static java.util.Set<java.nio.file.attribute.PosixFilePermission> directoryPerms(int numask)
private static java.util.Set<java.nio.file.attribute.PosixFilePermission> filePerms(int numask)
private java.lang.String directoryName()
private static java.lang.String elementName(int rnd)
private java.lang.String addPathHelper(java.nio.file.Path tmp, java.lang.String dir) throws java.io.IOException
java.io.IOException
private java.nio.file.Path createFile(java.lang.String path) throws java.io.IOException
java.io.IOException
private java.nio.file.Path getNewPath(java.lang.String dir) throws java.io.IOException
java.io.IOException
private java.nio.file.Path addDataHelper(java.lang.String dir, byte[] data) throws java.io.IOException
java.io.IOException
private java.nio.file.Path addDataHelper(java.lang.String dir, java.lang.String data) throws java.io.IOException
java.io.IOException
private void ensureDirectory(java.nio.file.Path path) throws java.io.IOException
java.io.IOException
private boolean touchFile(java.io.File file)
public java.util.Iterator<java.lang.String> iterator()
iterator
in interface java.lang.Iterable<java.lang.String>