Class DelayedMessageStream<M extends Message>

java.lang.Object
org.axonframework.messaging.core.DelayedMessageStream<M>
Type Parameters:
M - The type of Message contained in the entries of this stream.
All Implemented Interfaces:
MessageStream<M>

public class DelayedMessageStream<M extends Message> extends Object implements MessageStream<M>
An implementation of the MessageStream that wraps a stream that will become available asynchronously.
Since:
5.0.0
Author:
Allard Buijze, Steven van Beelen
  • Method Details

    • create

      public static <M extends Message> MessageStream<M> create(@Nonnull CompletableFuture<? extends MessageStream<M>> delegate)
      Creates a stream that delays actions to its delegate when it becomes available.

      If the given delegate has already completed, it returns the MessageStream immediately from it. Otherwise, it returns a DelayedMessageStream instance wrapping the given delegate.

      Type Parameters:
      M - The type of Message contained in the entries of this stream.
      Parameters:
      delegate - A CompletableFuture providing access to the stream to delegate to when it becomes available.
      Returns:
      A stream that delegates all actions to the delegate when it becomes available.
    • createSingle

      public static <M extends Message> MessageStream.Single<M> createSingle(@Nonnull CompletableFuture<MessageStream.Single<M>> delegate)
      Creates a single stream that delays actions to its delegate when it becomes available.

      If the given delegate has already completed, it returns the MessageStream immediately from it. Otherwise, it returns a DelayedMessageStream instance wrapping the given delegate.

      Type Parameters:
      M - The type of Message contained in the entries of this stream.
      Parameters:
      delegate - A CompletableFuture providing access to the stream to delegate to when it becomes available.
      Returns:
      A stream that delegates all actions to the delegate when it becomes available.
    • next

      public Optional<MessageStream.Entry<M>> next()
      Description copied from interface: MessageStream
      Returns an Optional carrying the next entry from 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:
      next in interface MessageStream<M extends Message>
      Returns:
      An optional carrying the next entry, if available.
    • setCallback

      public void setCallback(@Nonnull Runnable callback)
      Description copied from interface: MessageStream
      Registers the callback to invoke when entries are 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:
      setCallback in interface MessageStream<M extends Message>
      Parameters:
      callback - The callback to invoke when entries are available for reading, or the stream completes.
    • error

      public Optional<Throwable> error()
      Description copied from interface: MessageStream
      Indicates whether any error has been reported in this stream. Implementations may choose to not return any error here until all entries that were available for reading before any error occurred have been consumed.
      Specified by:
      error in interface MessageStream<M extends Message>
      Returns:
      An optional containing the possible error this stream completed with.
    • isCompleted

      public boolean isCompleted()
      Description copied from interface: MessageStream
      Indicates whether this stream has been completed. A completed stream will never return entries from MessageStream.next(), and MessageStream.hasNextAvailable() will always return false. If the stream completed with an error, MessageStream.error() will report so.
      Specified by:
      isCompleted in interface MessageStream<M extends Message>
      Returns:
      true if the stream completed, otherwise false.
    • hasNextAvailable

      public boolean hasNextAvailable()
      Description copied from interface: MessageStream
      Indicates whether an entry is available for immediate reading. When entries are reported available, there is no guarantee that MessageStream.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 returns false.
      Specified by:
      hasNextAvailable in interface MessageStream<M extends Message>
      Returns:
      true when there are entries available for reading, false otherwise.
    • close

      public void close()
      Description copied from interface: MessageStream
      Closes this stream, freeing any possible resources occupied by the underlying stream. After invocation, some entries may 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:
      close in interface MessageStream<M extends Message>
    • reduce

      public <R> CompletableFuture<R> reduce(@Nonnull R identity, @Nonnull BiFunction<R,MessageStream.Entry<M>,R> accumulator)
      Description copied from interface: MessageStream
      Returns a CompletableFuture of type R, using the given identity as the initial value for the given accumulator. Throws an exception if this stream is unbounded.

      The accumulator will process all entries within this stream until a single value of type R is left.

      Note that parallel processing is not supported!

      Specified by:
      reduce in interface MessageStream<M extends Message>
      Type Parameters:
      R - The result of the accumulator after reducing all entries from this stream.
      Parameters:
      identity - The initial value given to the accumulator.
      accumulator - The BiFunction accumulating all entries from this stream into a return value of type R.
      Returns:
      A CompletableFuture carrying the result of the given accumulator that reduced the entire stream.
    • peek

      public Optional<MessageStream.Entry<M>> peek()
      Description copied from interface: MessageStream
      Returns an Optional carrying the next entry from 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:
      peek in interface MessageStream<M extends Message>
      Returns:
      An optional carrying the next entry, if available.