Package nl.colorize.util
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.
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionflush()
Removes, then returns all messages currently in the queue.void
Invokes the specified callback function for all messages, then removes all messages from the queue.boolean
isEmpty()
void
limitCapacity
(int limit) Limits the queue capacity to the specified limit.boolean
Offers a new message to the queue.poll()
Removes, then returns the oldest message from the queue.void
Deprecated.Selectively removing messages goes against the intended publish/subscribe workflow.static <T> MessageQueue
<T> subscribe
(Subscribable<T> subscribable) Factory method that creates aMessageQueue
which is subscribed to events from the specifiedSubscribable
.static MessageQueue
<Exception> subscribeErrors
(Subscribable<?> subscribable) Factory method that creates aMessageQueue
which is subscribed to errors> from the specifiedSubscribable
.
-
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
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
Removes, then returns the oldest message from the queue. Returnsnull
if the queue is currently empty. -
flush
Removes, then returns all messages currently in the queue. This is effectively a bulk version ofpoll()
that operates on all messages, instead of processing them one by one. -
flush
Invokes the specified callback function for all messages, then removes all messages from the queue. This is effectively a bulk version ofpoll()
that operates on all messages, instead of processing them one by one. -
remove
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
Factory method that creates aMessageQueue
which is subscribed to events from the specifiedSubscribable
. -
subscribeErrors
Factory method that creates aMessageQueue
which is subscribed to errors> from the specifiedSubscribable
.
-