Class MessageStreamResolverUtils
MessageHandler
into the expected corresponding MessageStream.
Whether a returned Iterable represents several results or one result that happens to be a
collection cannot be derived from the value itself. Callers therefore choose the cardinality up front:
resolveToStream(Object, MessageTypeResolver) spreads an Iterable over as many
Messages as it holds elements, while resolveToSingleStream(Object, MessageTypeResolver)
carries it as the Message.payload() of a single Message. Both unwrap asynchronous containers such as
CompletableFuture first, so that List<T> and CompletableFuture<List<T>> yield the same
cardinality.
This utility class currently has a drawback, which is that it only takes the "top level" type into account.
Differently put, if we receive a Mono<Message> as the given result, we will push that
Message through the given typeResolver and make it the Message.payload().
This is a known limitation that will be supported in due time.
- Since:
- 5.0.0
- Author:
- Simon Zambrovski, Steven van Beelen
-
Method Summary
Modifier and TypeMethodDescriptionstatic MessageStream<?> resolveToSingleStream(@Nullable Object result, MessageTypeResolver typeResolver) Resolves the givenresultinto aMessageStreamcarrying at most oneMessage, using thetypeResolverwhen thatMessageis constructed to define theMessageType.static MessageStream<?> resolveToStream(@Nullable Object result, MessageTypeResolver typeResolver) Resolves the givenresultinto aMessageStreamthat may carry severalMessages, using thetypeResolverwhen aMessageis constructed to define theMessageType.
-
Method Details
-
resolveToStream
public static MessageStream<?> resolveToStream(@Nullable Object result, MessageTypeResolver typeResolver) Resolves the givenresultinto aMessageStreamthat may carry severalMessages, using thetypeResolverwhen aMessageis constructed to define theMessageType.Is able to switch between
Optional,CompletableFuture,Iterable,Stream,Mono, andFlux. AnIterableorStreamis spread over as manyMessagesas it holds elements, also when wrapped in aCompletableFutureorOptional. If none of the above apply,MessageStream.just(Message)will be used. If the givenresultisnull,MessageStream.empty()is returned.Use
resolveToSingleStream(Object, MessageTypeResolver)for handlers that produce exactly one result, as those must carry a returned collection as a whole instead of spreading it.- Parameters:
result- The result to map into aMessageStream.typeResolver- TheMessageTypeResolverused to resolve theMessageTypeforMessagesthat are held in the returnedMessageStream.result- the result to map into aMessageStreamtypeResolver- theMessageTypeResolverused to resolve theMessageTypeforMessagesthat are held in the returnedMessageStream- Returns:
- a
MessageStreambased on the givenresult
-
resolveToSingleStream
public static MessageStream<?> resolveToSingleStream(@Nullable Object result, MessageTypeResolver typeResolver) Resolves the givenresultinto aMessageStreamcarrying at most oneMessage, using thetypeResolverwhen thatMessageis constructed to define theMessageType.Behaves like
resolveToStream(Object, MessageTypeResolver), except that a returnedIterablebecomes theMessage.payload()of a singleMessagerather than being spread over oneMessageper element. This suits handlers that produce exactly one result, such ascommand handlers, for which spreading would mean silently discarding all but the first element.- Parameters:
result- the result to map into aMessageStreamtypeResolver- theMessageTypeResolverused to resolve theMessageTypefor theMessagethat is held in the returnedMessageStream- Returns:
- a
MessageStreambased on the givenresult
-