Timelimiter is not interrupting the thread

864 Views Asked by At

I am using resilience4j Timelimiter in my project.

The timelimiter is throwing an error if a request is taking more than 10s, but it is not interrupting the thread.

When call comes from postman, i have put the debug and tested, after 10s in postman it displays an exception, but the thread still executes the method and after that added some print statements and it executed as well.

How to cancel or interrupt the thread after 10s in resilience4j.

class A {
TimeLimiterConfig config = TimeLimiterConfig.custom().cancelRunningFuture(true)
      .timeoutDuration(Duration.ofMillis(TimeLimit)).build();

TimeLimiterRegistry timeLimiterRegistry = TimeLimiterRegistry.of(config);

TimeLimiter timeLimiter = timeLimiterRegistry.timeLimiter("APITimelimiter", config);


public Response someMethod() throws Exception {
try {
  timeLimiter.executeFutureSupplier(() -> CompletableFuture.supplyAsync(() -> {
            return getData();
          }));
} catch (Exception e) {
      logger.error("Request has crossed the execution time of " + TimeLimit
          + " seconds");
      throw new Exception("Your request has crossed the execution time of "+ TimeLimit+" seconds.");
    }
}

public UserData getData() {
String jsonData = "";
return jsonData;
}
}
1

There are 1 best solutions below

0
On

TimeLimiter cannot cancel a CompletableFuture. See @TimeLimiter times out slow method but does not cancel running future #905 Points out, that: the limited cancel() in case of CompletableFuture is not a bug, but a design decision. CompletableFuture is not inherently bound to any thread, while Future almost always represents background task.