gpt4 book ai didi

java - 如何在 Spring Boot REST API 上设置超时?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:33:29 26 4
gpt4 key购买 nike

我有一些 REST API 可能需要一段时间才能执行,我想限制它们的执行时间。最好是,如果 30 秒过去了并且请求没有返回,我想返回一个特定的 HTTP 代码/数据并完全终止该请求。

当前代码:

@RestController
@CrossOrigin(origins = {"*"}, maxAge = 4800, allowCredentials = "false")
public class APIController {

@RequestMapping(value = "/api/myapifunc", method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<?> optimize(@RequestParam(value="param1", defaultValue="")) {
// Code here
}

最佳答案

您似乎在描述 Circuit Breaker pattern .如果您可以控制客户端和服务器代码并想探索 Spring Cloud 和 Netflix Hysterix 库,您可以查看 Getting Started: Circuit Breaker指导。

如果您使用 Apache Tomcat 作为您的 servlet 容器,您可以配置 Stuck Thread Detection Valve :

This valve allows to detect requests that take a long time to process, which might indicate that the thread that is processing it is stuck. Additionally it can optionally interrupt such threads to try and unblock them.

When such a request is detected, the current stack trace of its thread is written to Tomcat log with a WARN level.

The IDs and names of the stuck threads are available through JMX in the stuckThreadIds and stuckThreadNames attributes. The IDs can be used with the standard Threading JVM MBean (java.lang:type=Threading) to retrieve other information about each stuck thread.

关于java - 如何在 Spring Boot REST API 上设置超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54080658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com