How to handle Jetty exception - a long running HTTP request times out, but the process it calls never terminates and Jetty is unhappy

℡╲_俬逩灬. 提交于 2019-12-03 13:02:01

The problem here is not your call of AsyncContext#complete() but the overal design of the code.

Continuations (same thing for Servlet async) is designed to be asynchronous. The while loop that makes use of the internal continuations timeout must not be here. You are transforming an asynchronous design to a synchronous one by doing this. The right thing to do is to register a listener using Continuation#addContinuationListener() and implements onTimeout() method to process the timeout case appropriately.

Once your timeout logic is out, I would recommend to move the process X logic to the class AsyncHTTPRequestProcessor and move out from the need of using a collector. During the processing, you should assume that the current thread will never be timed out. By doing this your call to complete() makes sens and you will be immune to concurrency trouble on the collector.

Using try catch block might help in this case.

   try{
      ctx.complete()
   } catch (IllegalStateException e){
      //Handle it the way you prefer.
   }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!