Class QueueBuilder<T>
DistributedQueue
and DistributedPriorityQueue
-
Method Summary
Modifier and TypeMethodDescriptionBuild aDistributedDelayQueue
from the current builder values.static <T> QueueBuilder
<T> builder
(CuratorFramework client, QueueConsumer<T> consumer, QueueSerializer<T> serializer, String queuePath) Allocate a new builderBuild aDistributedIdQueue
from the current builder valuesbuildPriorityQueue
(int minItemsBeforeRefresh) Build aDistributedPriorityQueue
from the current builder values.Build aDistributedQueue
from the current builder valuesChange the executor used.finalFlushTime
(int time, TimeUnit unit) Sets an amount of time to callDistributedQueue.flushPuts(long, TimeUnit)
when the queue is closed.Without a lock set, queue items are removed before being sent to the queue consumer.maxItems
(int maxItems) By default, the various queues are unbounded.putInBackground
(boolean putInBackground) By default, messages are added in the background.threadFactory
(ThreadFactory factory) Change the thread factory used.
-
Method Details
-
builder
public static <T> QueueBuilder<T> builder(CuratorFramework client, QueueConsumer<T> consumer, QueueSerializer<T> serializer, String queuePath) Allocate a new builder- Parameters:
client
- the curator clientconsumer
- functor to consume messages - NOTE: passnull
to make this a producer-only queueserializer
- serializer to use for itemsqueuePath
- path to store queue- Returns:
- builder
-
buildQueue
Build aDistributedQueue
from the current builder values- Returns:
- distributed queue
-
buildIdQueue
Build aDistributedIdQueue
from the current builder values- Returns:
- distributed id queue
-
buildPriorityQueue
Build a
DistributedPriorityQueue
from the current builder values.When the priority queue detects an item addition/removal, it will stop processing its current list of items and refresh the list.
minItemsBeforeRefresh
modifies this. It determines the minimum number of items from the active list that will get processed before a refresh.Due to a quirk in the way ZooKeeper notifies changes, the queue will get an item addition/remove notification after every item is processed. This can lead to poor performance. Set
minItemsBeforeRefresh
to the value your application can tolerate being out of sync.For example: if the queue sees 10 items to process, it will end up making 10 calls to ZooKeeper to check status. You can control this by setting
minItemsBeforeRefresh
to 10 (or more) and the queue will only refresh with ZooKeeper after 10 items are processed- Parameters:
minItemsBeforeRefresh
- minimum items to process before refreshing the item list- Returns:
- distributed priority queue
-
buildDelayQueue
Build a
DistributedDelayQueue
from the current builder values.- Returns:
- distributed delay queue
-
threadFactory
Change the thread factory used. The default isExecutors.defaultThreadFactory()
- Parameters:
factory
- new thread factory to use- Returns:
- this
-
executor
Change the executor used. The default isinvalid reference
MoreExecutors#directExectutor()
- Parameters:
executor
- new executor to use- Returns:
- this
-
lockPath
Without a lock set, queue items are removed before being sent to the queue consumer. This can result in message loss if the consumer fails to complete the message or the process dies.
Use a lock to make the message recoverable. A lock is held while the message is being processed - this prevents other processes from taking the message. The message will not be removed from the queue until the consumer functor returns. Thus, if there is a failure or the process dies, the message will get sent to another process. There is a small performance penalty for this behavior however.
- Parameters:
path
- path for the lock- Returns:
- this
-
maxItems
By default, the various queues are unbounded. This method allows setting a max number of items to have in the queue. With this value set, the variousput
methods will block when the number of items in the queue approachesmaxItems
. NOTE:maxItems
cannot be exactly achieved. The only guarantee is that approximatelymaxItems
will cause puts to block.- Parameters:
maxItems
- the upper bound for the queue- Returns:
- this
-
putInBackground
By default, messages are added in the background. However, this can flood the background thread.- Parameters:
putInBackground
- true to put in the background (default). false to put in the foreground.- Returns:
- this
-
finalFlushTime
Sets an amount of time to callDistributedQueue.flushPuts(long, TimeUnit)
when the queue is closed. The default is 5 seconds. Pass 0 to turn flushing on close off.- Parameters:
time
- timeunit
- the unit- Returns:
- this
-