Tomcat 7的异步处理失败 - 只有一个请求simultanously处理 [英] Tomcat 7 Async Processing failing - only one request processed simultanously

查看:272
本文介绍了Tomcat 7的异步处理失败 - 只有一个请求simultanously处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是想实现使用Servlet API的3.不工作定义异步处理COMET聊天 - 聊天得到受阻,所以我创建的调试servlet来测试只有部分异步

I was trying to implement COMET chat using Async Processing defined in Servlet API 3. It was not working - chat got blocked, so I have created debug servlet to test async part only.

这是我的doGet方法:

This is my doGet method:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.debug("doGet called");
    int timeout = 30 + RandomUtils.nextInt(60);
    String message = RandomStringUtils.randomAlphanumeric(50 + RandomUtils.nextInt(250));
    response.setHeader("Access-Control-Allow-Origin", "*");
    final AsyncContext context = request.startAsync();

    synchronized(items) {
        items.add(new RequestItem(context, message, timeout));
    }
    log.debug("doGet created request and finished");
}

我把在队列中要求的项目,并有一个线程在运行,将采取指定超时后的项目,并发送响应AsyncContext,它打印的消息。问题是,该线程被阻塞,直到AsyncContext得到回应。这是在我的日志可见请求的浏览器4页面加载后:

I'm putting request items in queue, and there's a thread running, that will take the items after specified timeout and send response to AsyncContext, printing message about it. The problem is, the thread is blocked until the AsyncContext gets responded. This is what is visible in my log after requesting 4 page loads in browser:

2011-12-08 13:56:36,923 DEBUG [my.servlet.TestAsyncServlet] doGet called
2011-12-08 13:56:36,952 DEBUG [my.servlet.TestAsyncServlet] doGet created request and finished
2011-12-08 13:57:39,934 TRACE [my.servlet.TestAsyncServlet] respond on item RequestItem [context=org.apache.catalina.core.AsyncContextImpl@175870a, message=zEQpATavzwFl6qIbBKve4OzIY9UUuZBwbqN1TC5KpU3i8LM9B6ChgUqaRmcT2yF, timeout=0]
2011-12-08 13:57:39,962 DEBUG [my.servlet.TestAsyncServlet] doGet called
2011-12-08 13:57:39,962 DEBUG [my.servlet.TestAsyncServlet] doGet created request and finished
2011-12-08 13:58:53,949 TRACE [my.servlet.TestAsyncServlet] respond on item RequestItem [context=org.apache.catalina.core.AsyncContextImpl@88ee03, message=pKHKC632CPIk7hGLV0YqCbQl1qpWIoyNv5OWCp21bEqoni1gbY79HT61QEUS2eCjeTMoNEwdqKzCZNGgDngULysSzVdzFTnQQ5cQ8JvcYnp1pLVqGTueJPWnbRdUuO, timeout=0]
2011-12-08 13:58:53,960 DEBUG [my.servlet.TestAsyncServlet] doGet called
2011-12-08 13:58:53,960 DEBUG [my.servlet.TestAsyncServlet] doGet created request and finished
2011-12-08 13:59:36,954 TRACE [my.servlet.TestAsyncServlet] respond on item RequestItem [context=org.apache.catalina.core.AsyncContextImpl@197950e, message=43FPeEUZWBLqgkAqS3WOFMiHUMVvx6o4jNqWLx8kUvwxqJqpOZyGCtiIcr7yw, timeout=0]
2011-12-08 13:59:36,999 DEBUG [my.servlet.TestAsyncServlet] doGet called
2011-12-08 13:59:36,999 DEBUG [my.servlet.TestAsyncServlet] doGet created request and finished
2011-12-08 14:00:34,957 TRACE [my.servlet.TestAsyncServlet] respond on item RequestItem [context=org.apache.catalina.core.AsyncContextImpl@1cb1278, message=r69Y4NQsyR1vj0kzUlHssic2x1Yrr6T09IGKjWAH1E6Lz4VhFTy9dQHi5CPeTObyjLLBDlCLEDfiyMUnVkVIEgYG7r47Ak4w30RklhzdEi9nthqdfNkry6nyjircsFPX534NqWjI1LwsrGq5nOa3ZYtfjfPVpGlk4KDmWP11L53YntO3GmptZPKa50gcqj9i, timeout=0]

由于这是看到的,接下来的doGet方法被调用后,才previous请求是(理论上异步)回答。所以整个异步事情是不工作!而这里的web.xml中声明:

As it is to see, the next doGet method is called only after previous request is (theoretically asynchronous) answered. So the whole async thing is not working at all! And here's the web.xml declaration:

  <servlet>
    <servlet-name>TestAsyncServlet</servlet-name>
    <servlet-class>my.servlet.TestAsyncServlet</servlet-class>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestAsyncServlet</servlet-name>
    <url-pattern>/test-async</url-pattern>
  </servlet-mapping>

我做的一切作为在Internet上找到。我没有看到来进行错误的地方。我发现没什么特别servlet.xml中来configurure。所以现在的问题是,为什么它不工作,因为它应该?

I'm doing all as to be found in Internet. I don't see the place of mistake to be made. I've found nothing special to configurure in servlet.xml. So the question is, why it is not working as it should?

推荐答案

OK,因为我写的测试PROGRAMM研究的繁衍开到Tomcat连接并没有GET / POST的异步servlet的一部分。我调试,并重新检查我的server.xml配置,有限的线程池测试结果等。现在我的连接器配置看起来像的更好的可见性:

OK, as the part of the research I've written testing programm that opened multiply connections to tomcat and did GET/POST on the async servlet. I've debugged and rechecked my server.xml configuration, limited thread pool for better visibility of test results etc. Now my configuration of connector looks like that:

<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" 
               minProcessors="3"
               maxProcessors="8"
               maxThreads="20"
               connectionTimeout="150000" 
               asyncTimeout="150000" />

和这是工作!我做了使用NIO的测试,并在同一时间取得1000连接,所有的人都得到了一次性处理。

And this is working! I've made test using NIO and made 1000 connections at one time, and all of them got processed in one time.

不过,我所描述的效果,仍然存在于浏览器中。当我尝试在10个标签加载的servlet,先获取加载,比第二名等,这似乎是浏览器的行为,没有东西阻止在服务器上。当我打开3浏览器(火狐,Chrome,Opera)的我有3个连接上一次处理。

However, the effect I've described, still exists in browser. When I try to load servlet on 10 tabs, first get loaded, than second etc. It seems to be browser's behaviour, nothing gets blocked on server. When I opened 3 browsers (Firefox, Chrome, Opera) I've got 3 connections processed on one time.

所以,在Servlet API的3.0作品在Tomcat 7中定义的异步处理,但是,它必须以自己的PROGRAMM浏览器测试与多个标签......在多个标签测试COMET聊天预期,也不能正常工作。然而,在现实生活中的例子一体电脑将打开只有一个连接。而且,浏览器的行为没有任何服务器的故障。

So, the asynchronous processing defined in Servlet API 3.0 works on Tomcat 7, however, it must be tested with own programm not with multiple tabs in browser... testing COMET chat in multiple tabs can also not work as expected. However, in real-life example one computer will open only one connection. And, the browser's behaviour is not any server's fault.

修改
经过Spring MVC的溶液纳入Web应用程序,异步处理模式被叫停(从web.xml中参数被忽略)。然而,异步支持,可以通过手动添加线路设置:

edit After Spring MVC solution was included into web application, async processing mode was stopped (parameter from web.xml was ignored). However, the async support can be set up manually via adding line:

request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);

这篇关于Tomcat 7的异步处理失败 - 只有一个请求simultanously处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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