In my Sprint boot application, I am calling an external API to validate some data, whenever API is down or there is some network delay, I get an SSLHandShakeException/Exception and then it reconnects. I want to add a retry logic that can try for 5 times whenever there is an exception. I tried the following way, I wanted to know if there is a better way of doing this.
public Mono<ResponseObj> getInfo(String xxxxx) {
return serviceCall.findBy(xxxx)
.map()
.map()
.switchIfEmpty(Mono.defer(() -> fetchResponse(xxxxx)));
}
private Mono<ResponseObj> fetchResponse(String xxxxx)
return getInfo(xxxxx)
.onErrorResume(e -> throwError(xxxxx))
.retryWhen(Retry.fixedDelay(5, Duration.ofSeconds(20))
.filter(this::is5xxServerError))
.map(res -> proceedOrThrowError(xxxx))
.flatMap()
.map()
.map();
}
private Mono<ResponseObj> getInfo(String xxxx)
return ThrowingFunction
.unchecked(x ->
webClient.get()
.uri(uriBuilder -> uriBuilder
.path(url + "/" + xxxx)
.build())
.bodyToMono(ResponseObj.class))
.apply(xxxx);
}
private boolean is5xxServerError(Throwable throwable) {
return throwable instanceof WebClientResponseException &&
((WebClientResponseException) throwable).getStatusCode().is5xxServerError();
}