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);
}
.tmp
suffix.
.lck
suffix.
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 path)
Constructor creating a simple directory queue from the given path.
|
QueueSimple(String path,
int numask)
Constructor creating a simple directory queue from the given path and umask.
|
Modifier and Type | Method and Description |
---|---|
String |
add(byte[] data)
Add byte array data to the queue.
|
String |
add(String data)
Add String data 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.
|
String |
get(String name)
Get the given locked element as String data.
|
byte[] |
getAsByteArray(String name)
Get the given locked element as byte array data.
|
int |
getGranularity()
Get the granularity.
|
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().
|
String |
getPath(String name)
Get the path of the given locked element.
|
String |
getQueuePath()
Return the path of the queue.
|
int |
getRndHex()
Get the random hexadecimal digit.
|
int |
getUmask()
Get the umask.
|
Iterator<String> |
iterator()
Iterator for the simple directory queue.
|
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(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(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.
|
boolean |
unlock(String name)
Unlock an element in non-permissive mode.
|
boolean |
unlock(String name,
boolean permissive)
Unlock an element.
|
equals, 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 path) throws IOException
path
- path of the directory queueIOException
- if any file operation failspublic QueueSimple(String path, int numask) throws IOException
path
- path of the directory queuenumask
- numerical umask of the directory queueIOException
- if any file operation failspublic String getQueuePath()
Queue
getQueuePath
in interface Queue
public String getId()
Queue
public String add(String data) throws IOException
Queue
add
in interface Queue
data
- data to be addedIOException
- if any file operation failspublic String add(byte[] data) throws IOException
Queue
add
in interface Queue
data
- data to be addedIOException
- if any file operation failspublic String addPath(String path) throws IOException
Queue
addPath
in interface Queue
path
- path of the file to be addedIOException
- if any file operation failspublic String get(String name) throws IOException
Queue
get
in interface Queue
name
- name of the element to be retrievedIOException
public byte[] getAsByteArray(String name) throws IOException
Queue
getAsByteArray
in interface Queue
name
- name of the element to be retrievedIOException
public 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 failspublic 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 failspublic boolean unlock(String name) throws IOException
Queue
unlock
in interface Queue
name
- name of the element to be unlockedtrue
on success, false
if the element
could not be unlockedIOException
- if any file operation failspublic boolean unlock(String name, boolean permissive) throws IOException
Queue
unlock
in interface Queue
name
- name of the element to be unlockedpermissive
- work in permissive modetrue
on success, false
if the element
could not be unlockedIOException
- if any file operation failspublic void remove(String name) throws IOException
Queue
remove
in interface Queue
name
- name of the element to be removedIOException
- if any file operation failspublic int count()
Queue
public void purge() throws IOException
Queue
purge
in interface Queue
IOException
- if any file operation failspublic void purge(int 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 failspublic void purge(int maxLock, int 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 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)Copyright © 2019. All rights reserved.