I need to implement following behavior:
- Make a REST post request
- If response returns with a status
429 Too many requests
, retry up to 3 times with a delay of 1 second - If the third retry fails or any other error occurs, log and write something to the database
- If the request was successful (http status 200), log some information
I would like to use Spring WebClient for this purpose and came up with this code:
Mono<ClientResponse> response = webClient.post()
.uri(URI.create("/myuri"))
.body(BodyInserters.fromObject(request))
.retrieve()
.onStatus(httpStatus -> httpStatus.equals(HttpStatus.TOO_MANY_REQUESTS),
response -> Mono.error(new TooManyRequestsException("System is overloaded")))
.bodyToMono(ClientResponse.class)
.retryWhen(Retry.anyOf(TooManyRequestsException.class)
.fixedBackoff(Duration.ofSeconds(1)).retryMax(3))
.doOnError(throwable -> saveToDB(some_id, throwable))
.subscribe(response -> logResponse(some_id, response));
Now I would like to test if the retry mechanism and error handling works as I expect. May be I could use StepVerifier for this purpose, but I just cannot figure out how to use it in my case. Any useful hints?