gpt4 book ai didi

java - 嵌入式 Jetty,在给定时间后终止请求

转载 作者:行者123 更新时间:2023-11-30 07:27:24 25 4
gpt4 key购买 nike

我运行一个带有嵌入式 Jetty 的 jar。有时,一个请求会陷入某种无限循环。显然修复无限循环将是最好的选择。然而,目前这是不可能的。

所以我正在寻找一个选项,检查请求是否存在超过例如5分钟,杀死对应的线程。

我尝试了典型的 Jetty 选项:

  • 最大空闲时间
  • soLingerTime
  • 停止超时

它们都没有按预期工作。还有其他选择可以考虑吗?

最佳答案

您是否可以访问需要很长时间才能完成的代码?如果是这样,您可以使用 callable 和 Executor 自己实现此目的,下面是带有示例的单元测试:

@Test
public void timerTest() throws Exception
{
//create an executor
ExecutorService executor = Executors.newFixedThreadPool(10);

//some code to run
Callable callable = () -> {
Thread.sleep(10000); //sleep for 10 seconds
return 123;
};

//run the callable code
Future<Integer> future = (Future<Integer>) executor.submit(callable);

Integer value = future.get(5000, TimeUnit.MILLISECONDS); //this will timeout after 5 seconds

//kill the thread
future.cancel(true);

}

关于java - 嵌入式 Jetty,在给定时间后终止请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36642801/

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