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)
  .withFallback(asList(CallNotPermittedException.class, BulkheadFullException.class),  
      throwable -> "Hello from fallback")

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

Get Started


Getting started with resilience4j-timelimiter

Create a TimeLimiterRegistry

Just like the CircuitBreaker module, this module provides an in-memory TimeLimiterRegistry which you can use to manage (create and retrieve) TimeLimiter instances.

TimeLimiterRegistry timeLimiterRegistry = TimeLimiterRegistry.ofDefaults();

Create and configure TimeLimiter

You can provide a custom global TimeLimiterConfig. In order to create a custom global TimeLimiterConfig, you can use the TimeLimiterConfig builder. You can use the builder to configure:

  • the timeout duration
  • whether cancel should be called on the running future
TimeLimiterConfig config = TimeLimiterConfig.custom()

// Create a TimeLimiterRegistry with a custom global configuration
TimeLimiterRegistry timeLimiterRegistry = TimeLimiterRegistry.of(config);

// Get or create a TimeLimiter from the registry - 
// TimeLimiter will be backed by the default config
TimeLimiter timeLimiterWithDefaultConfig = registry.timeLimiter("name1");

// Get or create a TimeLimiter from the registry, 
// use a custom configuration when creating the TimeLimiter
TimeLimiterConfig config = TimeLimiterConfig.custom()

TimeLimiter timeLimiterWithCustomConfig = registry.timeLimiter("name2", config);

Decorate and execute a functional interface

TimeLimiter has higher order decorator functions to decorate a CompletionStage or Future in order to limit the execution time.

// Given I have a helloWorldService.sayHelloWorld() method which takes too long
HelloWorldService helloWorldService = mock(HelloWorldService.class);

// Create a TimeLimiter
TimeLimiter timeLimiter = TimeLimiter.of(Duration.ofSeconds(1));
// The Scheduler is needed to schedule a timeout on a non-blocking CompletableFuture
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3);

// The non-blocking variant with a CompletableFuture
CompletableFuture<String> result = timeLimiter.executeCompletionStage(
  scheduler, () -> CompletableFuture.supplyAsync(helloWorldService::sayHelloWorld)).toCompletableFuture();

// The blocking variant which is basically future.get(timeoutDuration, MILLISECONDS)
String result = timeLimiter.executeFutureSupplier(
  () -> CompletableFuture.supplyAsync(() -> helloWorldService::sayHelloWorld));

Updated 5 months ago


Getting started with resilience4j-timelimiter

Suggested Edits are limited on API Reference Pages

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