完成HttpServletResponse但继续处理 [英] Finishing a HttpServletResponse but continue processing
问题描述
我的情况似乎符合Async Servlet 3.0 / Comet情况但我需要做的就是在接受传入参数后返回200响应代码(或其他)。
I have a situation that seems to fit the Async Servlet 3.0 / Comet situation but all I need to do is return a 200 response code (or other) after accepting the incoming parameters.
HttpServlet有没有办法完成http请求/响应握手并继续处理?
Is there a way for a HttpServlet to complete the http request/response handshake and yet continue processing?
类似于......
doPost( req, response ) {
// verify input params...
response.setStatus( SC_OK );
response.close();
// execute long query
}
编辑:查看javax。 servlet包 - 我的问题的正确措辞是
Looking at the javax.servlet package - the proper phrasing to my question is
如何提交响应?
与Servlet一样.isCommitted()
as in Servlet.isCommitted()
推荐答案
以下是我处理这种情况的方法:
Here's how I've handled this situation:
- 当应用启动时,创建
ExecutorService
,带Executors.newFixedThreadPool(numThreads)
(还有其他类型)执行者,但我建议从这个开始) - 在
doPost()
中,创建一个Runnable
将执行所需的处理 - 您的任务 - 并将其提交到ExecutorService
,如下所示:executor.execute(任务)
- 最后,您应该返回HTTP状态
202接受
,如果可能的话,位置
标题表示客户可以检查处理状态的位置。
- When the app starts up, create an
ExecutorService
withExecutors.newFixedThreadPool(numThreads)
(there are other types of executors, but I suggest starting with this one) - In
doPost()
, create an instance ofRunnable
which will perform the desired processing - your task - and submit it to theExecutorService
like so:executor.execute(task)
- Finally, you should return the HTTP Status
202 Accepted
, and, if possible, aLocation
header indicating where a client will be able to check up on the status of the processing.
我高度建议您阅读 Java Concurrency in Practice ,这是一本非常棒且非常实用的书。
I highly recommend you read Java Concurrency in Practice, it's a fantastic and very practical book.
这篇关于完成HttpServletResponse但继续处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!