Class QueueBuilder<T>

java.lang.Object
org.apache.curator.framework.recipes.queue.QueueBuilder<T>

public class QueueBuilder<T> extends Object
  • 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 client
      consumer - functor to consume messages - NOTE: pass null to make this a producer-only queue
      serializer - serializer to use for items
      queuePath - path to store queue
      Returns:
      builder
    • buildQueue

      public DistributedQueue<T> buildQueue()
      Build a DistributedQueue from the current builder values
      Returns:
      distributed queue
    • buildIdQueue

      public DistributedIdQueue<T> buildIdQueue()
      Build a DistributedIdQueue from the current builder values
      Returns:
      distributed id queue
    • buildPriorityQueue

      public DistributedPriorityQueue<T> buildPriorityQueue(int minItemsBeforeRefresh)

      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

      public DistributedDelayQueue<T> buildDelayQueue()

      Build a DistributedDelayQueue from the current builder values.

      Returns:
      distributed delay queue
    • threadFactory

      public QueueBuilder<T> threadFactory(ThreadFactory factory)
      Change the thread factory used. The default is Executors.defaultThreadFactory()
      Parameters:
      factory - new thread factory to use
      Returns:
      this
    • executor

      public QueueBuilder<T> executor(Executor executor)
      Change the executor used. The default is
      invalid reference
      MoreExecutors#directExectutor()
      Parameters:
      executor - new executor to use
      Returns:
      this
    • lockPath

      public QueueBuilder<T> lockPath(String path)

      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

      public QueueBuilder<T> maxItems(int 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 various put methods will block when the number of items in the queue approaches maxItems. NOTE: maxItems cannot be exactly achieved. The only guarantee is that approximately maxItems will cause puts to block.
      Parameters:
      maxItems - the upper bound for the queue
      Returns:
      this
    • putInBackground

      public QueueBuilder<T> putInBackground(boolean 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

      public QueueBuilder<T> finalFlushTime(int time, TimeUnit unit)
      Sets an amount of time to call DistributedQueue.flushPuts(long, TimeUnit) when the queue is closed. The default is 5 seconds. Pass 0 to turn flushing on close off.
      Parameters:
      time - time
      unit - the unit
      Returns:
      this