嵌入Jetty,在给定时间后杀死Request



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

因此,我正在寻找一个选项,它检查请求是否存在超过5分钟,并杀死相应的线程。

我尝试了典型的Jetty选项:

  • maxIdleTime
  • soLingerTime
  • stopTimeout

他们都没有达到预期的效果。还有其他选择可以考虑吗?

您是否可以访问那些需要很长时间才能完成的代码?如果是这样的话,你可以使用可调用和执行器来自己实现这一点,下面是一个单元测试的例子:

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

最新更新