簡體   English   中英

Timelimiter 沒有中斷線程

[英]Timelimiter is not interrupting the thread

我在我的項目中使用了resilience4j Timelimiter。

如果請求耗時超過 10 秒,時間限制器會拋出錯誤,但不會中斷線程。

當調用來自 postman 時,我已經進行了調試和測試,在 postman 中的 10 秒后它顯示一個異常,但線程仍然執行該方法,然后添加一些打印語句並且它也執行了。

10s后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;
}
}

TimeLimiter 無法取消 CompletableFuture。 請參閱@TimeLimiter 超時慢方法但不取消正在運行的未來 #905指出:CompletableFuture 情況下的有限 cancel() 不是錯誤,而是設計決定。 CompletableFuture 本身並不綁定到任何線程,而 Future 幾乎總是代表后台任務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM