Class BlockingQueueConsumer<T>

java.lang.Object
org.apache.curator.framework.recipes.queue.BlockingQueueConsumer<T>
All Implemented Interfaces:
QueueConsumer<T>, ConnectionStateListener

public class BlockingQueueConsumer<T> extends Object implements QueueConsumer<T>
Utility - a queue consumer that provides behavior similar to a BlockingQueue
  • Constructor Details

    • BlockingQueueConsumer

      public BlockingQueueConsumer(ConnectionStateListener connectionStateListener)
      Creates with capacity of Integer.MAX_VALUE
      Parameters:
      connectionStateListener - listener for connection state changes
    • BlockingQueueConsumer

      public BlockingQueueConsumer(ConnectionStateListener connectionStateListener, int capacity)
      Parameters:
      connectionStateListener - listener for connection state changes
      capacity - max capacity (i.e. puts block if full)
    • BlockingQueueConsumer

      public BlockingQueueConsumer(ConnectionStateListener connectionStateListener, BlockingQueue<T> queue)
      Wrap the given blocking queue
      Parameters:
      connectionStateListener - listener for connection state changes
      queue - queue to use
  • Method Details

    • consumeMessage

      public void consumeMessage(T message) throws Exception
      Description copied from interface: QueueConsumer
      Process a message from the queue
      Specified by:
      consumeMessage in interface QueueConsumer<T>
      Parameters:
      message - message to process
      Throws:
      Exception - any errors
    • getItems

      public List<T> getItems()
      Return any currently queued items without removing them from the queue
      Returns:
      items (can be empty)
    • size

      public int size()
      Returns the number of currently queue items
      Returns:
      currently queue item count or 0
    • take

      public T take() throws InterruptedException
      Take the next item from the queue, blocking until there is an item available
      Returns:
      the item
      Throws:
      InterruptedException - thread interruption
    • take

      public T take(int time, TimeUnit unit) throws InterruptedException
      Take the next item from the queue, waiting up to the specified time for an available item. If the time elapses, null is returned.
      Parameters:
      time - amount of time to block
      unit - time unit
      Returns:
      next item or null
      Throws:
      InterruptedException - thread interruption
    • drainTo

      public int drainTo(Collection<? super T> c)
      Removes all available elements from this queue and adds them to the given collection. This operation may be more efficient than repeatedly polling this queue. A failure encountered while attempting to add elements to collection c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.
      Parameters:
      c - the collection to transfer elements into
      Returns:
      the number of elements transferred
      Throws:
      UnsupportedOperationException - if addition of elements is not supported by the specified collection
      ClassCastException - if the class of an element of this queue prevents it from being added to the specified collection
      NullPointerException - if the specified collection is null
      IllegalArgumentException - if the specified collection is this queue, or some property of an element of this queue prevents it from being added to the specified collection
    • stateChanged

      public void stateChanged(CuratorFramework client, ConnectionState newState)
      Description copied from interface: ConnectionStateListener
      Called when there is a state change in the connection
      Specified by:
      stateChanged in interface ConnectionStateListener
      Parameters:
      client - the client
      newState - the new state