Embeded Jetty, kill Request after a given time

爱⌒轻易说出口 提交于 2019-12-11 13:06:56

问题


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.

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?


回答1:


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

}


来源:https://stackoverflow.com/questions/36642801/embeded-jetty-kill-request-after-a-given-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!