I wrote very simple controller which test Servlet 3 features:
@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;
}
In separate Thread I'm doing nothing but 5 second sleep command and after it I return MyResult POJO to DeferredResult.
My web.xml file is according to Servlet 3 specifications:
<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 is the following:
<Connector port="8080" protocol="HTTP/1.1"
maxThreads="5"
acceptCount="5"
connectionTimeout="20000"
redirectPort="8443" />
Now this is the interesting part. When running simple program which opens 10 concurrent connection I see that only 5 connections are served first and second 5 connections are served after first set is released (You can see it from time stemps). This is not how Servlet 3.0 should behave
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"}
If change Tomcat Connector to
<Connector connectionTimeout="200000" maxThreads="5" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"/>
it works like charm. I don't want to do it. According to Tomcat docs I should receive Servlet 3.0 functionality without Http11NioProtocol connector.
What is wrong?
All connectors release the thread to handle other requests once startAsync has been called.Have you tried this with AsyncContext instead of DeferredResult ? May be spring is introducing some behavior here ! - 2020