Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从HttpServletRequest获取AsyncContext_Java_Spring Mvc_Servlet Filters_Servlet 3.0 - Fatal编程技术网

Java 从HttpServletRequest获取AsyncContext

Java 从HttpServletRequest获取AsyncContext,java,spring-mvc,servlet-filters,servlet-3.0,Java,Spring Mvc,Servlet Filters,Servlet 3.0,我使用Spring的OncePerRequestFilter重写shouldNotFilterAsyncDispatch方法返回false。这样它就可以处理异步请求。在过滤器中,我尝试执行以下操作: if (isAsyncDispatch(request)) { request.getAsyncContext().addListener(new AsyncListener() { @Override public void onComplete(Async

我使用Spring的
OncePerRequestFilter
重写
shouldNotFilterAsyncDispatch
方法返回false。这样它就可以处理异步请求。在过滤器中,我尝试执行以下操作:

if (isAsyncDispatch(request)) {
    request.getAsyncContext().addListener(new AsyncListener() {
        @Override
        public void onComplete(AsyncEvent event) throws IOException {
            System.out.println("onComplete");
        }

        @Override
        public void onTimeout(AsyncEvent event) throws IOException {

        }

        @Override
        public void onError(AsyncEvent event) throws IOException {
            System.out.println("onError");
        }

        @Override
        public void onStartAsync(AsyncEvent event) throws IOException {

        }
    });
}
因此,
isAsyncDispatch
按预期返回true。但是当我尝试
getAsyncContext
时,它失败了,出现以下异常:

IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
事实上,
request.isAsyncStarted()
返回false,但
request.isAsyncSupported()
为true,而
request.getDispatcherType()
ASYNC

我不明白:它是异步的还是非异步的?也许我用错了API?如何添加
AsyncListener
?也许是因为我在用Tomcat


提前谢谢你

我过去做过这件事,我们做过:

if (request.isAsyncSupported()) {
      AsyncContext asyncContext = request.startAsync();
      // Do whatever with context
}
getAsyncContext()
的javadoc没有声明:(in)

IllegalStateException-如果此请求未被置于异步模式,即如果既没有调用startAsync()也没有调用startAsync(ServletRequest,ServletResponse)


谢谢,老兄,现在我可以做广告了。但它会启动异步操作,不是吗?由于Spring controller中的操作,我有
未来
。所以我假设它已经是异步的。此外,它还为其他请求启动异步处理,但我只需要为异步控制器方法使用它,因为请求是为DispatcherServlet中的某个异步启动的。或者更准确地说,在处理返回一种异步结果的实际请求映射方法时。因此,如果此代码位于预请求端的筛选器中,则尚未调用
startAsync()
方法。