com.netflix.curator.framework.recipes.queue
Class BlockingQueueConsumer<T>

java.lang.Object
  extended by com.netflix.curator.framework.recipes.queue.BlockingQueueConsumer<T>
All Implemented Interfaces:
QueueConsumer<T>, com.netflix.curator.framework.state.ConnectionStateListener

public class BlockingQueueConsumer<T>
extends java.lang.Object
implements QueueConsumer<T>

Utility - a queue consumer that provides behavior similar to a BlockingQueue


Constructor Summary
BlockingQueueConsumer(com.netflix.curator.framework.state.ConnectionStateListener connectionStateListener)
          Creates with capacity of Integer.MAX_VALUE
BlockingQueueConsumer(com.netflix.curator.framework.state.ConnectionStateListener connectionStateListener, java.util.concurrent.BlockingQueue<T> queue)
          Wrap the given blocking queue
BlockingQueueConsumer(com.netflix.curator.framework.state.ConnectionStateListener connectionStateListener, int capacity)
           
 
Method Summary
 void consumeMessage(T message)
          Process a message from the queue
 int drainTo(java.util.Collection<? super T> c)
          Removes all available elements from this queue and adds them to the given collection.
 java.util.List<T> getItems()
          Return any currently queued items without removing them from the queue
 int size()
          Returns the number of currently queue items
 void stateChanged(com.netflix.curator.framework.CuratorFramework client, com.netflix.curator.framework.state.ConnectionState newState)
           
 T take()
          Take the next item from the queue, blocking until there is an item available
 T take(int time, java.util.concurrent.TimeUnit unit)
          Take the next item from the queue, waiting up to the specified time for an available item.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BlockingQueueConsumer

public BlockingQueueConsumer(com.netflix.curator.framework.state.ConnectionStateListener connectionStateListener)
Creates with capacity of Integer.MAX_VALUE

Parameters:
connectionStateListener - listener for connection state changes

BlockingQueueConsumer

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

BlockingQueueConsumer

public BlockingQueueConsumer(com.netflix.curator.framework.state.ConnectionStateListener connectionStateListener,
                             java.util.concurrent.BlockingQueue<T> queue)
Wrap the given blocking queue

Parameters:
queue - queue to use
connectionStateListener - listener for connection state changes
Method Detail

consumeMessage

public void consumeMessage(T message)
                    throws java.lang.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:
java.lang.Exception - any errors

getItems

public java.util.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 java.lang.InterruptedException
Take the next item from the queue, blocking until there is an item available

Returns:
the item
Throws:
java.lang.InterruptedException - thread interruption

take

public T take(int time,
              java.util.concurrent.TimeUnit unit)
       throws java.lang.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:
java.lang.InterruptedException - thread interruption

drainTo

public int drainTo(java.util.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:
java.lang.UnsupportedOperationException - if addition of elements is not supported by the specified collection
java.lang.ClassCastException - if the class of an element of this queue prevents it from being added to the specified collection
java.lang.NullPointerException - if the specified collection is null
java.lang.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(com.netflix.curator.framework.CuratorFramework client,
                         com.netflix.curator.framework.state.ConnectionState newState)
Specified by:
stateChanged in interface com.netflix.curator.framework.state.ConnectionStateListener