`
wiselyman
  • 浏览: 2080678 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
博客专栏
Group-logo
点睛Spring4.1
浏览量:81069
74ae1471-94c5-3ae2-b227-779326b57435
点睛Spring MVC4...
浏览量:130121
社区版块
存档分类
最新评论

09点睛Spring MVC4.1-异步请求处理(包含兼容浏览器的服务器端推送)

 
阅读更多

9.1 异步请求处理

  • Servlet 3开始支持异步请求处理
  • Spring MVC 3.2开始支持Servlet3的这项特性
  • controller可以从另外一个线程返回一个java.util.concurrent.Callable,而不是一个简单的值
    • 此时Servlet容器线程已经释放,可以处理其他的请求
    • Spring MVC通过借助TaskExecutor调起另外一个线程(例子中的mvcTaskExecutor)
  • controller也可以从另外一个线程返回一个DeferredResult
    • 此时,Spring MVC并不知道这个线程的存在
    • 比如一个定时任务

9.2 演示

  • 05点睛Spring MVC 4.1-服务器端推送中,我们也演示了通过SSE实现长连接

    • 但在IE的一些版本是不支持的
  • 本例通过Spring MVC对异步处理的支持来演示长连接的支持,即服务器端推送

    • 支持所有浏览器
  • Servlet开启异步支持

package com.wisely;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {
            AnnotationConfigWebApplicationContext ctx = 
                                     new AnnotationConfigWebApplicationContext();  
            ctx.register(DemoMVCConfig.class); 
            //注册spring mvc的DispatcherServlet
            ctx.setServletContext(servletContext);    
            Dynamic servlet = 
                   servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));  
            servlet.addMapping("/");  
            servlet.setLoadOnStartup(1);
            servlet.setAsyncSupported(true);//此句开启

    }

}
  • DeferredResult所需的定时处理
package com.wisely.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.async.DeferredResult;

@Service
public class AysncService {
    private DeferredResult<String> deferredResult;

    public DeferredResult<String> getAsyncUpdate() {
        deferredResult = new DeferredResult<String>();
        return deferredResult;
    }

    @Scheduled(fixedDelay = 5000)
    public void refresh() {
        if (deferredResult != null) {
            deferredResult.setResult(new Long(System.currentTimeMillis())
                    .toString());
        }
    }


}
  • 开启Spring MVC支持配置:继承WebMvcConfigurerAdapter的配置类DemoMVCConfig
  @Override
  public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
       configurer.setDefaultTimeout(30*1000L); //tomcat默认10秒
       configurer.setTaskExecutor(mvcTaskExecutor());//所借助的TaskExecutor
   }
   @Bean
   public ThreadPoolTaskExecutor mvcTaskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setQueueCapacity(100);
        executor.setMaxPoolSize(25);
        return executor;

    }

     @Override
   public void addViewControllers(ViewControllerRegistry registry) {
       registry.addViewController("/async").setViewName("/async");
   }
  • 测试控制器
package com.wisely.web;

import java.util.concurrent.Callable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;

import com.wisely.service.AysncService;

@Controller
public class AysncController {

    @Autowired
    AysncService aysncService;


    @RequestMapping("/call")
    @ResponseBody
    public Callable<String> asyncCall() {
        //借助mvcTaskExecutor在另外一个线程调用
        //此时Servlet容器线程已经释放,可以处理其他的请求
        return new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(3000);
                return "Async Hello World";
            }
        };
    }

    @RequestMapping("/defer")
    @ResponseBody
    public DeferredResult<String> deferredCall() {
        //调用aysncService的getAsyncUpdate方法
        //deferredResult被计划任务每五秒钟更新一次
        return aysncService.getAsyncUpdate();
    }

}

  • 测试页面
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

</head>
<body>
<input type="button" value="call" onclick="call();"/>
<input type="button" value="deferred" onclick="deferred();"/>


<script type="text/javascript" src="<c:url value="/js/jquery.js" />"></script>
<script type="text/javascript">

function call(){
    $.get('call',function(data){
        console.log(data);
    });
}


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


</script>
</body>
</html>
  • 测试

    • 访问http://localhost:8080/testSpringMVC/async
    • 点击call

    • 点击defer

 

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


2
0
分享到:
评论
7 楼 renchunli 2016-07-27  
确实,你说的对。不过仔细考虑下可能还有些使用场景:1.web服务器能创建的最大线程数往往是有限制的,如果当前访问量比较大,而处理逻辑比较耗时,这时可用的web线程数就会减少,此时把处理交给其他非web线程来处理,就可以腾出更多可用的web线程供其他访问使用,也就是说可以增大吞掉量。2.还可以方便的模拟长连接,如long polling .
6 楼 yuanyonggai123 2015-06-29  
@EnableScheduling
public class AysncService {
明白了,少写了个这
5 楼 yuanyonggai123 2015-06-29  
GET http://localhost:8080/testSpringMVC/defer 500 (Internal Server Error)

public DeferredResult<String> getAsyncUpdate() {
        deferredResult = new DeferredResult<String>();
        deferredResult.setResult(new Long(System.currentTimeMillis())
        .toString());
        return deferredResult;
    }

改成这个后,就不报错了,但是这段代码,没有起到作用
@Scheduled(fixedDelay = 5000)
    public void refresh()
4 楼 yuanyonggai123 2015-06-29  
请问高手楼主,按照代码来的,我这里报错了
GET http://localhost:8080/testSpringMVC/defer 500 (Internal Server Error)send @ jquery-2.1.4.js:8630jQuery.extend.ajax @ jquery-2.1.4.js:8166jQuery.(anonymous function) @ jquery-2.1.4.js:8311deferred @ async:35onclick @ async:13
3 楼 white_crucifix 2015-06-01  
困惑,servlet线程那么快结束,但其实还是开了一个新的线程去做事,开销并没有减少,意义何在?
2 楼 wiselyman 2015-06-01  
wanghaojava 写道
这和WebSocket有啥区别呢


websocket是双向长连接

这个类比于ajax不停向后台刷减少请求数,提高时效性

本例完成一次请求后,需要浏览器再发一次请求

就是我们经常以为的long polling,或者服务器端推送
1 楼 wanghaojava 2015-06-01  
这和WebSocket有啥区别呢

相关推荐

Global site tag (gtag.js) - Google Analytics