Class MessageStreamUtils

java.lang.Object
org.axonframework.messaging.core.MessageStreamUtils

public abstract class MessageStreamUtils extends Object
Utility methods to work with MessageStreams.
Since:
5.0.0
Author:
Allard Buijze
  • Method Details

    • reduce

      public static <M extends Message, R> CompletableFuture<R> reduce(@Nonnull MessageStream<M> source, @Nonnull R identity, @Nonnull BiFunction<R,MessageStream.Entry<M>,R> accumulator)
      Returns a CompletableFuture that completes with the given reduction of messages read from the source. The reduction is computed by applying the given accumulator function on the result of the previous invocation in combination with each entry returned by the given source. The very first invocation of the accumulator function is given the identity.

      If the given source completes normally without producing any entries, the returned CompletableFuture completes with the given identity.

      If the given source completes with an error, whether entries have been produced or not, the returned CompletableFuture completes exceptionally with that error.

      Multi-threading
      The accumulator function is invoked either on the thread calling this method, when entries are immediately available for reading, or on the thread on which entries are reported to be available for reading from the given source. The accumulator function does not need to be thread-safe.

      Type Parameters:
      M - The type of Message to consume from the MessageStream.
      R - The type of result expected from the reduction operation.
      Parameters:
      source - The MessageStream to consume messages from.
      identity - The initial value to use for the accumulation.
      accumulator - The function to combine the current reduction result with the next entry from the MessageStream.
      Returns:
      A CompletableFuture that completes with the result of the reduction operation.
    • asCompletableFuture

      public static <M extends Message> CompletableFuture<MessageStream.Entry<M>> asCompletableFuture(@Nonnull MessageStream<M> source)
      Returns a CompletableFuture that completes with the first entry from the given source.

      If the given source has completed without producing any entries, the returned CompletableFuture will either complete with a null result if the source completed normally, or exceptionally if the source completed with an error.

      Once the first entry is read from the source, it is automatically closed, and any subsequent entries in the source are ignored.

      Type Parameters:
      M - The type of Message produced by the stream.
      Parameters:
      source - The source to read the first entry from.
      Returns:
      A CompletableFuture that completes with the first entry from the stream.