您正在混淆不同的概念。您必须区分:
- 根据Servlet 3.0的异步请求处理 ;一个使您能够将传入的Servlet请求与Web容器线程池分离的API。它不会动态创建任何线程。界面的用户可以实施适当的多线程解决方案。它与非阻塞IO不相关。
- 线程池 ; 提供了一种获取和管理Web容器中的线程的机制。对于 异步请求处理, 您有2个选择。您可以定义自己的文件
ExecutorService
并使用它来进一步处理请求,也可以创建新文件Runnable
并AsyncContext
通过调用将其提交给获得的文件AsyncContext.start()
。对于Tomcat,后一种方法使用中定义的Tomcat线程池server.xml
。 - 非阻塞IO(NIO) ;尽管它也是 异步的 ,但情况有所不同。它涉及非阻塞IO操作,例如磁盘或网络IO。如果要为HTTP请求处理启用NIO,请查看Tomcat的文档。
下面的示例概述了它 如何 工作。它仅使用一个线程进行工作者作业。如果您从2个不同的浏览器并行运行它,则输出如下所示(我使用自定义记录器):
DATE THREAD_ID LEVEL MESSAGE2011-09-03 11:51:22.198 +0200 26 I: >doGet: chrome2011-09-03 11:51:22.204 +0200 26 I: <doGet: chrome2011-09-03 11:51:22.204 +0200 28 I: >run: chrome2011-09-03 11:51:27.908 +0200 29 I: >doGet: firefox2011-09-03 11:51:27.908 +0200 29 I: <doGet: firefox2011-09-03 11:51:32.227 +0200 28 I: <run: chrome2011-09-03 11:51:32.228 +0200 28 I: >run: firefox2011-09-03 11:51:42.244 +0200 28 I: <run: firefox
您会看到
doGet方法立即完成,而工作程序仍在运行。2个测试请求:
http://localhost:8080/pc/TestServlet?name=chrome和
http://localhost:8080/pc/TestServlet?name=firefox。
简单示例Servlet
@WebServlet(asyncSupported = true, value = "/TestServlet", loadonStartup = 1)public class TestServlet extends HttpServlet { private static final Logger LOG = Logger.getLogger(TestServlet.class.getName()); private static final long serialVersionUID = 1L; private static final int NUM_WORKER_THREADS = 1; private ExecutorService executor = null; @Override public void init() throws ServletException { this.executor = Executors.newFixedThreadPool(NUM_WORKER_THREADS); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final String name = request.getParameter("name"); LOG.info(">doGet: " + name); AsyncContext ac = request.startAsync(); // obtain async context ac.setTimeout(0); // test only, no timeout Runnable worker = new TestWorker(name, ac); this.executorService.execute(worker); // ac.start(worker); LOG.info("<doGet: " + name); }}…以及TestWorker
public class TestWorker implements Runnable { private static final Logger LOG = Logger.getLogger(TestWorker.class.getName()); private final String name; private final AsyncContext context; private final Date queued; public TestWorker(String name, AsyncContext context) { this.name = name; this.context = context; this.queued = new Date(System.currentTimeMillis()); } @Override public void run() { LOG.info(">run: " + name); for (int i = 0; i < 100; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } } ServletResponse response = this.context.getResponse(); response.setContentType("text/plain"); try { PrintWriter out = response.getWriter(); out.println("Name:tt" + this.name); out.println("Queued:tt" + this.queued); out.println("End:tt" + new Date(System.currentTimeMillis())); out.println("Thread:tt" + Thread.currentThread().getId()); out.flush(); } catch (IOException e) { throw new RuntimeException(e); } this.context.complete(); LOG.info("<run: " + name); }}


