栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

详解spring mvc对异步请求的处理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解spring mvc对异步请求的处理

在spring mvc3.2及以上版本增加了对请求的异步处理,是在servlet3的基础上进行封装的。

1、修改web.xml



...

1.1、声明version="3.0",声明web-app_3_0.xsd

1.2、为servlet或者filter设置启用异步支持: true ,修改WEB应用的web.xml



SpringMvc
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
...

1
true

2、使controller类支持async

2.1、返回java.util.concurrent.Callable来完成异步处理

package org.springframework.samples.mvc.async;
 
import java.util.concurrent.Callable;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.WebAsyncTask;
 
@Controller
@RequestMapping("/async/callable")
public class CallableController {
  @RequestMapping("/response-body")
  public @ResponseBody Callable callable() {
 
    return new Callable() {
      @Override
      public String call() throws Exception {
 Thread.sleep(2000);
 return "Callable result";
      }
    };
  }
 
  @RequestMapping("/view")
  public Callable callableWithView(final Model model) {
 
    return new Callable() {
      @Override
      public String call() throws Exception {
 Thread.sleep(2000);
 model.addAttribute("foo", "bar");
 model.addAttribute("fruit", "apple");
 return "views/html";
      }
    };
  }
 
  @RequestMapping("/exception")
  public @ResponseBody Callable callableWithException(
      final @RequestParam(required=false, defaultValue="true") boolean handled) {
 
    return new Callable() {
      @Override
      public String call() throws Exception {
 Thread.sleep(2000);
 if (handled) {
   // see handleException method further below
   throw new IllegalStateException("Callable error");
 }
 else {
   throw new IllegalArgumentException("Callable error");
 }
      }
    };
  }
 
  @RequestMapping("/custom-timeout-handling")
  public @ResponseBody WebAsyncTask callableWithCustomTimeoutHandling() {
 
    Callable callable = new Callable() {
      @Override
      public String call() throws Exception {
 Thread.sleep(2000);
 return "Callable result";
      }
    };
 
    return new WebAsyncTask(1000, callable);
  }
 
  @ExceptionHandler
  @ResponseBody
  public String handleException(IllegalStateException ex) {
    return "Handled exception: " + ex.getMessage();
  }
 
}

2.2、在异步处理完成时返回org.springframework.web.context.request.async.DeferredResult其他线程,例如一个JMS或一个AMQP消息,Redis通知等等:

@RequestMapping("/quotes")
@ResponseBody
public DeferredResult quotes() {
 DeferredResult deferredResult = new DeferredResult();
 // Add deferredResult to a Queue or a Map...
 return deferredResult;
}
  
// In some other thread...
deferredResult.setResult(data);
// Remove deferredResult from the Queue or Map

3、spring配置文件的修改

spring mvc的dtd的声明必须大于等于3.2



  

 实际使用示例:

function deferred(){
  $.get('quotes.htm',function(data){
    console.log(data);
    deferred();//每次请求完成,再发一次请求,避免客户端定时刷新来获取数据
  });
}

这么做的好处避免web server的连接池被长期占用而引起性能问题,调用后生成一个非web的服务线程来处理,增加web服务器的吞吐量~~

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148248.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号