如何在反应器通量排序之前获得订单

问题描述

我有一个喜欢的列表

List<Mono<Integer>> numberMonos = new LinkedList();

我使用这段代码将List转换为Mono,但是因为并发的原因,顺序发生了变化。

Flux.fromIterable(contentPlatformDetailDTOMonos).flatMap(x -> x).collectList();

在反应堆通量排序之前使用此代码转换顺序时,如何获得顺序。

解决方法

请参阅 concatMap() 运算符:https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#concatMap-java.util.function.Function-

对于整个画面,我在这里重复 JavaDoc:

/**
 * Transform the elements emitted by this {@link Flux} asynchronously into Publishers,* then flatten these inner publishers into a single {@link Flux},sequentially and
 * preserving order using concatenation.
 * <p>
 * There are three dimensions to this operator that can be compared with
 * {@link #flatMap(Function) flatMap} and {@link #flatMapSequential(Function) flatMapSequential}:
 * <ul>
 *     <li><b>Generation of inners and subscription</b>: this operator waits for one
 *     inner to complete before generating the next one and subscribing to it.</li>
 *     <li><b>Ordering of the flattened values</b>: this operator naturally preserves
 *     the same order as the source elements,concatenating the inners from each source
 *     element sequentially.</li>
 *     <li><b>Interleaving</b>: this operator does not let values from different inners
 *     interleave (concatenation).</li>
 * </ul>
 *
 * <p>
 * Errors will immediately short circuit current concat backlog.
 *
 * <p>
 * <img class="marble" src="doc-files/marbles/concatMap.svg" alt="">
 *
 * <p><strong>Discard Support:</strong> This operator discards elements it internally queued for backpressure upon cancellation.
 *
 * @param mapper the function to transform this sequence of T into concatenated sequences of V
 * @param <V> the produced concatenated type
 *
 * @return a concatenated {@link Flux}
 */
public final <V> Flux<V> concatMap(Function<? super T,? extends Publisher<? extends V>>
        mapper) {