完成HttpServletResponse但继续处理 [英] Finishing a HttpServletResponse but continue processing

查看:539
本文介绍了完成HttpServletResponse但继续处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况似乎符合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:


  1. 当应用启动时,创建 ExecutorService ,带 Executors.newFixedThreadPool(numThreads)(还有其他类型)执行者,但我建议从这个开始)

  2. doPost()中,创建一个 Runnable 将执行所需的处理 - 您的任务 - 并将其提交到 ExecutorService ,如下所示: executor.execute(任务)

  3. 最后,您应该返回HTTP状态 202接受 ,如果可能的话,位置标题表示客户可以检查处理状态的位置。

  1. When the app starts up, create an ExecutorService with Executors.newFixedThreadPool(numThreads) (there are other types of executors, but I suggest starting with this one)
  2. In doPost(), create an instance of Runnable which will perform the desired processing - your task - and submit it to the ExecutorService like so: executor.execute(task)
  3. Finally, you should return the HTTP Status 202 Accepted, and, if possible, a Location 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆