Class DistributedQueue<T>

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, QueueBase<T>

    public class DistributedQueue<T>
    extends java.lang.Object
    implements QueueBase<T>

    An implementation of the Distributed Queue ZK recipe. Items put into the queue are guaranteed to be ordered (by means of ZK's PERSISTENT_SEQUENTIAL node).

    Guarantees:

    • If a single consumer takes items out of the queue, they will be ordered FIFO. i.e. if ordering is important, use a LeaderSelector to nominate a single consumer.
    • Unless a QueueBuilder.lockPath(String) is used, there is only guaranteed processing of each message to the point of receipt by a given instance.
    • If an instance receives an item from the queue but dies while processing it, the item will be lost. If you need message recoverability, use a QueueBuilder.lockPath(String)
    • Method Detail

      • start

        public void start()
                   throws java.lang.Exception
        Start the queue. No other methods work until this is called
        Specified by:
        start in interface QueueBase<T>
        Throws:
        java.lang.Exception - startup errors
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • getPutListenerContainer

        public org.apache.curator.framework.listen.ListenerContainer<QueuePutListener<T>> getPutListenerContainer()
        Return the manager for put listeners
        Specified by:
        getPutListenerContainer in interface QueueBase<T>
        Returns:
        put listener container
      • flushPuts

        public boolean flushPuts​(long waitTime,
                                 java.util.concurrent.TimeUnit timeUnit)
                          throws java.lang.InterruptedException
        Wait until any pending puts are committed
        Specified by:
        flushPuts in interface QueueBase<T>
        Parameters:
        waitTime - max wait time
        timeUnit - time unit
        Returns:
        true if the flush was successful, false if it timed out first
        Throws:
        java.lang.InterruptedException - if thread was interrupted
      • put

        public void put​(T item)
                 throws java.lang.Exception
        Add an item into the queue. Adding is done in the background - thus, this method will return quickly.

        NOTE: if an upper bound was set via QueueBuilder.maxItems, this method will block until there is available space in the queue.
        Parameters:
        item - item to add
        Throws:
        java.lang.Exception - connection issues
      • put

        public boolean put​(T item,
                           int maxWait,
                           java.util.concurrent.TimeUnit unit)
                    throws java.lang.Exception
        Same as put(Object) but allows a maximum wait time if an upper bound was set via QueueBuilder.maxItems.
        Parameters:
        item - item to add
        maxWait - maximum wait
        unit - wait unit
        Returns:
        true if items was added, false if timed out
        Throws:
        java.lang.Exception
      • putMulti

        public void putMulti​(MultiItem<T> items)
                      throws java.lang.Exception
        Add a set of items into the queue. Adding is done in the background - thus, this method will return quickly.

        NOTE: if an upper bound was set via QueueBuilder.maxItems, this method will block until there is available space in the queue.
        Parameters:
        items - items to add
        Throws:
        java.lang.Exception - connection issues
      • putMulti

        public boolean putMulti​(MultiItem<T> items,
                                int maxWait,
                                java.util.concurrent.TimeUnit unit)
                         throws java.lang.Exception
        Same as putMulti(MultiItem) but allows a maximum wait time if an upper bound was set via QueueBuilder.maxItems.
        Parameters:
        items - items to add
        maxWait - maximum wait
        unit - wait unit
        Returns:
        true if items was added, false if timed out
        Throws:
        java.lang.Exception
      • getLastMessageCount

        public int getLastMessageCount()
        Return the most recent message count from the queue. This is useful for debugging/information purposes only.
        Specified by:
        getLastMessageCount in interface QueueBase<T>
        Returns:
        count (can be 0)
      • sortChildren

        protected void sortChildren​(java.util.List<java.lang.String> children)
      • getChildren

        protected java.util.List<java.lang.String> getChildren()
                                                        throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getDelay

        protected long getDelay​(java.lang.String itemNode)
      • tryRemove

        protected boolean tryRemove​(java.lang.String itemNode)
                             throws java.lang.Exception
        Throws:
        java.lang.Exception
      • processWithLockSafety

        protected boolean processWithLockSafety​(java.lang.String itemNode,
                                                DistributedQueue.ProcessType type)
                                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • makeRequeueItemPath

        protected java.lang.String makeRequeueItemPath​(java.lang.String itemPath)