首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用WebFlux的短轮询

使用WebFlux的短轮询
EN

Stack Overflow用户
提问于 2021-03-05 16:58:13
回答 1查看 261关注 0票数 0

我有这个post方法:

代码语言:javascript
运行
复制
webClientBuilder
                .build()
                .get()
                .uri("uri")
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .onStatus(HttpStatus::isError, response -> Mono.error(new CustomException("Response is in status: ".concat(response.statusCode().toString()))))
                .bodyToMono(GetResponse.class)
                .log()
                .flatMap(response ->
                        Mono.fromSupplier(() -> updateMember(entity.getId(), getScore(response)))
                                .subscribeOn(Schedulers.boundedElastic()))
                .block();

我应该在它上面创建一个简短的轮询,因为我调用的api可能会延迟应答我,我应该能够调用它几次,可能每3/5秒。外部服务总是会给我一个答案,但我必须验证答案中的某个特定字段是否为空。我最多需要重复5次,如果在5次之后返回null,我会将null传递给我的方法(updateMember)。

我正在尝试这样的东西:

代码语言:javascript
运行
复制
webClientBuilder
                .build()
                .get()
                .uri("uri")
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .onStatus(HttpStatus::isError, response -> Mono.error(new CustomException("response is in status: ".concat(response.statusCode().toString()))))
                .bodyToMono(GetResponse.class)
                .filter(response -> Objects.nonNull(response.getAnag().getSummary()))
                .repeatWhenEmpty(Repeat.onlyIf(repeatContext -> true)
                        .exponentialBackoff(Duration.ofSeconds(5), Duration.ofSeconds(10)).timeout(Duration.ofSeconds(30)))
                .delaySubscription(Duration.ofSeconds(10))
                .flatMap(response -> Mono.fromSupplier(() -> updateMember(entity.getId(), response.getAnag().getSummary()))
                        .subscribeOn(Schedulers.boundedElastic()))
                .block();

我想重复最多5次,只有当答案中的值为空时,否则我会直接传递该值。

你能帮帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2021-03-05 17:46:03

如果你因为延迟而得到一个超时异常,你可以在链中添加一个‘.retry()’运算符。也许这会有帮助:

代码语言:javascript
运行
复制
.retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(3)))

Example from reactor reference documentation

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66489528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档