spring-webflux - 如何在不为空时重复单声道

标签 spring-webflux project-reactor

我有一个像这样返回的方法!

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/

相关文章:

stream - 使用WebFlux的响应zip文件

spring - 如何使用 Reactor (Spring WebClient) 做重复调用?

java - 如何在 Spring Webflux Java 中记录请求正文

Spring Boot Webflux/Netty - 检测关闭的连接

java - Flux 根据日期时间属性发出元素

spring-boot - Spring WebFlux 认证的 WebSocket 连接

spring-boot - Spring WebFlux Reactive 和 Kotlin Coroutines 启动报错

spring - 如何检查 Mono 是否为空?

java - 在 Vert.x 应用程序中使用 Project Reactor

java - Schedulers.newElastic 和 Schedulers.elastic 方法有什么区别?