从spring3.2开始,支持servlet3的异步请求,这对于处理耗时的请求如缓慢的数据库查询是非常有好处的,不至于很快的耗光servlet的线程池,影响可扩展性。
让我们先来了解一下servlet是怎么处理异步操作的:

  1. 通过调用request.startAsync(),ServletRequest就变成异步模式。主要的影响是Servlet、Filter会退出,但是Response保持打开用来完成请求处理。
  2. 调用request.startAsync()返回AsyncContext实例,可进一步控制异步处理。例如,它提供dispatch方法,可
    以从应用线程调用以分发请求回Servlet容器。异步调度和forward的方式是相似的,但是异步调度是由应用线程到Servlet容器线程产生的,
    而forward的方式是同时发生在相同的Servlet容器线程。
  3. ServletRequest提供当前的DispatcherType,它可以用来在异步调度中区分正在处理的初始请求线程是Servlet还是Filter。

接着来看一下callable是怎么处理异步操作的:

  1. Controller返回一个Callable实例;
  2. Spring MVC开始异步处理并在一个单独的线程中,提交Callable实例给TaskExecutor处理;
  3. DispatcherServlet和所有的Filter退出请求线程但是response保存打开;
  4. Callable返回结果,Spring MVC分发请求回Servlet容器;
  5. DispatcherServlet重新调用并继续处理从Callable实例中异步返回的结果。

现在来看一下代码是怎么实现的

1、pom.xml

  1. <dependency>
  2. <groupId>javax.servlet</groupId>
  3. <artifactId>javax.servlet-api</artifactId>
  4. <version>3.1.0</version>
  5. </dependency>
  6.  
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-context-support</artifactId>
  10. <version>3.2.13.RELEASE</version>
  11. </dependency>
  12.  
  13. <dependency>
  14. <groupId>org.springframework</groupId>
  15. <artifactId>spring-webmvc</artifactId>
  16. <version>3.2.13.RELEASE</version>
  17. </dependency>
 

2、applicationContext-mvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:task="http://www.springframework.org/schema/task"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  10. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
  11. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  12. ">
  13.  
  14. <context:component-scan base-package="com.test"/>
  15.  
  16. <mvc:annotation-driven>
  17. <!-- 配置超时时间 -->
  18. <mvc:async-support default-timeout="3000">
  19. <!-- 这里可以配置callable或者deferred-result拦截器 -->
  20. </mvc:async-support>
  21. </mvc:annotation-driven>
  22. </beans>

3、web.xml  , 当有 很多 filter 时,filter 也一定要添加异步支持

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  6. version="3.0">
  7.  
  8. <filter>
  9. <filter-name>characterEncodingFilter</filter-name>
  10. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  11. <async-supported>true</async-supported>
  12. <init-param>
  13. <param-name>encoding</param-name>
  14. <param-value>UTF-8</param-value>
  15. </init-param>
  16. <init-param>
  17. <param-name>forceEncoding</param-name>
  18. <param-value>true</param-value>
  19. </init-param>
  20. </filter>
  21.  
  22. <filter-mapping>
  23. <filter-name>characterEncodingFilter</filter-name>
  24. <url-pattern>/*</url-pattern>
  25. </filter-mapping>
  26.  
  27. <servlet>
  28. <servlet-name>DispatcherServlet</servlet-name>
  29. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  30. <init-param>
  31. <param-name>contextConfigLocation</param-name>
  32. <param-value>classpath*:applicationContext-mvc.xml</param-value>
  33. </init-param>
  34. <load-on-startup>1</load-on-startup>
  35. <async-supported>true</async-supported>
  36. </servlet>
  37.  
  38. <servlet-mapping>
  39. <servlet-name>DispatcherServlet</servlet-name>
  40. <url-pattern>/</url-pattern>
  41. </servlet-mapping>
  42. </web-app>

web.xml需要声明web-app_3_0.xsdversion=”3.0″,启用异步支持<async-supported>true</async-supported>
4、CallableController.java

  1. package com.test.controller;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.ModelMap;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.servlet.ModelAndView;
  8.  
  9. import java.util.concurrent.Callable;
  10.  
  11. /**
  12. * spring实现方式:
  13. * 1、把任务提交给Executor异步执行
  14. */
  15. @Controller
  16. public class CallableController {
  17.  
  18. @RequestMapping(value = "/callable1", method = RequestMethod.GET)
  19. public Callable<String> callable1(final ModelMap modelMap) {
  20. return new Callable<String>() {
  21. public String call() throws Exception {
  22. Thread.sleep(2 * 1000L); //暂停2秒return "hello callable";
  23. }
  24. };
  25. }
  26. }

5,tomcat 部分,如果 tomcat 部署有 session  分页式缓存插件,则在插件配置的地方,也要添加异步支持:

  1. <!-- redis 缓存支持 -->
    <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" asyncSupported="true" />
  2. <Manager className="com.radiadesign.catalina.session.RedisSessionManager"
  3. host="localhost"
  4. port="6379"
  5. database="0"
  6. maxInactiveInterval="60" />

6, 启动服务,测试:

http://localhost:8088/test/callable1

  1.  

7, rsp:

hello callable

Spring MVC 异步测试的更多相关文章

  1. Spring mvc异步处理

    基于Servlet3.0的异步处理,springmvc的异步处理 控制器返回callable, spring mvc异步处理,将callable提交到TaskExecutor  使用一个隔离线程进行执 ...

  2. Spring MVC 异步处理请求,提高程序性能

    原文:http://blog.csdn.net/he90227/article/details/52262163 什么是异步模式 如何在Spring MVC中使用异步提高性能? 一个普通 Servle ...

  3. Spring MVC如何测试Controller(使用springmvc mock测试)

    在springmvc中一般的测试用例都是测试service层,今天我来演示下如何使用springmvc mock直接测试controller层代码. 1.什么是mock测试? mock测试就是在测试过 ...

  4. Spring MVC的测试

    测试是保证软件质量的关键. 与 Spring MVC 相关的测试,主要涉及控制器的测试. 为了测试Web项目通常不需要启动项目,需要一些Servlet相关的一些模拟对象,比如MockMVC.MockH ...

  5. spring mvc 异步 DeferredResult

    当一个请求到达API接口,如果该API接口的return返回值是DeferredResult,在没有超时或者DeferredResult对象设置setResult时,接口不会返回,但是Servlet容 ...

  6. Spring MVC 异步请求 Callable

    对于有的请求业务处理流程可能比较耗时,比如长查询,远程调用等,主线程会被一直占用,而tomcat线程池线程有限,处理量就会下降 servlet3.0以后提供了对异步处理的支持,springmvc封装了 ...

  7. spring mvc 和ajax异步交互完整实例

    Spring MVC 异步交互demo: 1.jsp页面: <%@ page language="java" contentType="text/html; cha ...

  8. spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例

    在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...

  9. Unit Testing of Spring MVC

    试验1:做的条目不发现首先,我们必须确保我们的应用是工作性质所做条目不发现.我们可以写的测试以确保通过以下步骤: 1.配置的模拟对象时抛出一个todonotfoundexception findbyi ...

随机推荐

  1. DB2创建视图view

    create view v_table1(col1,col2,col3...)--视图名(字段一,字段二,字段三...) as --后跟查询语句 select col1,col2,col3... fr ...

  2. 大数据入门第二十四天——SparkStreaming(二)与flume、kafka整合

    前一篇中数据源采用的是从一个socket中拿数据,有点属于“旁门左道”,正经的是从kafka等消息队列中拿数据! 主要支持的source,由官网得知如下: 获取数据的形式包括推送push和拉取pull ...

  3. 20155202张旭 Exp7 网络欺诈技术防范

    20155202张旭 Exp7 网络欺诈技术防范 基础问题回答 通常在什么场景下容易受到DNS spoof攻击? 在同一局域网下比较容易受到DNS spoof攻击,攻击者可以冒充域名服务器,来发送伪造 ...

  4. arm学习之汇编跳转指令总结

    目前所知道的跳转指令有 b,bl,bep,bne.他们共同点是都是以b开头,首先从字面上分析:b:是Branch,表示分支.bl:是Branch Link表示带连接的分支.bep:Branch ,Eq ...

  5. tkinter 弹出窗口 传值回到 主窗口

    有些时候,我们需要使用弹出窗口,对程序的运行参数进行设置.有两种选择 一.标准窗口 如果只对一个参数进行设置(或者说从弹出窗口取回一个值),那么可以使用simpledialog,导入方法: from ...

  6. 【第五课】LNMP环境的入门

    目录 一. LNMP环境介绍 二.Mysql的二进制免编译安装 三.PHP 7.2.5编译部署 四.Nginx的编译安装 五.YUM安装Nginx 一. LNMP环境介绍 LNMP(Linux + N ...

  7. 【干货】YUM安装PHP 7版本后,增加phalcon框架的报错解决

    目录 1.yum安装php 7.x版本,此处部署7.3版本 2.安装phalcon框架 2.1.PHP版本依赖关系 2.2.编译phalcon扩展模块 2.3.增加扩展文件 3.部署phalcon遇到 ...

  8. linux下的yum命令详细介绍

    yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能够从指定的服务器自动下载RP ...

  9. python常用算法实现

    排序是计算机语言需要实现的基本算法之一,有序的数据结构会带来效率上的极大提升. 1.插入排序 插入排序默认当前被插入的序列是有序的,新元素插入到应该插入的位置,使得新序列仍然有序. def inser ...

  10. C#_父窗体跟子窗体的控件操作

    很多人都苦恼于如何在子窗体中操作主窗体上的控件,或者在主窗体中操作子窗体上的控件.相比较而言,后面稍微简单一些,只要在主窗体中创建子窗体的时候,保留所创建子窗体对象即可. 下面重点介绍前一种,目前常见 ...