public class QueueSimple extends 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 Pattern |
DIRECTORY_REGEXP |
static Pattern |
ELEMENT_REGEXP |
static String |
LOCKED_SUFFIX |
static String |
TEMPORARY_SUFFIX |
Constructor and Description |
---|
QueueSimple(String queuePath)
Constructor creating a simple directory queue given a path.
|
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.
|
protected String |
addDir() |
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()
Get the granularity.
|
String |
getId()
Return the queue id.
|
int |
getMaxLock()
Get the default maxLock for purge().
|
int |
getMaxTemp()
Get the default maxTemp for purge().
|
String |
getPath(String name)
Return the path given the name of the element.
|
String |
getQueuePath()
Get the queue path.
|
int |
getRndHex()
Get the random hexadecimal digit.
|
int |
getUmask()
Get the umask.
|
Iterator<String> |
iterator()
Iterator over QueueSimple implementation.
|
boolean |
lock(String name)
Lock an element in permissive mode.
|
boolean |
lock(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(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(Integer maxLock,
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(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(String name)
Unlock an element in non-permissive mode.
|
boolean |
unlock(String name,
boolean permissive)
Unlock an element.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEach, spliterator
public static final String TEMPORARY_SUFFIX
public static final String LOCKED_SUFFIX
public static final Pattern DIRECTORY_REGEXP
public static final Pattern ELEMENT_REGEXP
public QueueSimple(String queuePath) throws IOException
queuePath
- the path of the directory queueIOException
public 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 String add(String data) throws IOException
Queue
add
in interface Queue
data
- data to be added to the queueIOException
- if any file operation failpublic String add(byte[] data) throws IOException
Queue
add
in interface Queue
data
- data to be added to the queueIOException
- if any file operation failpublic String addPath(String path) throws IOException
Queue
addPath
in interface Queue
path
- the path of the file to be addedIOException
- if any file operation failprotected String addDir()
public String get(String name)
Queue
public byte[] getAsByteArray(String name)
Queue
getAsByteArray
in interface Queue
name
- the name of the element to be returnedpublic String getPath(String name)
Queue
public boolean lock(String name) throws IOException
Queue
lock
in interface Queue
name
- name of the element to be lockedtrue
on success, false
if the element
could not be lockedIOException
- if any file operation failpublic boolean lock(String name, boolean permissive) throws IOException
Queue
lock
in interface Queue
name
- name of the element to be lockedpermissive
- work in permissive modetrue
on success, false
if the element
could not be lockedIOException
- if any file operation failpublic boolean unlock(String name) throws IOException
Queue
unlock
in interface Queue
name
- name of the element to be lockedtrue
on success, false
if the element
could not be unlockedIOException
- if any file operation failpublic boolean unlock(String name, boolean permissive) throws IOException
Queue
unlock
in interface Queue
name
- name of the element to be lockedpermissive
- work in permissive modetrue
on success, false
if the element
could not be unlockedIOException
- if any file operation failpublic void remove(String name)
Queue
public int count()
Queue
public void purge() throws IOException
Queue
purge
in interface Queue
IOException
- if any file operation failpublic void purge(Integer maxLock) throws 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 usedIOException
- if any file operation failpublic void purge(Integer maxLock, Integer maxTemp) throws 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 usedIOException
- if any file operation failCopyright © 2015. All rights reserved.