问题
I would like to add a delay to some requests without holding onto a thread under jetty. I think this can be done using the asynchronous support added to servlet 3.
I have inside of my filter, which is the first filter to run, something that adds the delay:
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(1000L);
that appears to delay the request but I am not sure how to to resume the filter chain. I would like to resume the filter chain when the timeout times out.
I tried to add a listener:
asyncContext.addListener(new AsyncListener() {
@Override
public void onTimeout(AsyncEvent event) throws IOException {
try {
chain.doFilter(event.getAsyncContext().getRequest(), event.getAsyncContext().getResponse());
} catch (ServletException e) {
throw new RuntimeException(e);
}
}
It looks like the next Filter in chain was called but I don't think spring was happy with that. For some request I got no errors but for the login API I always got a 401 yet my code which checks authentication was not called. In other cases I saw a null pointer exceptions: 2019-06-28 13:27:08,772 [admin-78] [::] WARN advice.CatchAllException - Catching
java.lang.NullPointerException
at org.springframework.util.StringUtils.uriDecode(StringUtils.java:724)
at org.springframework.web.util.UriUtils.decode(UriUtils.java:342)
at org.springframework.web.util.UrlPathHelper.decodeInternal(UrlPathHelper.java:464)
at org.springframework.web.util.UrlPathHelper.decodeRequestString(UrlPathHelper.java:455)
at org.springframework.web.util.UrlPathHelper.getContextPath(UrlPathHelper.java:344)
at org.springframework.web.util.UrlPathHelper.getPathWithinApplication(UrlPathHelper.java:239)
at org.springframework.web.util.UrlPathHelper.getPathWithinServletMapping(UrlPathHelper.java:192)
at org.springframework.web.util.UrlPathHelper.getLookupPathForRequest(UrlPathHelper.java:169)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:365)
How can I continue on with the filter chain?
来源:https://stackoverflow.com/questions/56800640/resuming-a-javax-servlet-filter-filter-chain-after-an-async-timeout-when-using-s