Class DistributedQueue<T>
java.lang.Object
org.apache.curator.framework.recipes.queue.DistributedQueue<T>
- All Implemented Interfaces:
Closeable
,AutoCloseable
,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
LeaderSelector
to 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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
boolean
Wait until any pending puts are committedprotected long
int
Return the most recent message count from the queue.Return the manager for put listenersprotected String
makeRequeueItemPath
(String itemPath) protected boolean
processWithLockSafety
(String itemNode, DistributedQueue.ProcessType type) void
Add an item into the queue.boolean
Same asput(Object)
but allows a maximum wait time if an upper bound was set viaQueueBuilder.maxItems
.void
Add a set of items into the queue.boolean
Same asputMulti(MultiItem)
but allows a maximum wait time if an upper bound was set viaQueueBuilder.maxItems
.void
setErrorMode
(ErrorMode newErrorMode) Used when the queue is created with aQueueBuilder.lockPath(String)
.protected void
sortChildren
(List<String> children) void
start()
Start the queue.protected boolean
-
Method Details
-
start
Start the queue. No other methods work until this is called -
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
getPutListenerContainer
Return the manager for put listeners- Specified by:
getPutListenerContainer
in interfaceQueueBase<T>
- Returns:
- put listener container
-
setErrorMode
Used when the queue is created with aQueueBuilder.lockPath(String)
. Determines the behavior when the queue consumer throws an exception- Specified by:
setErrorMode
in interfaceQueueBase<T>
- Parameters:
newErrorMode
- the new error mode (the default isErrorMode.REQUEUE
-
flushPuts
Wait until any pending puts are committed- Specified by:
flushPuts
in interfaceQueueBase<T>
- Parameters:
waitTime
- max wait timetimeUnit
- time unit- Returns:
- true if the flush was successful, false if it timed out first
- Throws:
InterruptedException
- if thread was interrupted
-
put
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:
Exception
- connection issues
-
put
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:
Exception
-
putMulti
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:
Exception
- connection issues
-
putMulti
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:
Exception
-
getLastMessageCount
public int getLastMessageCount()Return the most recent message count from the queue. This is useful for debugging/information purposes only.- Specified by:
getLastMessageCount
in interfaceQueueBase<T>
- Returns:
- count (can be 0)
-
sortChildren
-
getChildren
- Throws:
Exception
-
getDelay
-
tryRemove
- Throws:
Exception
-
processWithLockSafety
protected boolean processWithLockSafety(String itemNode, DistributedQueue.ProcessType type) throws Exception - Throws:
Exception
-
makeRequeueItemPath
-