Class QueueMessageStream<M extends Message>
- Type Parameters:
M- The type of Message managed by this stream.
- All Implemented Interfaces:
MessageStream<M>
- Since:
- 5.0.0
- Author:
- Allard Buijze
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.axonframework.messaging.core.MessageStream
MessageStream.Empty<M extends Message>, MessageStream.Entry<M extends Message>, MessageStream.Single<M extends Message> -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an instance with an unbounded queue.Construct an instance with givenqueueas the underlying queue. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this stream, freeing any possible resources occupied by the underlying stream.voidcomplete()Marks the queue as completed, indicating to any consumer that no moreentrieswill become available.voidcompleteExceptionally(Throwable error) Completes the stream with the giventhrowable.booleanIndicates whether anentryis available for immediate reading.booleanisClosed()Indicates whether this stream has been closed, either by completion or by explicit closing from the consumer.booleanIndicates whether this stream has been completed.next()Returns an Optional carrying the nextentryfrom the stream, if such entry was available.booleanAdd the givenmessageand accompanyingcontextavailable for reading by a consumer.voidonConsumeCallback(Runnable callback) Registers givencallbackto be invoked whenentrieshave been consumed from the underlying queue.peek()Returns an Optional carrying the nextentryfrom the stream (without moving the stream pointer), if such entry was available.voidsetCallback(Runnable callback) Registers the callback to invoke whenentriesare available for reading or when the stream completes (either normally or with an error).Methods inherited from class org.axonframework.messaging.core.AbstractMessageStream
error, invokeCallbackSafelyMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.axonframework.messaging.core.MessageStream
cast, concatWith, filter, first, ignoreEntries, map, mapMessage, onClose, onComplete, onErrorContinue, onNext, reduce
-
Constructor Details
-
QueueMessageStream
public QueueMessageStream()Constructs an instance with an unbounded queue. Offeringentrieswill always be possible, as long as memory permits. -
QueueMessageStream
Construct an instance with givenqueueas the underlying queue. Offering and consumingentrieswill depend on the semantics of the implementation of the queue.Note that delivery and consumption of entries is done through
BlockingQueue.offer(Object)andQueue.poll(), respectively. This means that a queue must be available to buffer elements. Implementations of aTransferQueuetypically don't have this, and will therefore not work.- Parameters:
queue- The queue to use to storeentriesin transit from producer to consumer.
-
-
Method Details
-
offer
Add the givenmessageand accompanyingcontextavailable for reading by a consumer. Any callback that has been registered will be notified of the availability of a newentry.If the underling buffer has insufficient space to store the offered element, or if the stream has been closed, the method returns
false.- Parameters:
message- The message to add to the queue.context- The context to accompany the message.- Returns:
trueif the message was successfully buffered. Otherwisefalse.
-
complete
public void complete()Marks the queue as completed, indicating to any consumer that no moreentrieswill become available.Note that there is no validation on offering items whether the stream is completed. It is the caller's responsibility to ensure no
Messagesareofferedafter completion.- Overrides:
completein classAbstractMessageStream<M extends Message>
-
completeExceptionally
Description copied from class:AbstractMessageStreamCompletes the stream with the giventhrowable.- Overrides:
completeExceptionallyin classAbstractMessageStream<M extends Message>- Parameters:
error- The error that caused the stream to complete.
-
onConsumeCallback
Registers givencallbackto be invoked whenentrieshave been consumed from the underlying queue. Any previously registered callback will be replaced.The given
callbackis also notified when the consumer has requested toclose()this stream.- Parameters:
callback- The callback to invoke whenentriesare consumed.
-
setCallback
Description copied from interface:MessageStreamRegisters the callback to invoke whenentriesare available for reading or when the stream completes (either normally or with an error). An invocation of the callback does not in any way guarantee that entries are indeed available, or that the stream has indeed been completed. Implementations may choose to suppress repeated invocations of the callback if no entries have been read in the meantime.Any previously registered callback is replaced with the given
callback.The callback is called on an arbitrary thread, and it should keep work performed on this thread to a minimum as this may interfere with other callbacks handled by the same thread. Any exception thrown by the callback will result in the stream completing with this exception as the error.
- Specified by:
setCallbackin interfaceMessageStream<M extends Message>- Overrides:
setCallbackin classAbstractMessageStream<M extends Message>- Parameters:
callback- The callback to invoke whenentriesare available for reading, or the stream completes.
-
next
Description copied from interface:MessageStreamReturns an Optional carrying the nextentryfrom the stream, if such entry was available. If no entry was available for reading, this method returns an empty Optional.This method will never block for elements becoming available.
- Returns:
- An optional carrying the next
entry, if available.
-
peek
Description copied from interface:MessageStreamReturns an Optional carrying the nextentryfrom the stream (without moving the stream pointer), if such entry was available. If no entry was available for reading, this method returns an empty Optional.This method will never block for elements becoming available.
- Returns:
- An optional carrying the next
entry, if available.
-
isCompleted
public boolean isCompleted()Description copied from interface:MessageStreamIndicates whether this stream has been completed. A completed stream will never returnentriesfromMessageStream.next(), andMessageStream.hasNextAvailable()will always returnfalse. If the stream completed with an error,MessageStream.error()will report so.- Specified by:
isCompletedin interfaceMessageStream<M extends Message>- Overrides:
isCompletedin classAbstractMessageStream<M extends Message>- Returns:
trueif the stream completed, otherwisefalse.
-
hasNextAvailable
public boolean hasNextAvailable()Description copied from interface:MessageStreamIndicates whether anentryis available for immediate reading. When entries are reported available, there is no guarantee thatMessageStream.next()will indeed return an entry. However, besides any concurrent activity on this stream, it is guaranteed that no entries are available for reading when this method returnsfalse.- Returns:
truewhen there areentriesavailable for reading,falseotherwise.
-
close
public void close()Description copied from interface:MessageStreamCloses this stream, freeing any possible resources occupied by the underlying stream. After invocation, someentriesmay still be available for reading.Implementations must always release resources when a stream is completed, either with an error or normally. Therefore, it is only required to
close()a stream if the consumer decides to not read until the end. -
isClosed
public boolean isClosed()Indicates whether this stream has been closed, either by completion or by explicit closing from the consumer.Unlike
isCompleted(), this may also returntruewhen there are still messages to consume- Returns:
truewhen closed or completed, otherwisefalse
-