The story is that, we have a webapp hosted by Tomcat + Apache Httpd as front-end. The webapp needs an extra long time to process certain REST request (query to the database for a long time then generate a summary after getting the database response).
Under the current implementation, even if the REST client stops the request, the query is still being processed until the database query finishes and the response is tried to be sent back and find out that the output stream is closed. On the other hand, the webapp does not send back anything within a long time, causing the Apache Httpd session times out (we configure 5 minutes as time out).
So what would be the best practice to achieve below goals:
If the client terminates the requests, I want the processing of the request stops, and the query to database terminates as well.
Tomcat can send back premature response if the request has not been terminated by the client but the query already takes a long time.
Update: I thought server may send back 100 continue, but I am wrong. Looks like 100 continue is not intend for that purpose at all.
I am thinking a model like this:
- Introduce a servelet filter to accept the REST request.
- The filter create another thread to invoke the target REST asynchronously, providing an output stream.
- The filter periodically do some sleep and inspect the output stream of the new thread, and sends back 100 continue from time to time, or stream the result from output stream to the client.
Is this workable, or any other preferable approach?