时间限制器不会中断线程

发布于 2025-01-11 21:06:05 字数 1027 浏览 4 评论 0原文

我在我的项目中使用resilience4j Timelimiter。

如果请求花费的时间超过 10 秒,时间限制器会抛出错误,但不会中断线程。

当调用来自邮递员时,我已经进行了调试和测试,在邮递员中10秒后它显示异常,但线程仍然执行该方法,之后添加了一些打印语句并且它也执行了。

如何在resilience4j中10秒后取消或中断线程。

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;
}
}

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;
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷 2025-01-18 21:06:05

TimeLimiter 无法取消 CompletableFuture。请参阅 @TimeLimiter 超时慢速方法但不取消运行 future #905 指出,即:CompletableFuture 中有限的 cancel() 不是一个错误,而是一个设计决策。 CompletableFuture 本身并不绑定到任何线程,而 Future 几乎总是代表后台任务。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文