public class DistributedQueue<T> extends java.lang.Object implements QueueBase<T>
An implementation of the Distributed Queue ZK recipe. Items put into the queue are guaranteed to be ordered (by means of ZK's PERSISTENT_SEQUENTIAL node).
Guarantees:
LeaderSelector
to nominate a single consumer.QueueBuilder.lockPath(String)
is used, there is only guaranteed processing of each message to the point of receipt by a given instance.
If an instance receives an item from the queue but dies while processing it, the item will be lost. If you need message recoverability, use
a QueueBuilder.lockPath(String)
Modifier and Type | Class and Description |
---|---|
protected static class |
DistributedQueue.ProcessType |
Modifier and Type | Method and Description |
---|---|
void |
close() |
boolean |
flushPuts(long waitTime,
java.util.concurrent.TimeUnit timeUnit)
Wait until any pending puts are committed
|
protected java.util.List<java.lang.String> |
getChildren() |
protected long |
getDelay(java.lang.String itemNode) |
int |
getLastMessageCount()
Return the most recent message count from the queue.
|
ListenerContainer<QueuePutListener<T>> |
getPutListenerContainer()
Return the manager for put listeners
|
protected boolean |
processWithLockSafety(java.lang.String itemNode,
DistributedQueue.ProcessType type) |
void |
put(T item)
Add an item into the queue.
|
boolean |
put(T item,
int maxWait,
java.util.concurrent.TimeUnit unit)
Same as
put(Object) but allows a maximum wait time if an upper bound was set
via QueueBuilder.maxItems . |
void |
putMulti(MultiItem<T> items)
Add a set of items into the queue.
|
boolean |
putMulti(MultiItem<T> items,
int maxWait,
java.util.concurrent.TimeUnit unit)
Same as
putMulti(MultiItem) but allows a maximum wait time if an upper bound was set
via QueueBuilder.maxItems . |
void |
setErrorMode(ErrorMode newErrorMode)
Used when the queue is created with a
QueueBuilder.lockPath(String) . |
protected void |
sortChildren(java.util.List<java.lang.String> children) |
void |
start()
Start the queue.
|
protected boolean |
tryRemove(java.lang.String itemNode) |
public void start() throws java.lang.Exception
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
public ListenerContainer<QueuePutListener<T>> getPutListenerContainer()
getPutListenerContainer
in interface QueueBase<T>
public void setErrorMode(ErrorMode newErrorMode)
QueueBuilder.lockPath(String)
. Determines
the behavior when the queue consumer throws an exceptionsetErrorMode
in interface QueueBase<T>
newErrorMode
- the new error mode (the default is ErrorMode.REQUEUE
public boolean flushPuts(long waitTime, java.util.concurrent.TimeUnit timeUnit) throws java.lang.InterruptedException
public void put(T item) throws java.lang.Exception
QueueBuilder.maxItems
, this method will
block until there is available space in the queue.item
- item to addjava.lang.Exception
- connection issuespublic boolean put(T item, int maxWait, java.util.concurrent.TimeUnit unit) throws java.lang.Exception
put(Object)
but allows a maximum wait time if an upper bound was set
via QueueBuilder.maxItems
.item
- item to addmaxWait
- maximum waitunit
- wait unitjava.lang.Exception
public void putMulti(MultiItem<T> items) throws java.lang.Exception
QueueBuilder.maxItems
, this method will
block until there is available space in the queue.items
- items to addjava.lang.Exception
- connection issuespublic boolean putMulti(MultiItem<T> items, int maxWait, java.util.concurrent.TimeUnit unit) throws java.lang.Exception
putMulti(MultiItem)
but allows a maximum wait time if an upper bound was set
via QueueBuilder.maxItems
.items
- items to addmaxWait
- maximum waitunit
- wait unitjava.lang.Exception
public int getLastMessageCount()
getLastMessageCount
in interface QueueBase<T>
protected void sortChildren(java.util.List<java.lang.String> children)
protected java.util.List<java.lang.String> getChildren() throws java.lang.Exception
java.lang.Exception
protected long getDelay(java.lang.String itemNode)
protected boolean tryRemove(java.lang.String itemNode) throws java.lang.Exception
java.lang.Exception
protected boolean processWithLockSafety(java.lang.String itemNode, DistributedQueue.ProcessType type) throws java.lang.Exception
java.lang.Exception