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

后端 未结 2 682
灰色年华
灰色年华 2021-02-14 13:05

I have a Jetty server handling long running HTTP requests- the responses are generated by an a different process X and end up in a collector hash which Jetty requests periodical

2条回答
  •  广开言路
    2021-02-14 13:40

    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.

提交回复
热议问题