使用Spring的WebAsyncTask异步请求时出错Can't set AsyncWebRequest with concurrent handling in progress
2017-04-30 23:31
1636 查看
使用Spring的WebAsyncTask异步请求时出现如下错误:
表面上看错误描述并不清楚。这里我仅仅记录我的解决方法,我这里是因为时间超时引起的。也许你也是这样的情况:
原代码结结构如下:
这里直接返回的new WebAsyncTask(callable)报错。我是通过设置超时时间解决的:返回new WebAsyncTask(60000, callable);
注意:即使设置里超时时间而没做超时处理,这样是不严谨的。因为当60000毫秒之后call()没执行完依然会报异常。Spring对超时处理提供有WebAsyncTask.onTimeout()方法。
所有我最终的代码结构如下:
java.lang.IllegalStateException: Cannot forward after response has been committed java.lang.IllegalStateException: Can't set AsyncWebRequest with concurrent handling in progress org.springframework.web.util.NestedServletException: Request processing failed; Caused by: java.lang.IllegalStateException: Can't set AsyncWebRequest with concurrent handling in progress at org.springframework.util.Assert.state(Assert.java:392) at org.springframework.web.context.request.async.WebAsyncManager.setAsyncWebRequest(WebAsyncManager.java:108)
表面上看错误描述并不清楚。这里我仅仅记录我的解决方法,我这里是因为时间超时引起的。也许你也是这样的情况:
原代码结结构如下:
@RequestMapping(value = { "/webAsyncTask/test" }) public WebAsyncTask test(HttpServletRequest request) { Callable<String> callable = new Callable<String>() { public String call() throws InterruptedException { String string = new String(); Thread.sleep(15000); return string; } }; return new WebAsyncTask(callable); }
这里直接返回的new WebAsyncTask(callable)报错。我是通过设置超时时间解决的:返回new WebAsyncTask(60000, callable);
注意:即使设置里超时时间而没做超时处理,这样是不严谨的。因为当60000毫秒之后call()没执行完依然会报异常。Spring对超时处理提供有WebAsyncTask.onTimeout()方法。
所有我最终的代码结构如下:
@RequestMapping(value = { "/webAsyncTask/test" }) public WebAsyncTask test(HttpServletRequest request) { Callable<String> callable = new Callable<String>() { public String call() throws InterruptedException { String string = new String(); Thread.sleep(15000); return string; } }; WebAsyncTask asyncTask = new WebAsyncTask(60000, callable); asyncTask.onTimeout(new Callable<String>() { public String call() throws Exception { String string = "请求超时!"; return string; } }); return asyncTask; }
相关文章推荐
- No mapping found for HTTP request with URI [/spring/WEB-INF/page/index.jsp] in DispatcherServlet with name &#39;spring&#39;
- 利用AFN请求网路数据时出错。(Assertion failure in -[AFHTTPRequestSerializer requestWithMethod:URLString:paramete)
- 掌握 Ajax,第 2 部分: 使用 JavaScript 和 Ajax 发出异步请求 在 Web 请求中使用 XMLHttpRequest
- 使用JavaScript和Ajax发出异步请求 XMLHttpRequest
- 使用HttpWebRequest发送自定义POST请求
- 转:Working with HttpWebRequest and HttpWebResponse in ASP.NET
- 使用HttpWebRequest和HttpWebResponse在后台进行文件的请求和响应
- 使用HttpWebRequest请求远端服务器时如何加载SSL证书
- 使用 WebRequest 类请求数据全过程(整理自msdn)
- 使用HttpWebRequest发送自定义POST请求
- Spring Error creating bean with name 'sessionFactory ' defined in ServletContext resource [/WEB-INF/
- 下面的代码示例使用 BeginGetRequestStream 方法对流实例发出异步请求。
- How can a singleton bean collaborate with a non-singleton one in Spring
- c#中HttpWebRequest使用Proxy实现指定IP的域名请求
- [zt]使用 AJAX 的 Sys.Net.WebRequest 从客户端请求网页
- [dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- [dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- 使用HttpWebRequest发送自定义POST请求
- 使用HttpWebRequest发送自定义POST请求
- [C#]使用HttpWebRequest请求远端服务器时如何加载SSL证书