Spring Webclient : Lecture 3 - Retry Failed HTTP Calls using Spring WebClient

preview_player
Показать описание
In this lecture, we will code and explore "How to perform a Retry for the failed HTTP calls in a Functional approach".

The Complete source code is available in the below link.

Рекомендации по теме
Комментарии
Автор

You sir are very underrated! So good content. This account needs to blow up!

mitulvaghela
Автор

This guys is so underrated but this guy also should sent to prison for not lunching a full springboot course. I am doing his lamda series but then tried to find if he has a springboot course but really frusted that he did not but he is all about reactive springboot

deeplife
Автор

Hey Dilip, just ran a marathon on your lectures on webclient and it made me understand how it works better, thank you. Could you do a lecture on how to set a "connection timed out" after a given amount of milliseconds and how to break the call and raise a specific exception? I`m trying to create a scenario where the back-end does not respond or it takes longer than usual, and i`d like to interrupt the call after a given time if the server does not respond.

remenyiczn
Автор

Hi bro... I need to carry the same headers of the initial request to the retryable requests as well... Please provide me the information 🙏

neutral
Автор

Hello Dilip,

Its a very nice lecture. I have a scenerio where I have need to retry a request when I get Unauthorized error. I have a access token to pass for each API request which is specific to individual user this access token is valid for 1 day, but sometimes during rare case it is rejected before the expiry time and Unauthorized error is received. In that case I have to retry the request with a new access token.

The new access token is received by making another Api request by passing a refresh_token value for the user. Now before retry I have to make this request and get the new access_token then with this new access_token make the retry call. I tried using the retry function but it seems to get stucked while making the request to get new access token and then throw the Unauthorized error.

Would you be able to help me with that....

private Retry<?> fixedRetry(Token token) {
return Retry.onlyIf(context -> context.exception() instanceof ClientResponseException && ((ClientResponseException) ==
//
.retryOnce()
.doOnRetry(objectRetryContext ->
//calls a api request to get new token
);
}

public Mono<Abc> create(Token token, final CreateAbc createabc) {

return client.post()
.uri(PATH_V2)
.header(AUTHORIZATION, token.bearer())


.body(fromObject(createabc))
.retrieve()
.onStatus(HttpStatus::isError, response ->
.flatMap(error -> Mono.error(new ClientResponseException(response.statusCode().value(), response.statusCode(), error))))
.bodyToMono(Abc.class)



}

public Mono<Token> forceRefreshToken(Token token) {
return
}

public Mono<Token> refresh(Token token) {

return client.post()
.uri(OAUTH_TOKEN_PATH)
.header(AUTHORIZATION, basicAuth())
.body(forRefreshToken(new
.retrieve()
.onStatus(HttpStatus::is4xxClientError, response ->
.flatMap(error -> Mono.error(new ClientResponseException(response.statusCode().value(), response.statusCode(), error))))
.onStatus(HttpStatus::is5xxServerError, response ->
.flatMap(error -> Mono.error(new ClientResponseException(response.statusCode().value(), response.statusCode(), error))))
.bodyToMono(Token.class)
;
}

Thanks a lot

littlestarsam
Автор

Hi Dilip, thx for the helpful content. When I add a request logger to the webclient it only outputs one request although 3 retries are executed. This cannot be correct surely? Greetings from Germany

mwsaab
Автор

It would be nice if u can update ur git code base to the updated Retry api - reactor.util.retry.Retry

ravivedala
join shbcf.ru