0
点赞
收藏
分享

微信扫一扫

Spring Boot 异步请求(Servlet 3.0)

沐之轻语 2022-02-25 阅读 106


在Spring 3.2 及以后版本中增加了对请求的异步处理,旨在提高请求的处理速度降低服务性能消耗。

在我们的请求中做了耗时处理,当并发请求的情况下,为了避免web server的连接池被长期占用而引起性能问题,调用后生成一个非web的服务线程来处理,增加web服务器的吞吐量。

为此 Servlet 3.0 新增了请求的异步处理,Spring 也在此基础上做了封装处理。

本文还是以代码例子的方式说明如何在 Spring Boot 中应用异步请求。

首先说一下几个要点:

1、@WebFilter 和 @WebServlet 注解中的 asyncSupported = true 属性

异步处理的servlet若存在过滤器,则过滤器的注解@WebFilter应设置asyncSupported=true,

否则会报错 A filter or servlet of the current chain does not support asynchronous operations.

2、@EnableAsync 注解

Spring Boot 默认添加了一些拦截 /* 的过滤器,因为 /* 会拦截所有请求,按理说我们也要设置 asyncSupported=true 属性。因为这些过滤器都是 Spring Boot 初始化的,所以它提供了 @EnableAsync 注解来统一配置,该注解只针对 “非 @WebFilter 和 @WebServlet 注解的有效”,所以我们自己定义的 Filter 还是需要自己配置 asyncSupported=true 的。

3、AsyncContext 对象

获取一个异步请求的上下文对象。

4、asyncContext.setTimeout(20 * 1000L);

我们不能让异步请求无限的等待下去,通过 setTimeout 来设定最大超时时间。

下面通过两种方式来测试异步任务:

先在 SpringBootSampleApplication 上添加 @EnableAsync 注解。

再检查所有自定义的Filter,如存在如下两种情况需要配置 asyncSupported=true

1) 自定义Filter 拦截了 /*

2) 某Filter 拦截了 /shanhy/* ,我们需要执行的异步请求的 Servlet 为 /shanhy/testcomet

方法一:原生Servlet方式

package org.springboot.sample.servlet;

import java.io.IOException;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* HTTP长连接实现
*
* @author 单红宇(365384722)
* @create 2016年3月29日
*/
@WebServlet(urlPatterns = "/xs/cometservlet", asyncSupported = true)
//异步处理的servlet若存在过滤器,则过滤器的注解@WebFilter应设置asyncSupported=true,
//否则会报错A filter or servlet of the current chain does not support asynchronous operations.
public class CometServlet extends HttpServlet {

private static final long serialVersionUID = -8685285401859800066L;

private final Queue<AsyncContext> asyncContexts = new LinkedBlockingQueue<>();

private final Thread generator = new Thread("Async Event generator") {

@Override
public void run() {
while (!generator.isInterrupted()) {// 线程有效
try {
while (!asyncContexts.isEmpty()) {// 不为空
TimeUnit.SECONDS.sleep(10);// 秒,模拟耗时操作
AsyncContext asyncContext = asyncContexts.poll();
HttpServletResponse res = (HttpServletResponse) asyncContext.getResponse();
res.getWriter().write("{\"result\":\"OK - "+System.currentTimeMillis()+"\"}");
res.setStatus(HttpServletResponse.SC_OK);
res.setContentType("application/json");
asyncContext.complete();// 完成
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

};

@Override
public void init() throws ServletException {
super.init();
generator.start();
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(">>>>>>>>>>CometServlet Request<<<<<<<<<<<");
doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
AsyncContext asyncContext = req.startAsync();
asyncContext.setTimeout(20 * 1000L);
asyncContexts.offer(asyncContext);
}

@Override
public void destroy() {
super.destroy();
generator.interrupt();
}


}

方法二:Controller 方式

@Controller
public class PageController {

@RequestMapping("/async/test")
@ResponseBody
public Callable<String> callable() {
// 这么做的好处避免web server的连接池被长期占用而引起性能问题,
// 调用后生成一个非web的服务线程来处理,增加web服务器的吞吐量。
return new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(3 * 1000L);
return "小单 - " + System.currentTimeMillis();
}
};
}

}

最后写一个comet.jsp页面测试:

<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>长连接测试</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/webjarslocator/jquery/jquery.js"></script>
<script type="text/javascript">
$(function(){
function longPolling(){
$.getJSON('${pageContext.request.contextPath }/xs/cometservlet', function(data){
console.log(data.result);
$('#n1').html(data.result);
longPolling();
});
}
longPolling();

function longPolling2(){
$.get('${pageContext.request.contextPath }/async/test', function(data){
console.log(data);
$('#n2').html(data);
longPolling2();
});
}
longPolling2();
});
</script>
</head>

<body>
<h1>长连接测试</h1>
<h2 id="n1"></h2>
<h2 id="n2"></h2>
</body>
</html>



举报

相关推荐

0 条评论