Class DistributedQueue<T>
- java.lang.Object
-
- org.apache.curator.framework.recipes.queue.DistributedQueue<T>
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,QueueBase<T>
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:
- If a single consumer takes items out of the queue, they will be ordered FIFO. i.e. if ordering is important,
use a
LeaderSelectorto nominate a single consumer. - Unless a
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)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classDistributedQueue.ProcessType
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()booleanflushPuts(long waitTime, java.util.concurrent.TimeUnit timeUnit)Wait until any pending puts are committedprotected java.util.List<java.lang.String>getChildren()protected longgetDelay(java.lang.String itemNode)intgetLastMessageCount()Return the most recent message count from the queue.org.apache.curator.framework.listen.ListenerContainer<QueuePutListener<T>>getPutListenerContainer()Return the manager for put listenersprotected java.lang.StringmakeRequeueItemPath(java.lang.String itemPath)protected booleanprocessWithLockSafety(java.lang.String itemNode, DistributedQueue.ProcessType type)voidput(T item)Add an item into the queue.booleanput(T item, int maxWait, java.util.concurrent.TimeUnit unit)Same asput(Object)but allows a maximum wait time if an upper bound was set viaQueueBuilder.maxItems.voidputMulti(MultiItem<T> items)Add a set of items into the queue.booleanputMulti(MultiItem<T> items, int maxWait, java.util.concurrent.TimeUnit unit)Same asputMulti(MultiItem)but allows a maximum wait time if an upper bound was set viaQueueBuilder.maxItems.voidsetErrorMode(ErrorMode newErrorMode)Used when the queue is created with aQueueBuilder.lockPath(String).protected voidsortChildren(java.util.List<java.lang.String> children)voidstart()Start the queue.protected booleantryRemove(java.lang.String itemNode)
-
-
-
Method Detail
-
start
public void start() throws java.lang.ExceptionStart the queue. No other methods work until this is called
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
getPutListenerContainer
public org.apache.curator.framework.listen.ListenerContainer<QueuePutListener<T>> getPutListenerContainer()
Return the manager for put listeners- Specified by:
getPutListenerContainerin interfaceQueueBase<T>- Returns:
- put listener container
-
setErrorMode
public void setErrorMode(ErrorMode newErrorMode)
Used when the queue is created with aQueueBuilder.lockPath(String). Determines the behavior when the queue consumer throws an exception- Specified by:
setErrorModein interfaceQueueBase<T>- Parameters:
newErrorMode- the new error mode (the default isErrorMode.REQUEUE
-
flushPuts
public boolean flushPuts(long waitTime, java.util.concurrent.TimeUnit timeUnit) throws java.lang.InterruptedExceptionWait until any pending puts are committed
-
put
public void put(T item) throws java.lang.Exception
Add an item into the queue. Adding is done in the background - thus, this method will return quickly.
NOTE: if an upper bound was set viaQueueBuilder.maxItems, this method will block until there is available space in the queue.- Parameters:
item- item to add- Throws:
java.lang.Exception- connection issues
-
put
public boolean put(T item, int maxWait, java.util.concurrent.TimeUnit unit) throws java.lang.Exception
Same asput(Object)but allows a maximum wait time if an upper bound was set viaQueueBuilder.maxItems.- Parameters:
item- item to addmaxWait- maximum waitunit- wait unit- Returns:
- true if items was added, false if timed out
- Throws:
java.lang.Exception
-
putMulti
public void putMulti(MultiItem<T> items) throws java.lang.Exception
Add a set of items into the queue. Adding is done in the background - thus, this method will return quickly.
NOTE: if an upper bound was set viaQueueBuilder.maxItems, this method will block until there is available space in the queue.- Parameters:
items- items to add- Throws:
java.lang.Exception- connection issues
-
putMulti
public boolean putMulti(MultiItem<T> items, int maxWait, java.util.concurrent.TimeUnit unit) throws java.lang.Exception
Same asputMulti(MultiItem)but allows a maximum wait time if an upper bound was set viaQueueBuilder.maxItems.- Parameters:
items- items to addmaxWait- maximum waitunit- wait unit- Returns:
- true if items was added, false if timed out
- Throws:
java.lang.Exception
-
getLastMessageCount
public int getLastMessageCount()
Return the most recent message count from the queue. This is useful for debugging/information purposes only.- Specified by:
getLastMessageCountin interfaceQueueBase<T>- Returns:
- count (can be 0)
-
sortChildren
protected void sortChildren(java.util.List<java.lang.String> children)
-
getChildren
protected java.util.List<java.lang.String> getChildren() throws java.lang.Exception- Throws:
java.lang.Exception
-
getDelay
protected long getDelay(java.lang.String itemNode)
-
tryRemove
protected boolean tryRemove(java.lang.String itemNode) throws java.lang.Exception- Throws:
java.lang.Exception
-
processWithLockSafety
protected boolean processWithLockSafety(java.lang.String itemNode, DistributedQueue.ProcessType type) throws java.lang.Exception- Throws:
java.lang.Exception
-
makeRequeueItemPath
protected java.lang.String makeRequeueItemPath(java.lang.String itemPath)
-
-