我有这个post方法:
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)。
我正在尝试这样的东西:
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次,只有当答案中的值为空时,否则我会直接传递该值。
你能帮帮我吗?
发布于 2021-03-05 17:46:03
如果你因为延迟而得到一个超时异常,你可以在链中添加一个‘.retry()’运算符。也许这会有帮助:
.retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(3)))
https://stackoverflow.com/questions/66489528
复制相似问题