javaretrofit2aopresilience4j

Retry and circuit breaker both with retrofit 2 and resillence4j


I know the default order of resillence4j is Retry(CircuitBreaker(func())) .... But I am in need of something like this CircuitBreaker(Retry(func())).... And I am on retrofit2. Is it acheivable? If so, how can I acheive this?

My retry and circuit breaker:

 public Retry getRetry() {

    if (retry == null) {

        retry = Retry.of(APP_NAME, RetryConfig.custom()
                .maxAttempts(3)
                .waitDuration(Duration.ofSeconds(3))
                .retryExceptions(IOException.class, TimeoutException.class, ServiceException.class)
                .failAfterMaxAttempts(true)
                .build());

        log.debug("New instance of resilience4j retry created");
    }

    return retry;
}

public CircuitBreaker getCircuitBreaker() {

    if (circuitBreaker == null) {

        circuitBreaker = CircuitBreaker.of(APP_NAME, CircuitBreakerConfig.custom()
                .failureRateThreshold(50)
                .minimumNumberOfCalls(5)
                .automaticTransitionFromOpenToHalfOpenEnabled(true)
                .waitDurationInOpenState(Duration.ofSeconds(3))
                .permittedNumberOfCallsInHalfOpenState(3)
                .slidingWindowType(COUNT_BASED)
                .slidingWindowSize(10)
                .slowCallRateThreshold(50)
                .slowCallDurationThreshold(Duration.ofSeconds(5))
                .recordExceptions(IOException.class, TimeoutException.class, ServiceException.class)
                .build());

        log.debug("New instance of resilience4j circuit breaker created");
    }

    return circuitBreaker;
}

And my retrofit builder:

protected void initializeRetrofit() {

    retrofitBuilder.baseUrl(baseUrl());

    Retrofit retrofit = retrofitBuilder
            .addCallAdapterFactory(RetryCallAdapter.of(retrofitCallerConfigurar.getRetry()))
            .addCallAdapterFactory(CircuitBreakerCallAdapter.of(retrofitCallerConfigurar.getCircuitBreaker()))
            .addConverterFactory(GsonConverterFactory.create(dateTimeFormat()))
            .client(retrofitCallerConfigurar.getOkHttpClient())
            .build();

    api = retrofit.create(getApiClient());
}

Solution

  • Apparently no solution was found. So, assuming it is not possible and moving to spring webclient.