servlet3.0 异步请求

servlet3.0异步请求的好处

提高吞吐, 减少tomcat线程数量

代码相关

1.返回 Callable 来完成异步处理

@RequestMapping(method=RequestMethod.POST)
public Callable<String> processUpload(final MultipartFile file) {
  return new Callable<String>() {
    public Object call() throws Exception {
      // ...
      return "someView";
    }
  };
}

2.返回 DeferredResult

@RequestMapping("/quotes")
@ResponseBody
public DeferredResult quotes() {
  DeferredResult deferredResult = new DeferredResult();
  // Add deferredResult to a Queue or a Map...
  return deferredResult;
}

// In some other thread..
// Set the return value on the deferredResult

deferredResult.set(data);

区别:
1.Callable:springMVC开启一个Callable新线程,利用TaskExecutor管理新产生的线程
2.第二种:自己管理线程池.

配置相关

1.tomcat7.0及以上;
2.spring 3.2及以上;

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
       default-autowire="byName">


<mvc:annotation-driven>
   <mvc:async-support default-timeout="3000"/>
</mvc:annotation-driven>

3.web.xml 配置async-supported

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <filter>
        <async-supported>true</async-supported>
    </filter>

    <servlet>
        <async-supported>true</async-supported>
    </servlet>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值