Tomcat 7.0.32 + Spring MVC Servlet 3 Async无法正常工作

时间:2013-05-30 22:40:59

标签: spring tomcat servlets spring-mvc servlet-3.0

我编写了非常简单的控制器来测试Servlet 3的功能:

@Autowired
    ThreadPoolTaskExecutor taskExecutor;

    @RequestMapping(value="{name}", method = RequestMethod.GET)
    public @ResponseBody DeferredResult<MyResponse> getShopInJSON(@PathVariable String name) {

        DeferredResult<MyResponse> df = new DeferredResult<MyResponse>();
        taskExecutor.submit(new MyRunnable(df));    

        return df; 
    }

在单独的线程中我只做了5秒睡眠命令,之后我将MyResult POJO返回DeferredResult

我的web.xml文件符合Servlet 3规范:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0"
         metadata-complete="true">
  <display-name>Archetype Created Web Application</display-name>
    <servlet>
    <async-supported>true</async-supported>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

My Connector tomcat如下:

 <Connector port="8080" protocol="HTTP/1.1"
                maxThreads="5"
                acceptCount="5"
                connectionTimeout="20000"
                redirectPort="8443" />

现在这是有趣的部分。当运行打开10个并发连接的简单程序时,我看到第一个集合被释放后,首先只提供5个连接,然后提供第二个5个连接(您可以从时间间隔看到它)。这不是Servlet 3.0的行为方式

Fri May 31 01:17:57 IDT 2013: Preparing 10 concurrent connections
Fri May 31 01:18:02 IDT 2013: Output from Server int thread 9 :{"props1":"param1","props2":"param1"}
Fri May 31 01:18:02 IDT 2013: Output from Server int thread 8 :{"props1":"param1","props2":"param1"}
Fri May 31 01:18:02 IDT 2013: Output from Server int thread 4 :{"props1":"param1","props2":"param1"}
Fri May 31 01:18:02 IDT 2013: Output from Server int thread 7 :{"props1":"param1","props2":"param1"}
Fri May 31 01:18:02 IDT 2013: Output from Server int thread 2 :{"props1":"param1","props2":"param1"}
Fri May 31 01:18:07 IDT 2013: Output from Server int thread 1 :{"props1":"param1","props2":"param1"}
Fri May 31 01:18:07 IDT 2013: Output from Server int thread 0 :{"props1":"param1","props2":"param1"}
Fri May 31 01:18:07 IDT 2013: Output from Server int thread 5 :{"props1":"param1","props2":"param1"}
Fri May 31 01:18:07 IDT 2013: Output from Server int thread 6 :{"props1":"param1","props2":"param1"}
Fri May 31 01:18:07 IDT 2013: Output from Server int thread 3 :{"props1":"param1","props2":"param1"}

如果将Tomcat Connector更改为

   <Connector connectionTimeout="200000" maxThreads="5" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"/>

它就像魅力一样。我不想这样做。根据Tomcat文档,我应该接收没有Http11NioProtocol连接器的Servlet 3.0功能。

有什么问题?

2 个答案:

答案 0 :(得分:7)

问题是由于Tomcat配置中的 maxThreads = 5 设置。

对于非NIO情况,此设置不仅限制请求处理线程的最大数量,还限制最大连接数!

由于您尚未指定maxConnections,因此它正在为maxConnections选择默认值。 以下是Tomcat doc关于如何选择maxConnections的默认值的摘录:

  

maxConnections:服务器的最大连接数   将在任何给定时间接受和处理。这个号码已经过了   到达后,服务器将不再接受任何连接   连接数低于此值。操作系统可以   仍然接受基于acceptCount设置的连接。默认   值因连接器类型而异。 对于BIO,默认值为   maxThreads 除非使用Executor,否则默认值为   是来自执行者的maxThreads的值。 对于NIO,默认为   10000 即可。对于APR / native,默认值为8192。

您可以显式指定 maxConnections =“10”(例如)设置以覆盖此默认行为。然后,您应该看到无论使用哪种连接器,都可以获得10个并行请求。我试过这个并且有效。

答案 1 :(得分:0)

您是否尝试过将maxThreads增加到10?看起来您的Tomcat连接器的上限为5个线程