SpringMvc4x高级配置(四) 服务器端推送技术之S

一. 点睛

在前面的文章SpringMvc4.x高级配置(三):服务器端推送技术之SSE中已经介绍了服务器端推送技术的第一种方案,下面演示第二种服务器端推送技术,基于Servlet3.0+异步方法处理。

二. 示例

1.开启异步方法支持

在文件WebInitializer的方法onStartup末尾增加以下代码开启异步方法支持,代码如下:

1
复制代码servlet.setAsyncSupported(true);//①

添加完成之后的代码如下所示:

1
2
3
4
5
复制代码 
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); //③
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
servlet.setAsyncSupported(true);//①

代码解释:

① 此句开启异步方法支持。

  1. 演示控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
复制代码package org.light4j.springMvc4.web;

import org.light4j.springMvc4.service.PushService;
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;

@Controller
public class AysncController {
@Autowired
PushService pushService; //①

@RequestMapping("/defer")
@ResponseBody
public DeferredResult<String> deferredCall() { //②
return pushService.getAsyncUpdate();
}
}

代码解释:

异步任务实现是通过控制器从另外一个线程返回一个DeferredResult,这里的DeferredResult是从pushService中获得的。
① 定时任务,定时更新DeferredResult
② 返回给客户端DeferredResult

  1. 定时任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
复制代码package org.light4j.springMvc4.service;

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

@Service
public class PushService {
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());
}
}
}

代码解释:

① 在PushService里产生DeferredResult给控制器使用,通过@Scheduled注解的方法定时更新DeferredResult

  1. 演示页面

src/main/resources下新建async.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
复制代码<%@ 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>servlet async support</title>

</head>
<body>

<script type="text/javascript" src="assets/js/jquery.js"></script>
<script type="text/javascript">

deferred();//①

function deferred(){
$.get('defer',function(data){
console.log(data); //②
deferred(); //③
});
}

</script>
</body>
</html>

代码解释:

此处的代码使用的是JQueryAjax请求,所以没有浏览器兼容性问题。
① 页面打开就向后台发送请求。
② 在控制台输出服务端推送的数据。
③ 一次请求完成后再向后台推送数据。

  1. 配置

在文件MyMvcConfig上使用注解@EnableScheduling开启计划任务的支持,代码如下:

1
2
3
4
5
6
7
复制代码@Configuration
@EnableWebMvc// ①
@EnableScheduling
@ComponentScan("org.light4j.springMvc4")
public class MyMvcConfig extends WebMvcConfigurerAdapter {

}

在文件MyMvcConfig的方法addViewControllers添加viewController映射访问演示页面async.jsp,代码如下:

1
复制代码registry.addViewController("/async").setViewName("/async");

添加完成之后的代码如下所示:

1
2
3
4
5
6
7
8
9
复制代码
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("/index");
registry.addViewController("/toUpload").setViewName("/upload");
registry.addViewController("/converter").setViewName("/converter");
registry.addViewController("/sse").setViewName("/sse");
registry.addViewController("/async").setViewName("/async");
}
  1. 运行

访问http://localhost/springMvc4.x-servlet3/async,可以看到网络不断的在获取服务器端推送的消息,如下图所示:

查看浏览器控制台可以看到不断的在打印消息,如下图所示:

三. 源代码示例:

github地址:点击查看
码云地址:点击查看

打赏 欢迎关注人生设计师的微信公众账号
公众号ID:longjiazuoA

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%