Class QueueHandler<K,​V>

  • Type Parameters:
    K - type of message-key; it should have a proper hash function in order to get O(1) access time, otherwise it may degrade to O(n)
    V - type of message-value
    All Implemented Interfaces:
    AutoCloseable

    public class QueueHandler<K,​V>
    extends Object
    implements AutoCloseable
    Retrieves RabbitMQ messages from specified queue via provided channel. Messages are converted by specified value function and indexed by specified key function.

    Messages are consumed in background threads and made available for retrieval by their key according to a specified retry policy. If a retry policy is not specified, then an internal default will be used.

    Typical workflow:

    1. build it
    2. start consumption -- this will run in background until closing
    3. retrieve messages
    • Field Detail

      • channel

        public final com.rabbitmq.client.Channel channel
      • queue

        public final String queue
      • consumingBy

        public final Function<byte[],​V> consumingBy
      • publishingBy

        public final Function<V,​byte[]> publishingBy
    • Method Detail

      • connectionFactoryFrom

        public static com.rabbitmq.client.ConnectionFactory connectionFactoryFrom​(String uri)
        Parameters:
        uri - an amqp or amqps uri; e.g., amqps://username:password@host/virtual-host
        Returns:
        a connection factory
        Throws:
        RuntimeException - upon malformed uri
      • cancel

        public String cancel()
                      throws IOException
        Cancels the consumption, previously started by consume(). Must be called on same thread as last consume() call.
        Returns:
        the consumer tag
        Throws:
        IOException - upon connection failures, or if consumer was not started
      • publishValues

        public void publishValues​(Stream<V> values)
      • recievedMessages

        public Map<K,​Message<V>> recievedMessages()
        Unmodifiable view of retrieved messages.
        Returns:
        the messages