---
agentTools:
  projectIndex: https://resilience4j.readme.io/llms.txt
---

# TimeLimiter

Getting started with resilience4j-timelimiter

[block:api-header]
{
  "title": "Create a TimeLimiterRegistry"
}
[/block]

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

[block:code]
{
  "codes": [
    {
      "code": "TimeLimiterRegistry timeLimiterRegistry = TimeLimiterRegistry.ofDefaults();",
      "language": "java"
    }
  ]
}
[/block]

[block:api-header]
{
  "title": "Create and configure TimeLimiter"
}
[/block]

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

[block:code]
{
  "codes": [
    {
      "code": "TimeLimiterConfig config = TimeLimiterConfig.custom()\n   .cancelRunningFuture(true)\n   .timeoutDuration(Duration.ofMillis(500))\n   .build();\n\n// Create a TimeLimiterRegistry with a custom global configuration\nTimeLimiterRegistry timeLimiterRegistry = TimeLimiterRegistry.of(config);\n\n// Get or create a TimeLimiter from the registry - \n// TimeLimiter will be backed by the default config\nTimeLimiter timeLimiterWithDefaultConfig = registry.timeLimiter(\"name1\");\n\n// Get or create a TimeLimiter from the registry, \n// use a custom configuration when creating the TimeLimiter\nTimeLimiterConfig config = TimeLimiterConfig.custom()\n   .cancelRunningFuture(false)\n   .timeoutDuration(Duration.ofMillis(1000))\n   .build();\n\nTimeLimiter timeLimiterWithCustomConfig = registry.timeLimiter(\"name2\", config);",
      "language": "java"
    }
  ]
}
[/block]

[block:api-header]
{
  "title": "Decorate and execute a functional interface"
}
[/block]

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

[block:code]
{
  "codes": [
    {
      "code": "// Given I have a helloWorldService.sayHelloWorld() method which takes too long\nHelloWorldService helloWorldService = mock(HelloWorldService.class);\n\n// Create a TimeLimiter\nTimeLimiter timeLimiter = TimeLimiter.of(Duration.ofSeconds(1));\n// The Scheduler is needed to schedule a timeout on a non-blocking CompletableFuture\nScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3);\n\n// The non-blocking variant with a CompletableFuture\nCompletableFuture<String> result = timeLimiter.executeCompletionStage(\n  scheduler, () -> CompletableFuture.supplyAsync(helloWorldService::sayHelloWorld)).toCompletableFuture();\n\n// The blocking variant which is basically future.get(timeoutDuration, MILLISECONDS)\nString result = timeLimiter.executeFutureSupplier(\n  () -> CompletableFuture.supplyAsync(() -> helloWorldService::sayHelloWorld));",
      "language": "java"
    }
  ]
}
[/block]