Class MonoUtils

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

public abstract class MonoUtils extends Object
Utility methods to work with Project Reactor's monos.
Since:
5.0.0
Author:
John Hendrikx
  • Method Details

    • asSingle

      public static <M extends Message> MessageStream.Single<M> asSingle(reactor.core.publisher.Mono<M> mono)
      Create a stream that returns a single entry wrapping the Message from the given mono, once it completes.

      The stream will contain at most a single entry. It may also contain no entries if the mono completes empty. The stream will complete with an exception when the given mono completes exceptionally.

      The given mono is subscribed with ThreadLocal context capture: Mono.toFuture() subscribes with a context-less CoreSubscriber, so without an explicit capture the mono's Reactor Context would always be empty, and thread-bound state present at subscription -- such as the tracing span and observation a message handler runs under -- could never reach the mono's operators or context-reading instrumentation (for example Spring Boot's R2DBC observation) downstream.

      Type Parameters:
      M - the type of Message contained in the entries of this stream
      Parameters:
      mono - the Mono providing the Message to contain in the stream
      Returns:
      a stream containing at most one entry from the given mono
    • asSingle

      public static <M extends Message> MessageStream.Single<M> asSingle(reactor.core.publisher.Mono<M> mono, Function<M,Context> contextSupplier)
      Create a stream that returns a single entry wrapping the Message from the given mono, once it completes.

      The automatically generated Entry will have the Context as given by the contextSupplier.

      The stream will contain at most a single entry. It may also contain no entries if the mono completes empty. The stream will complete with an exception when the given mono completes exceptionally.

      The given mono is subscribed with ThreadLocal context capture -- see asSingle(Mono) for the rationale.

      Type Parameters:
      M - the type of Message contained in the entries of this stream
      Parameters:
      mono - the Mono providing the Message to contain in the stream
      contextSupplier - a Function ingesting the Message from the given mono returning the Context to set for the MessageStream.Entry the Message is wrapped in
      Returns:
      a stream containing at most one entry from the given mono