resilience4j

Resilience4j is a fault tolerance library for Java™

Resilience4j is a lightweight fault tolerance library inspired by Netflix Hystrix, but designed for functional programming.
Resilience4j provides higher-order functions (decorators) to enhance any functional interface, lambda expression or method reference with a Circuit Breaker, Rate Limiter, Retry or Bulkhead. You can stack more than one decorator on any functional interface, lambda expression or method reference. The advantage is that you have the choice to select the decorators you need and nothing else.

Supplier<String> supplier = () -> service.sayHelloWorld(param1);

String result = Decorators.ofSupplier(supplier)
  .withBulkhead(Bulkhead.ofDefaults("name"))
  .withCircuitBreaker(CircuitBreaker.ofDefaults("name"))
  .withRetry(Retry.ofDefaults("name"))
  .withFallback(asList(CallNotPermittedException.class, BulkheadFullException.class),  
      throwable -> "Hello from fallback")
  .get()

With Resilience4j you don’t have to go all-in, you can pick what you need.

Get Started

Retrofit

A type-safe HTTP client for Android and Java. https://square.github.io/retrofit/

Circuit Breaking

Circuit breaking http client calls is based upon the CircuitBreaker instance provided to a CircuitBreakerCallAdaptor.

// Create a CircuitBreaker
private final CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");

// Create a retrofit instance with CircuitBreaker call adapter
Retrofit retrofit = new Retrofit.Builder()
                .addCallAdapterFactory(CircuitBreakerCallAdapter.of(circuitBreaker))
                .baseUrl("http://localhost:8080/")
                .build();

// Get an instance of your service with circuit breaking built in.
RetrofitService service = retrofit.create(RetrofitService.class);

Timeouts

To trigger circuit breaking by timeout, the time thresholds should be set on a OkHttpClient instance passed into the Retrofit.Builder.

// Create a CircuitBreaker
private final CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");

final long TIMEOUT = 300; // ms
OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
        .readTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
        .writeTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
        .build();

Retrofit retrofit = new Retrofit.Builder()
        .addCallAdapterFactory(CircuitBreakerCallAdapter.of(circuitBreaker))
        .baseUrl("http://localhost:8080/")
        .client(client)
        .build();

Error responses

Retrofit retrofit = new Retrofit.Builder()
        .addCallAdapterFactory(CircuitBreakerCallAdapter.of(circuitBreaker, (r) -> r.code() < 500));
        .baseUrl("http://localhost:8080/")
        .build();

Rate Limiting

Rate limiting of http client calls is based upon the configuration passed to the RateLimiterCallAdaptor.

RateLimiter rateLimiter = RateLimiter.ofDefaults("testName");

Retrofit retrofit = new Retrofit.Builder()
        .addCallAdapterFactory(RateLimiterCallAdapter.of(rateLimiter))
        .baseUrl("http://localhost:8080/")
        .build();

If the number of calls are exceeded within the period defined by the RateLimiter, a HTTP 429 response (too many requests) will be returned.

Updated 5 months ago

Retrofit


A type-safe HTTP client for Android and Java. https://square.github.io/retrofit/

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.