resilience4j Retry retrofit

822 Views Asked by At

is there a way to add the retry configuration in a similar way to circuitBreacker?

val circuitBreakerConfig = CircuitBreakerConfig.custom()
        .slidingWindowType(CircuitBreakerConfig.SlidingWindowType.COUNT_BASED)
        .failureRateThreshold(10.0F)
        .build()

    val circuitBreaker = CircuitBreaker.of("name", circuitBreakerConfig)
 
    val retrofit = Retrofit.Builder()
        .baseUrl(baseUrl)
        .addCallAdapterFactory(
            CircuitBreakerCallAdapter.of(circuitBreaker) {
                it.code() == 500
            }
        ).addConverterFactory(GsonConverterFactory.create())
        .client(httpClient)
        .build()

The retry configuration I wanted to use is this :

val retryConfig = RetryConfig.custom<Any>()
        .maxAttempts(3)
        .waitDuration(Duration.ofSeconds(120))
        .retryExceptions(RuntimeException::class.java)
        .build()
1

There are 1 best solutions below

0
On

Yes you can

private Retrofit buildRetrofitWithRetry(
        final OkHttpClient okHttpClient, final String baseUrl) {
    return new Retrofit.Builder()
            .client(okHttpClient)
            .addCallAdapterFactory(RetryCallAdapter.of(retry))
            .baseUrl(baseUrl)
            .addConverterFactory(JacksonConverterFactory.create())
            .build();
}