如果通量为空,如何调用方法(消费者),例如,如果通量为空,我想记录消息
这个为您服务:
/**
* 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>
我看到两种实现方法:
switchIfEmpty()
然后您的代码段将具有以下表示形式:
.switchIfEmpty(Flux.empty().doOnComplete(() -> log.info("there were no elements")))
另一个选项是hasElements()
,您可以将其放置在链的末尾。
.hasElements()
.doOnNext(hasElements -> {
if (!hasElements) {
log.info("there were no elements");
}
})