異步請求中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的坑