在spring 3.2 及以后版本中增加了对请求的异步处理,这篇文章主要介绍了spring boot实现异步请求(servlet 3.0),感兴趣的小伙伴们可以参考一下。
在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)
* @myblog http://blog.csdn.net/catoop/
* @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>
【相关推荐】
1. java免费视频教程
2. java实现图片等比例缩略图视频教程
3. fastjson教程手册
以上就是spring boot两个异步请求的方法详解的详细内容。