助焊剂(或类似物),如果空的助焊剂

问题描述 投票:0回答:2

如果通量为空,如何调用方法(消费者),例如,如果通量为空,我想记录消息

spring-boot flux reactive reactor
2个回答
1
投票

这个为您服务:

/**
 * Add behavior (side-effect) triggered when the {@link Flux} completes successfully.
 * <p>
 * <img class="marble" src="doc-files/marbles/doOnComplete.svg" alt="">
 *
 * @param onComplete the callback to call on {@link Subscriber#onComplete}
 *
 * @return an observed  {@link Flux}
 */
public final Flux<T> doOnComplete(Runnable onComplete) {

请参阅此JavaDocs:

/**
 * Represents an empty publisher which only calls onSubscribe and onComplete.
 * <p>
 * This Publisher is effectively stateless and only a single instance exists.
 * Use the {@link #instance()} method to obtain a properly type-parametrized view of it.
 * @see <a href="https://github.com/reactor/reactive-streams-commons">Reactive-Streams-Commons</a>
 */
final class FluxEmpty extends Flux<Object>

0
投票

我看到两种实现方法:

  1. 用途switchIfEmpty()

然后您的代码段将具有以下表示形式:

.switchIfEmpty(Flux.empty().doOnComplete(() -> log.info("there were no elements")))
  1. 另一个选项是hasElements(),您可以将其放置在链的末尾。

    .hasElements()
    .doOnNext(hasElements -> {
        if (!hasElements) {
            log.info("there were no elements");
        }
    })
    
© www.soinside.com 2019 - 2024. All rights reserved.