异步请求中jetty处理ServletRequestListener的坑
标题起得比较诡异,其实并不是坑,而是jetty似乎压根就没做对异步request的ServletRequestListener的特殊处理,如果文中有错误欢迎提出,可能自己有所疏漏了。
之前遇到了一个bug,在Listener中重写requestDestroyed清理资源后,这些资源在异步任务中就不可用了。
这与预期不符,直觉上request应该在任务完成之后才触发requestDestroyed,而不应该是开始异步操作返回后就触发。
正确的触发时机应该是异步任务完成之后。
后来查阅了下,发现servlet 3的规范中只是增加了异步servlet和异步filter的支持,listener如何处理没有定义,也就是各个容器的实现可能会差异(我们自己使用的是jetty)。
但讲道理的话,合理的实现应该判断当前request是否处于异步处理中,如果是的话将销毁的触发的时机放置在AsyncContext完成之后。
自己试了下使用两个容器:Tomcat 8.5.15和Jetty 9.4.7.v20170914。
测试代码比较简单,为了去除其他框架的影响使用纯的servlet api。
servlet:
@WebServlet(asyncSupported = true, urlPatterns = { "/test" })
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 7395865716615939512L;
private ExecutorService pool = Executors.newCachedThreadPool();
@Override
public void destroy() {
pool.shutdown();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(LocalDateTime.now() + " get start " + request);
AsyncContext context = request.startAsync();
context.addListener(new AsyncListener() {
@Override
public void onTimeout(AsyncEvent event) throws IOException {
System.out.println(LocalDateTime.now() + " async onTimeout " + event.getSuppliedRequest());
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException {
System.out.println(LocalDateTime.now() + " async onStartAsync " + event.getSuppliedRequest());
}
@Override
public void onError(AsyncEvent event) throws IOException {
System.out.println(LocalDateTime.now() + " async onError " + event.getSuppliedRequest());
}
@Override
public void onComplete(AsyncEvent event) throws IOException {
System.out.println(LocalDateTime.now() + " async onComplete " + event.getSuppliedRequest());
}
}, request, response);
pool.execute(() -> {
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
System.out.println(LocalDateTime.now() + " job done");
context.complete();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println(LocalDateTime.now() + " get return");
}
}
listener:
@WebListener
public class TestListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent event) {
System.out.println(LocalDateTime.now() + " TestListener requestDestroyed " + event.getServletRequest());
}
@Override
public void requestInitialized(ServletRequestEvent event) {
System.out.println(LocalDateTime.now() + " TestListener requestInitialized " + event.getServletRequest());
}
}
使用tomcat返回的结果:
2017-11-19T16:42:04.256 TestListener requestInitialized org.apache.catalina.connector.RequestFacade@7c2df671
2017-11-19T16:42:04.257 get start org.apache.catalina.connector.RequestFacade@7c2df671
2017-11-19T16:42:04.261 get return
2017-11-19T16:42:09.261 job done
2017-11-19T16:42:09.261 async onComplete org.apache.catalina.connector.RequestFacade@7c2df671
2017-11-19T16:42:09.261 TestListener requestDestroyed org.apache.catalina.connector.RequestFacade@7c2df671
使用jetty返回的结果:
2017-11-19T16:46:35.231 TestListener requestInitialized (GET //localhost:8080/test)@1124787543 org.eclipse.jetty.server.Request@430ae557
2017-11-19T16:46:35.232 get start (GET //localhost:8080/test)@1124787543 org.eclipse.jetty.server.Request@430ae557
2017-11-19T16:46:35.234 get return
2017-11-19T16:46:35.235 TestListener requestDestroyed [GET //localhost:8080/test]@1124787543 org.eclipse.jetty.server.Request@430ae557
2017-11-19T16:46:40.235 job done
2017-11-19T16:46:58.019 async onComplete [GET //localhost:8080/test]@1124787543 org.eclipse.jetty.server.Request@430ae557
jetty果然没有讲道理... ...
先看下讲道理的tomcat,果不其然他做了特殊判断。
在StandardHostValve的invoke方法中:
if (!request.isAsync() && !asyncAtStart) {
context.fireRequestDestroyEvent(request.getRequest());
}
并且销毁的时机和预想的一样是在AsyncContext完成之后:

public void fireOnComplete() {
List<AsyncListenerWrapper> listenersCopy = new ArrayList<>();
listenersCopy.addAll(listeners);
ClassLoader oldCL = context.bind(Globals.IS_SECURITY_ENABLED, null);
try {
for (AsyncListenerWrapper listener : listenersCopy) {
try {
listener.fireOnComplete(event);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
log.warn("onComplete() failed for listener of type [" +
listener.getClass().getName() + "]", t);
}
}
} finally {
context.fireRequestDestroyEvent(request.getRequest());
clearServletRequestResponse();
context.unbind(Globals.IS_SECURITY_ENABLED, oldCL);
}
}
而jetty没有处理,在ContextHandler的doHandle中没有异步请求的判断:
finally
{
// Handle more REALLY SILLY request events!
if (new_context)
{
if (!_servletRequestListeners.isEmpty())
{
final ServletRequestEvent sre = new ServletRequestEvent(_scontext,request);
for (int i=_servletRequestListeners.size();i-->0;)
_servletRequestListeners.get(i).requestDestroyed(sre);
}
if (!_servletRequestAttributeListeners.isEmpty())
{
for (int i=_servletRequestAttributeListeners.size();i-->0;)
baseRequest.removeEventListener(_servletRequestAttributeListeners.get(i));
}
}
}
new_content用来在第一次调用返回true之后是false,和异步处理无关。
网上查了些也不清楚为什么jetty没有像tomcat那样实现这个,还是jetty希望用户通过AsyncListener来做处理呢。
异步请求中jetty处理ServletRequestListener的坑的更多相关文章
- Jquery中Ajax异步请求中的async参数的作用
之前不知道这个参数的作用,上网找了前辈的博客,在此收录到自己的博客,希望能帮到更多的朋友: test.html <a href="javascript:void(0)" on ...
- 在递归函数中使用JQuery.Deferred,异步请求中的同步执行...
标题不知道怎么起合适,其实需求很简单: 黑色背景的容器在页面打开时是隐藏的,点击提交后显示. 然后开始执行递归方法,每次ajax请求完成时,更新容器内容. 在全部执行完成后输出“执行完成”. subm ...
- UC浏览器中Ajax请求中传递数据的一个坑
今天突然收到一个bug,有用户在其浏览器环境中一直无法提交内容,使用的是UC浏览器.当换成Chrome时,内容能够正常提交.鉴于本地没有一直使用Firefox 以及Chrome,于是去下载了一个UC ...
- NSURLConnection同步与异步请求 问题
NSURLConnection目前有两个异步请求方法,异步请求中其中一个是代理.一个同步方法.有前辈已经详细介绍,见:http://blog.csdn.net/xyz_lmn/article/deta ...
- JSON(四)——异步请求中前后端使用Json格式的数据进行交互
json格式的数据广泛应用于异步请求中前后端的数据交互,本文主要介绍几种使用场景和使用方法. 一,json格式字符串 <input type="button" id=&quo ...
- ajax异步请求302分析
1.前言 遇到这样一种情况,打开网页两个窗口a,b(都是已经登录授权的),在a页面中退出登录,然后在b页面执行增删改查,这个时候因为授权原因,b页面后端的请求肯定出现异常(对这个异常的处理,进行内部跳 ...
- 异步请求获取JSON数据
json格式的数据广泛应用于异步请求中前后端的数据交互,本文主要介绍几种使用场景和使用方法. <script type="text/javascript"> funct ...
- ASIHTTP 框架,同步、 异步请求、 上传 、 下载
ASIHTTPRequest详解 ASIHTTPRequest 是一款极其强劲的 HTTP 访问开源项目.让简单的 API 完成复杂的功能,如:异步请求,队列请求,GZIP 压缩,缓存,断点续传,进度 ...
- Ajax异步请求返回文件流(eg:导出文件时,直接将导出数据用文件流的形式返回客户端供客户下载)
在异步请求中要返回文件流,不能使用JQuery,因为$.ajax,$.post 不支持返回二进制文件流的类型,可以看到下图,dataType只支持xml,json,script,html这几种格式,没 ...
随机推荐
- 1179: 零起点学算法86——小明A+B(未弄懂)
1179: 零起点学算法86——小明A+B Time Limit: 1 Sec Memory Limit: 32 MB 64bit IO Format: %lldSubmitted: 2540 ...
- spring+struts+hibernate整合
spring整合: 1:添加配置文件和相应的spring jar包(记得一定要加上commons-logging的jar包,有坑****) 2:创建date对象,如果成功则spring的环境ok
- Spring Boot jsp页面无法跳转问题
可能的情况如下: 1.未在pom.xml中添加依赖 <!-- jsp 视图支持--> <dependency> <groupId>org.apache.tom ...
- Java:ConcurrentHashMap的锁分段技术
术语定义 术语 英文 解释 哈希算法 hash algorithm 是一种将任意内容的输入转换成相同长度输出的加密方式,其输出被称为哈希值. 哈希表 hash table 根据设定的哈希函数H(ke ...
- arm 执行 交叉编译完成的可执行文件时 出现premission denied 问题
我用的tftp传过去的文件 然后传完 执行的时候 出现了premission denied (权限不够)得问题 解决方法 就是添加权限 chmod 777 filename
- Ubuntu16.04安装Ambari 2.7.3
概念了解 Ambair介绍 Apache Ambari是一个用于支持大数据软件供应 管理与监控软件.它也是一个分布式软件,分为Ambair-Server与Ambari-Client两个部分.在生产环境 ...
- git使用之后悔药
1.工作区的代码想撤销 背景:有时候编写了一大段代码之后,想要撤销更改(执行add操作之前), 命令:git checkout -- <file路径> 使用git checkout -- ...
- Blueking bk 蓝鲸开发环境搭建
首先根据文档安装各种东西 http://docs.bk.tencent.com/develop_center/ops/unified.html#installation 完后在在vagrantfile ...
- Notes : <Hands-on ML with Sklearn & TF> Chapter 3
Chapter 3-Classification .caret, .dropup > .btn > .caret { border-top-color: #000 !important; ...
- js every some 遍历函数理解
1.every let arr = [0, 1, 2, 3, 4, 5]; let result = arr.every((item, index) => { return item >= ...