Tomcat 7.0.32 +Spring MVC Servlet 3 Async 不工作 [英] Tomcat 7.0.32 +Spring MVC Servlet 3 Async is not working

查看:23
本文介绍了Tomcat 7.0.32 +Spring MVC Servlet 3 Async 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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; 
    }

在单独的线程中,我只执行 5 秒睡眠命令,然后将 MyResult POJO 返回到 DeferredResult.

In separate Thread I'm doing nothing but 5 second sleep command and after it I return MyResult POJO to DeferredResult.

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

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>

我的连接器tomcat如下:

My Connector tomcat is the following:

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

现在这是有趣的部分.当运行打开 10 个并发连接的简单程序时,我看到只有 5 个连接被第一次提供,第二个 5 个连接在第一组被释放后提供(你可以从时间茎上看到它).这不是 Servlet 3.0 应该有的行为

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"}

如果将Tomcat Connector改为

If change Tomcat Connector to

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

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

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.

怎么了?

推荐答案

问题是由于 Tomcat 配置中的 maxThreads=5 设置造成的.

The problem is due to the maxThreads=5 setting in your Tomcat config.

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

For the non-NIO case, this setting does not only restrict the maximum number of request processing threads but also restricts the maximum number of connections !

由于您没有指定 maxConnections,它为 maxConnections 选择一个默认值.以下是 Tomcat doc 关于它如何选择默认值的摘录maxConnections 的值:

Since you have not specified maxConnections, it is choosing a default value for maxConnections. Here is the excerpt from Tomcat doc on how it chooses the default value for maxConnections:

maxConnections : 服务器的最大连接数将在任何给定时间接受和处理.当这个数字被到达,服务器将不再接受任何连接,直到连接数低于此值.操作系统可能仍然接受基于 acceptCount 设置的连接.默认值因连接器类型而异.对于 BIO,默认值为maxThreads 除非使用 Executor 在这种情况下默认将是来自执行程序的 maxThreads 的值.对于 NIO,默认是10000.对于 APR/native,默认为 8192.

maxConnections : The maximum number of connections that the server will accept and process at any given time. When this number has been reached, the server will not accept any more connections until the number of connections falls below this value. The operating system may still accept connections based on the acceptCount setting. Default value varies by connector type. For BIO the default is the value of maxThreads unless an Executor is used in which case the default will be the value of maxThreads from the executor. For NIO the default is 10000. For APR/native, the default is 8192.

您可以明确指定一个 maxConnections="10"(例如)设置来覆盖此默认行为.然后您应该会看到,无论使用何种连接器,您都可以处理 10 个并行请求.我试过了,它有效.

You can explicitly specify a maxConnections="10" (for example) setting to override this default behavior. You should then see that you can get 10 parallel requests getting processed regardless of the Connector used. I tried this and it works.

这篇关于Tomcat 7.0.32 +Spring MVC Servlet 3 Async 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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