Class MessageQueue<T>

java.lang.Object
nl.colorize.util.MessageQueue<T>
Type Parameters:
T - The type of message that can be stored in the queue.

public final class MessageQueue<T> extends Object
Accumulates messages received from a possibly asynchronous operation in a queue. Messages remain in the queue until they are retrieved by recipients. The message queue is created with a limit. Once this limit has been exceeded, received messages will no longer be added to the queue.

Instances of this class are thread-safe, the queue can be accessed from multiple threads. This is in fact the intended use case of this class, with publisher and recipient operating on different threads.

This class is part of a portable framework for publish/subscribe communication. This framework can be used across different platforms, including platforms where java.util.concurrent is not available, such as TeaVM.

  • Constructor Details

    • MessageQueue

      public MessageQueue()
  • Method Details

    • limitCapacity

      public void limitCapacity(int limit)
      Limits the queue capacity to the specified limit. Once this limit has been reached, the queue will no longer be able to accept new messages.
      Throws:
      IllegalStateException - if the queue capacity already exceeds the requested new limit.
    • offer

      public boolean offer(T message)
      Offers a new message to the queue. The message will be added to the back of the queue. Returns true if the message was added, returns false if the queue limit has been reached and the message was not added.
    • poll

      public T poll()
      Removes, then returns the oldest message from the queue. Returns null if the queue is currently empty.
    • flush

      public Iterable<T> flush()
      Removes, then returns all messages currently in the queue. This is effectively a bulk version of poll() that operates on all messages, instead of processing them one by one.
    • flush

      public void flush(Consumer<T> callback)
      Invokes the specified callback function for all messages, then removes all messages from the queue. This is effectively a bulk version of poll() that operates on all messages, instead of processing them one by one.
    • remove

      @Deprecated public void remove(T message)
      Deprecated.
      Selectively removing messages goes against the intended publish/subscribe workflow. Prefer explicitly handling all received messages, without selectively trying to modify the queue.
      Removes the specified message from the queue, without flushing the remaining queue contents.
    • isEmpty

      public boolean isEmpty()
    • subscribe

      public static <T> MessageQueue<T> subscribe(Subscribable<T> subscribable)
      Factory method that creates a MessageQueue which is subscribed to events from the specified Subscribable.
    • subscribeErrors

      public static MessageQueue<Exception> subscribeErrors(Subscribable<?> subscribable)
      Factory method that creates a MessageQueue which is subscribed to errors> from the specified Subscribable.