我编写了非常简单的控制器来测试 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>
我的连接器 tomcat 如下:
<Connector port="8080" protocol="HTTP/1.1"
maxThreads="5"
acceptCount="5"
connectionTimeout="20000"
redirectPort="8443" />
现在这是有趣的部分。当运行打开 10 个并发连接的简单程序时,我看到第一个连接只有 5 个连接,第二个连接在第一组释放后提供了 5 个连接(您可以从 time stemps 看到它)。这不是 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 connectionTimeout="200000" maxThreads="5" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"/>
它就像魅力一样。我不想这样做。根据 Tomcat 文档,我应该收到没有Http11NioProtocol
连接器的 Servlet 3.0 功能。
怎么了?