Class BlockingQueueConsumer<T>
- java.lang.Object
-
- org.apache.curator.framework.recipes.queue.BlockingQueueConsumer<T>
-
- All Implemented Interfaces:
QueueConsumer<T>,org.apache.curator.framework.state.ConnectionStateListener
public class BlockingQueueConsumer<T> extends java.lang.Object implements QueueConsumer<T>
Utility - a queue consumer that provides behavior similar to aBlockingQueue
-
-
Constructor Summary
Constructors Constructor Description BlockingQueueConsumer(org.apache.curator.framework.state.ConnectionStateListener connectionStateListener)Creates with capacity ofInteger.MAX_VALUEBlockingQueueConsumer(org.apache.curator.framework.state.ConnectionStateListener connectionStateListener, int capacity)BlockingQueueConsumer(org.apache.curator.framework.state.ConnectionStateListener connectionStateListener, java.util.concurrent.BlockingQueue<T> queue)Wrap the given blocking queue
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidconsumeMessage(T message)Process a message from the queueintdrainTo(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 queueintsize()Returns the number of currently queue itemsvoidstateChanged(org.apache.curator.framework.CuratorFramework client, org.apache.curator.framework.state.ConnectionState newState)Ttake()Take the next item from the queue, blocking until there is an item availableTtake(int time, java.util.concurrent.TimeUnit unit)Take the next item from the queue, waiting up to the specified time for an available item.
-
-
-
Constructor Detail
-
BlockingQueueConsumer
public BlockingQueueConsumer(org.apache.curator.framework.state.ConnectionStateListener connectionStateListener)
Creates with capacity ofInteger.MAX_VALUE- Parameters:
connectionStateListener- listener for connection state changes
-
BlockingQueueConsumer
public BlockingQueueConsumer(org.apache.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(org.apache.curator.framework.state.ConnectionStateListener connectionStateListener, java.util.concurrent.BlockingQueue<T> queue)Wrap the given blocking queue- Parameters:
queue- queue to useconnectionStateListener- listener for connection state changes
-
-
Method Detail
-
consumeMessage
public void consumeMessage(T message) throws java.lang.Exception
Description copied from interface:QueueConsumerProcess a message from the queue- Specified by:
consumeMessagein interfaceQueueConsumer<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,nullis returned.- Parameters:
time- amount of time to blockunit- 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 collectionjava.lang.ClassCastException- if the class of an element of this queue prevents it from being added to the specified collectionjava.lang.NullPointerException- if the specified collection is nulljava.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(org.apache.curator.framework.CuratorFramework client, org.apache.curator.framework.state.ConnectionState newState)- Specified by:
stateChangedin interfaceorg.apache.curator.framework.state.ConnectionStateListener
-
-