Search code examples
resilience4j

Trouble with Resilience4j Retry and "java.net.http.HttpClient" working together


I'm trying to get a basic "httpclient" "httprequest" "httpresponse" working with Resilience4j Retry.

The verbatim code from : https://resilience4j.readme.io/docs/retry

  RetryConfig config = RetryConfig.custom()
  .maxAttempts(5)
  .waitDuration(Duration.ofMillis(1000))
  .retryOnResult(response -> response.getStatus() == 500)
  .retryOnException(e -> e instanceof WebServiceException)
  .retryExceptions(IOException.class, TimeoutException.class)
  .ignoreExceptions(BusinessException.class, OtherBusinessException.class)
  .build();

// Create a RetryRegistry with a custom global configuration
RetryRegistry registry = RetryRegistry.of(config);

// Get or create a Retry from the registry - 
// Retry will be backed by the default config
Retry retryWithDefaultConfig = registry.retry("name1");

Note, their code above misses defining the generic "T", like this:

  RetryConfig config = RetryConfig.<MyConcrete>custom()

and the verbatim code from : https://resilience4j.readme.io/docs/examples

Supplier<String> supplierWithResultAndExceptionHandler = SupplierUtils
  .andThen(supplier, (result, exception) -> "Hello Recovery");

Supplier<HttpResponse> supplier = () -> httpClient.doRemoteCall();
Supplier<HttpResponse> supplierWithResultHandling = SupplierUtils.andThen(supplier, result -> {
    if (result.getStatusCode() == 400) {
       throw new ClientException();
    } else if (result.getStatusCode() == 500) {
       throw new ServerException();
    }
    return result;
});
HttpResponse httpResponse = circuitBreaker
  .executeSupplier(supplierWithResultHandling);

======

So using those 2 "partials" , I've come up with this.

Note, I am using some "real" java.net.http.HttpClient and java.net.http.HttpResponse (from JDK11)

import io.github.resilience4j.core.SupplierUtils;
import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.RetryConfig;
import io.github.resilience4j.retry.RetryRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

import javax.inject.Inject;
import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;

public final class ResilientHttpClient /* implements IResilientHttpClient */ {

    private static Logger logger;

    private final HttpClient httpClient;

    @Inject
    public ResilientHttpClient(final HttpClient httpClient) {
        this(LoggerFactory
                .getLogger(ResilientHttpClient.class), httpClient);
    }

    /**
     * Constructor, which pre-populates the provider with one resource instance.
     */
    public ResilientHttpClient(final Logger lgr,
                               final HttpClient httpClient) {
        if (null == lgr) {
            throw new IllegalArgumentException("Logger is null");
        }
        this.logger = lgr;

        if (null == httpClient) {
            throw new IllegalArgumentException("HttpClient is null");
        }

        this.httpClient = httpClient;

    }

    public String executeHttpRequest(String circuitbreakerInstanceName, HttpRequest httpRequest) {

        try {

            /* circuitbreakerInstanceName  is future place holder for .yml configuration see : https://resilience4j.readme.io/docs/getting-started-3 */

        RetryConfig config = RetryConfig.<HttpResponse>custom()
                    .waitDuration(Duration.ofMillis(1000))
                    .retryOnResult(response -> response.statusCode() == 500)
                    .retryOnException(e -> e instanceof ArithmeticException)
                    .retryExceptions(IOException.class, TimeoutException.class)
                    //.ignoreExceptions(BusinessException.class, OtherBusinessException.class)
                    .build();

            // Create a RetryRegistry with a custom global configuration
            RetryRegistry registry = RetryRegistry.of(config);

            // Get or create a Retry from the registry -
            // Retry will be backed by the default config
            Retry retryWithDefaultConfig = registry.retry(circuitbreakerInstanceName);

            Supplier<HttpResponse> supplier = () -> this.httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());

            Supplier<String> supplierWithResultAndExceptionHandler = SupplierUtils
                    .andThen(supplier, (result, exception) -> "Hello Recovery");

            Supplier<HttpResponse> supplierWithResultHandling = SupplierUtils.andThen(supplier, result -> {
                if (result.statusCode() == HttpStatus.BAD_REQUEST.value()) {
                    throw new RuntimeException("400");
                } else if (result.statusCode() == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
                    throw new RuntimeException("500");
                }
                return result;
            });

            HttpResponse<String> response = retryWithDefaultConfig.executeSupplier(supplierWithResultHandling);

            String responseBody = response.body();

            return responseBody;

        } catch (Exception ex) {
            throw new RuntimeException((ex));
        }
    }

}

The issue I am having is:

The line:

Supplier<HttpResponse> supplier = () - > this.httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());

is giving an error (in intelliJ) of "unhandled exceptions" "IOException, InterruptedException"

So modifying the method to be:

 public String executeHttpRequest(String circuitbreakerInstanceName, HttpRequest httpRequest) throws IOException, InterruptedException {

"feels wrong". But even when I try it...it doesn't resolve anything. :(

It is probably some lamda checked-exception voodoo.

But more to the point:

So I don't know if the way I've brought together the 2 partials is even correct. The samples are a little lacking in the fully-working area.

Thank for any help. Getting a basic httpclient "retry" a few times shouldn't be too hard. But I'm hitting my head against the wall.

My gradle dependencies.

dependencies {

    implementation group: 'javax.inject', name: 'javax.inject', version: javaxInjectVersion
    implementation group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion

    implementation group: 'org.springframework', name: 'spring-web', version: springWebVersion

    implementation "io.github.resilience4j:resilience4j-circuitbreaker:${resilience4jVersion}"
    implementation "io.github.resilience4j:resilience4j-ratelimiter:${resilience4jVersion}"
    implementation "io.github.resilience4j:resilience4j-retry:${resilience4jVersion}"
    implementation "io.github.resilience4j:resilience4j-bulkhead:${resilience4jVersion}"
    implementation "io.github.resilience4j:resilience4j-cache:${resilience4jVersion}"
    implementation "io.github.resilience4j:resilience4j-timelimiter:${resilience4jVersion}"


    testCompile group: 'junit', name: 'junit', version: junitVersion
}

and

   resilience4jVersion = '1.5.0'
    slf4jVersion = "1.7.30"
    javaxInjectVersion = "1"
 springWebVersion = '5.2.8.RELEASE'
    junitVersion = "4.12"

Solution

  • just out of interest:

    • Which Java version are you using? Java 11?
    • Why can't you use Spring Boot? The Resilience4j Spring Boot starter simplifies the configuration a lot.

    If you configure retryOnResult(response -> response.getStatus() == 500), you don't have to use SupplierUtils anymore to map a HttpResponse with a certain status code to a runtime exception.

    RetryConfig config = RetryConfig.<HttpResponse<String>>custom()
                .waitDuration(Duration.ofMillis(1000))
                .retryOnResult(response -> response.statusCode() == 500)
                .retryExceptions(IOException.class, TimeoutException.class)
                .build();
    

    Please don't create Registries and Configs inside of executeHttpRequest, but inject them into your Constructor.

    You can create a static method like this:

    public static <T> HttpResponse<T> executeHttpRequest(Callable<HttpResponse<T>> callable, Retry retry, CircuitBreaker circuitBreaker) throws Exception {
            return Decorators.ofCallable(callable)
                .withRetry(retry)
                .withCircuitBreaker(circuitBreaker)
                .call();
    }
    

    and invoke the method as follows:

    HttpResponse<String> response = executeHttpRequest(
        () -> httpClient.send(request, HttpResponse.BodyHandlers.ofString()), 
        retry, 
        circuitBreaker);