我有一个像这样返回的方法!
Mono<Integer> getNumberFromSomewhere();
我需要一直调用它,直到它没有更多的项目可以发出。那就是我需要将其设为 Flux<Integer>
.
一个选项是添加 repeat
.重点是 - 我想在上述方法发出第一个空信号时停止。
有什么办法吗?我正在寻找一种干净的方式。
最佳答案
执行此操作的内置运算符(尽管它旨在用于“更深层次”的嵌套)是 expand
。
expand
在返回的 Publisher
完成为空时自然停止扩展。
您可以像这样将它应用于您的用例:
//this changes each time one subscribes to it
Mono<Integer> monoWithUnderlyingState;
Flux<Integer> repeated = monoWithUnderlyingState
.expand(i -> monoWithUnderlyingState);
关于spring-webflux - 如何在不为空时重复单声道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68051541/