2

I'm trying to add retry mechanism to a webclient rest call using resilience4j retry which is not working. The method is only getting called once in case of exception. I'm using spring boot 2 with kotlin.

This is the caller

    GlobalScope.launch {
            println(service.callRest())
        }

This is the config

resilience4j.retry:
  configs:
    default:
      maxRetryAttempts: 3
      waitDuration: 100
      retryExceptions:
        - java.lang.IllegalArgumentException
        - java.util.concurrent.TimeoutException
        - org.springframework.web.client.HttpServerErrorException
        - java.io.IOException
        - java.net.UnknownHostException
        - org.springframework.web.reactive.function.client.WebClientResponseException
        - org.springframework.web.reactive.function.client.WebClientResponseException$NotFound
        - org.springframework.web.client.HttpClientErrorException$NotFound
  instances:
    backendA:
      baseConfig: default

this is my method:

    @Retry(name = BACKEND_A)
    suspend fun callRest(): String {
        println("tried calling")
        return webClient.get()
                .uri("/api/v1/dummy1")
                .accept(APPLICATION_JSON)
                .retrieve()
                .awaitBody()
    }

If I throw a hardcoded exception from the method, the retry works correctly

    @Retry(name = BACKEND_A)
    @Throws(WebClientResponseException::class)
    suspend fun callRest(): String {
        println("tried calling")
        throw WebClientResponseException("abc", 404, "abc", null, null, null)

Also, it works with restTemplate

    @Retry(name = BACKEND_A)
    fun callRestTemplate(): String {
        println("tried calling")
        return restTemplate
                .getForObject("/api/v1/dummy1")
    }
dripto
  • 570
  • 1
  • 7
  • 17

1 Answers1

0

Try to return a Future for your asynchronous function.

@Retry(name = BACKEND_A)
fun callRestTemplate(): Future<String> {

Also I cannot see you service declaration but I had the same issue. To add the retry annotation on the class resolved it.

@Retry(name = BACKEND_A)
@Service
class BackendServiceA() {
Blackarrow
  • 36
  • 1
  • 3