关于 tomcat nio connector, servlet 3.0 async, spring mvc async 的关系
tomcat 的 org.apache.coyote.http11.Http11NioProtocol Connector 是一个使用 Java NIO 实现的异步 accept 请求的 connector
它的作用是不需要为每个请求建立一个线程, 而是使用固定的accept线程 accept 多个请求, 然后排队处理.
大概的意思是使用固定的 acceptThread (网络 IO 线程, 负责 accept, read, write) 来异步处理(nio accept, read, write) n 个请求, 然后将请求入队, 最后使用固定的 requestProcessingThread (业务线程) 来处理业务逻辑, 业务逻辑的处理实际上也是同步的
Http11NioProtocol 从 tomcat 6.x 开始支持, 其中文档中提到 read request body 以及 write response body 是 sim-blocking (simulated blocking), 也就是说在 read request body 以及 wirte response body 的时候, 不会在读到部分请求内容, 或者只写除了部分相应内容时候就给用户返回结果(例如只有收到完整的request才会触发 servlet 的 get() 或者 post() 方法), 但是实际上这个同步 api 的底层使用 nio 的异步 api 实现的, 所以叫做 simulated blocking. 同时, 它实际上还是异步的网络io读写, 所以它实际上是单个线程同时处理多个网络 io 请求的
servlet 3.0 从 tomcat 7.x 开始支持, 它使得业务处理也可以异步, Spring 3.2 的 async 变成也是基于 servlet 3.0 来做的, 例如
@ResponseBody
@RequestMapping("/{surl}")
public DeferredResult<ResponseEntity<String>> redirect(@PathVariable String surl) {
final DeferredResult<ResponseEntity<String>> dr = new DeferredResult<ResponseEntity<String>>(TIMEOUT_MS, ResponseEntityResult.TIMEOUT); // ... 异步的做一些事情, 最后将结果 set 到 DeferredResult
new Thread(new AsyncTask(dr)).start(); return dr;
} private static class AsyncTask implements Runnable { private DeferredResult result; private AsyncTask(DeferredResult result) {
this.result = result;
} @Override
public void run() {
// 业务逻辑
// ...
// 设置结果
result.setResult(result);
}
}
这样 tomcat 的 requestProcessingThread 可以解脱出去处理别的请求, 而且 servlet-2.5 你只能把最后结果处理出来, 最后 return, 而不能放到别的线程处理
关于 tomcat nio connector, servlet 3.0 async, spring mvc async 的关系的更多相关文章
- Java EE - Servlet 3.0 和 Spring MVC
Table of Contents 前言 基于 Java 的配置 ServletContainerInitializer 动态配置 DispatcherServlet 和 ContextLoaderL ...
- SpringMVC(八):使用Servlet原生API作为Spring MVC hanlder方法的参数
在SpringMVC开发中,是有场景需要在Handler方法中直接使用ServletAPI. 在Spring MVC Handler的方法中都支持哪些Servlet API作为参数呢? --Respo ...
- Spring3.0之后->Spring MVC过滤器-HiddenHttpMethodFilter
浏览器form表单只支持GET与POST请求,而DELETE.PUT等method并不支持,spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET.POST.PUT ...
- 【Spring学习笔记-MVC-1.0】Spring MVC架构介绍
作者:ssslinppp 1. 核心架构图 2. 核心架构的具体流程步骤 3. 具体的核心开发步骤 4. 常用注解 5. <mvc:annotation-driven>配置 6 ...
- Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用
Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...
- Tomcat 6.0.32 +Spring dbcp datasource关闭Tomcat出现严重异常
异常如下: 信息: Pausing Coyote HTTP/ -- :: org.apache.catalina.core.StandardService stop 信息: Stopping serv ...
- Tomcat配置和Spring MVC配置
Tomcat启动时,先找系统变量CATALINA_BASE,如果没有,则找CATALINA_HOME.然后找这个变量所指的目录下的conf文件夹,从中读取配置文件.最重要的配置文件:server.xm ...
- 编写简单的spring mvc程序,在tomcat上部署
编写简单的spring mvc程序,在tomcat上部署 1 用java 配置spring mvc ,可以省去web.xmlpackage hello;import org.springframewo ...
- JSP、Servlet和Spring MVC
感谢原博主!!!https://blog.csdn.net/whut2010hj/article/details/80874008 版权声明:本文为博主原创文章,遵循CC 4.0 BY版权协议,转载请 ...
随机推荐
- SpringMVC学习系列-后记 开启项目的OpenSessionInView
在系列的 SpringMVC学习系列(12) 完结篇 的示例项目中,由于当时考虑到OpenSessionInView会对性能有一定的影响,所以就没有配置项目的OpenSessionInView.在ma ...
- 创建MySQL 用户
create user 'lixianming'@'localhost' identified by 'lxm123456'; grant all on art.* to 'lixianming'@' ...
- Hello Dojo!(翻译)
http://dojotoolkit.org/documentation/tutorials/1.10/hello_dojo/index.html 欢迎学习DOJO!在本教程中,你将学些到如何加载DO ...
- (C#) Tasks 中的异常处理(Exception Handling.)
多线程编程中要注意对线程异常的处理.首先写个例子. 一个线程用于显示信息(Show Messages).主线程用于做其他工作(Do Works). using (Task taskShowMessag ...
- NeHe OpenGL教程 第四十七课:CG顶点脚本
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- 透视校正插值(Perspective-Correct Interpolation)
在渲染器光栅化每个三角形的过程中,需要对根据顶点属性对三角形进行扫描线插值.此时由于投影面上顶点的2D坐标与顶点属性不成线性关系,因此是不能简单地使用线性插值来计算顶点属性的. 此时应当利用透视校正插 ...
- ORACLE 10G 闪回建议
Flashback Tips The following tips and restrictions apply to using flashback features. Flashback Tips ...
- The listener supports no services解决一例
The listener supports no services解决一例 Listener动态监听静态监听注册实例 今天做Advacned Replication实验的时候碰到一个问题,启动目标 ...
- 使用opencv设置图像的格式以及帧率
最近楼主正在写一个关于图像存储的程序,LZ有一颗求知心,想要了解保存的图像的格式以及获取摄像头帧率.晚些时候会写一篇关于opencv获取摄像头并且保存每帧图像信息方法. 1.修改图像的像素显示: 首先 ...
- mongodb转换List实体(去掉多余的字段) 批量添加
Gson gson = new Gson(); String str = "list集合json字符串"; JsonObject object = gson.fromJson(st ...