我试图创建一个自定义重试方法,但我不明白它是如何工作的。
事实上,我只想再试几次,然后,如果所有尝试都失败了,返回一个异常,其中包含最后一个http状态代码、http状态消息和响应正文。
Webflux可以做到这一点吗?
下面是我的代码:
private RetryBackoffSpec retryServerError() {
return Retry.backoff(3, Duration.of(100, ChronoUnit.MILLIS))
.filter(this::isServerError)
.onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> new HttpServerErrorException(HttpStatus.SERVICE_UNAVAILABLE, "an error message"));
}
private boolean isServerError(Throwable throwable) {
return throwable instanceof HttpServerErrorException;
}
public void ResponseEntity<U> post(Class<Void> responseClass, Function<UriBuilder, URI> uri, MyBodyObject body) throws WebClientException, RestClientResponseException {
return this.getWebClientBuilder()
.build()
.post()
.uri(uri)
.body(BodyInserters.fromValue(body))
.retrieve()
.toEntity(responseClass)
.retryWhen(retryServerError())
.block();
}
谢谢你的帮助!
1条答案
按热度按时间e3bfsja21#
retrySignal允许您获取导致重试的异常。