Class DelayedMessageStream<M extends Message>
- All Implemented Interfaces:
MessageStream<M>
MessageStream that wraps a stream that will become available asynchronously.- Since:
- 5.0.0
- Author:
- Allard Buijze, Steven van Beelen
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.axonframework.messaging.core.MessageStream
MessageStream.Entry<M extends Message> -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this stream, freeing any possible resources occupied by the underlying stream.static <M extends Message>
MessageStream<M> create(CompletableFuture<? extends MessageStream<M>> delegate) Creates astreamthat delays actions to itsdelegatewhen it becomes available.static <M extends Message>
MessageStream.Single<M> createSingle(CompletableFuture<MessageStream.Single<M>> delegate) Creates asingle streamthat delays actions to itsdelegatewhen it becomes available.error()Indicates whether any error has been reported in this stream.booleanIndicates whether anentryis available for immediate reading.booleanIndicates whether this stream has been completed.next()Returns an Optional carrying the nextentryfrom the stream, if such entry was available.peek()Returns an Optional carrying the nextentryfrom the stream (without moving the stream pointer), if such entry was available.<R> CompletableFuture<R> reduce(R identity, BiFunction<R, MessageStream.Entry<M>, R> accumulator) Returns aCompletableFutureof typeR, using the givenidentityas the initial value for the givenaccumulator.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 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
-
Method Details
-
create
public static <M extends Message> MessageStream<M> create(@Nonnull CompletableFuture<? extends MessageStream<M>> delegate) Creates astreamthat delays actions to itsdelegatewhen it becomes available.If the given
delegatehas alreadycompleted, it returns theMessageStreamimmediately from it. Otherwise, it returns a DelayedMessageStream instance wrapping the givendelegate.- Type Parameters:
M- The type ofMessagecontained in theentriesof this stream.- Parameters:
delegate- ACompletableFutureproviding access to thestreamto delegate to when it becomes available.- Returns:
- A
streamthat delegates all actions to thedelegatewhen it becomes available.
-
createSingle
public static <M extends Message> MessageStream.Single<M> createSingle(@Nonnull CompletableFuture<MessageStream.Single<M>> delegate) Creates asingle streamthat delays actions to itsdelegatewhen it becomes available.If the given
delegatehas alreadycompleted, it returns theMessageStreamimmediately from it. Otherwise, it returns a DelayedMessageStream instance wrapping the givendelegate.- Type Parameters:
M- The type ofMessagecontained in theentriesof this stream.- Parameters:
delegate- ACompletableFutureproviding access to thestreamto delegate to when it becomes available.- Returns:
- A
streamthat delegates all actions to thedelegatewhen it becomes available.
-
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.
- Specified by:
nextin interfaceMessageStream<M extends Message>- Returns:
- An optional carrying the next
entry, if available.
-
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>- Parameters:
callback- The callback to invoke whenentriesare available for reading, or the stream completes.
-
error
Description copied from interface:MessageStreamIndicates whether any error has been reported in this stream. Implementations may choose to not return any error here until allentriesthat were available for reading before any error occurred have been consumed.- Specified by:
errorin interfaceMessageStream<M extends Message>- Returns:
- An optional containing the possible error this stream completed with.
-
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>- 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.- Specified by:
hasNextAvailablein interfaceMessageStream<M extends Message>- 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.- Specified by:
closein interfaceMessageStream<M extends Message>
-
reduce
public <R> CompletableFuture<R> reduce(@Nonnull R identity, @Nonnull BiFunction<R, MessageStream.Entry<M>, R> accumulator) Description copied from interface:MessageStreamReturns aCompletableFutureof typeR, using the givenidentityas the initial value for the givenaccumulator. Throws an exception if this stream is unbounded.The
accumulatorwill process allentrieswithin this stream until a single value of typeRis left.Note that parallel processing is not supported!
- Specified by:
reducein interfaceMessageStream<M extends Message>- Type Parameters:
R- The result of theaccumulatorafter reducing allentriesfrom this stream.- Parameters:
identity- The initial value given to theaccumulator.accumulator- TheBiFunctionaccumulating allentriesfrom this stream into a return value of typeR.- Returns:
- A
CompletableFuturecarrying the result of the givenaccumulatorthat reduced the entire stream.
-
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.
- Specified by:
peekin interfaceMessageStream<M extends Message>- Returns:
- An optional carrying the next
entry, if available.
-