嵌入码头,在给定时间后杀死请求 [英] Embeded Jetty, kill Request after a given time

查看:117
本文介绍了嵌入码头,在给定时间后杀死请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行带有嵌入式Jetty的罐子.有时,一个请求会陷入一个无限循环中.显然,修复无限循环将是最佳选择.但是,这目前是不可能的.

I run a jar with an embedded Jetty. From time to time it happens that one request get stuck in some endless loop. Obviously fixing the endless-loop would be the best option. However, this is currently not possible.

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

So I am looking for an option, that checks if a request exists for more than e.g. 5 minutes, and kills the corresponding thread.

我尝试了典型的码头选项:

I tried the typical Jetty options:

  • maxIdleTime
  • soLingerTime
  • stopTimeout

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

None of them worked as expected. Is there another option to consider?

推荐答案

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

Do you access to the code that kicks of the code which takes too long to complete? If so you can use callable and an Executor to achieve this yourself, below is a unit test with an example:

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

}

这篇关于嵌入码头,在给定时间后杀死请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆