我使用spring mvc 3.2.4和jquery 1.9.0进行长轮询。我的应用程序部署在Tomcat 7.0.42上。我的弹簧配置文件如下:
Application Web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee"
version="3.0">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/app-servlet.xml
</param-value>
</context-param>
</web-app>
Spring Configration xml as: -
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:annotation-config/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.webchat"/>
<bean id="defferedResult" class="com.exp.DeferredResultContainer"></bean>
</beans>
发布数据的控制器显示为
@RequestMapping(value = "/postComment", method = RequestMethod.POST)
public @ResponseBody String postComment(HttpServletRequest request) {
deferredResultContainer.updateAllResults(request.getParameter("comment"));
return "success";
}
延迟结果容器类
public class DeferredResultContainer {
private final Set<DeferredResult<String>> deferredResults = Collections.synchronizedSet(new HashSet<DeferredResult<String>>() );
public void put(DeferredResult<String> deferredResult){
deferredResults.add(deferredResult);
}
public void updateAllResults(String value){
for (DeferredResult<String> deferredResult : deferredResults){
deferredResult.setResult(value);
}
}
public void remove(DeferredResult<String> deferredResult){
deferredResults.remove(deferredResult);
}
public int determineSize(){
return deferredResults.size();
}
}
延迟结果的控制器显示为
@RequestMapping(value = "/getComments", method = RequestMethod.GET)
@ResponseBody
public DeferredResult<String> getComments() throws Exception{
final DeferredResult<String> deferredResult= new DeferredResult<String>();
deferredResultContainer.put(deferredResult);
deferredResult.onTimeout(new Runnable() {
@Override public void run() {
deferredResultContainer.remove(deferredResult);
}
});
deferredResult.onCompletion(new Runnable() {
@Override public void run() {
deferredResultContainer.remove(deferredResult);
}
});
return deferredResult;
}
当我试图通过ajax进行长时间轮询时,我得到了以下回复: -
{"setOrExpired":false}
onCompletion方法也没有被执行。
简单到控制器下面的东西给出了完美的响应 { “1”: “2”}
@RequestMapping(value = "/test1", method = RequestMethod.GET)
@ResponseBody
public Map test1() throws Exception{
Map m1 = new HashMap<String, Object>();
m1.put("1", "2");
return m1;
}
一旦我将其更改为下面并添加延迟结果,我得到响应 { “setOrExpired”:真}
@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public DeferredResult<Map> test() throws Exception{
DeferredResult<Map> result = new DeferredResult<Map>();
Map m1 = new HashMap<String, Object>();
m1.put("1", "2");
result.setResult(m1);
return result;
}
轮询代码
$(document).ready(function() {
longPoll();
function longPoll(){
$.support.cors = true;
var path = "http://localhost:8080/WebChatExp/rest";
$.ajax({
url: path + "/getComments",
cache:false,
success: function(data){
//To Do
alert("Data" + JSON.stringify(data));
},
error: function(err, status, errorThrown ) {
},
type: "GET",
dataType: "json",
complete: longPoll,
timeout: 60000 // timeout every one minute
});
}
我搜索了各种示例,但无法确定延迟结果是否需要任何额外配置。请指教。
答案 0 :(得分:2)
您正在获得的回复正文
{"setOrExpired":true}
表示Spring将您的DeferredResult
(包含setOrExpired
的各种属性)序列化为JSON,而不是使用DeferredResultMethodReturnValueHandler
处理它。换句话说,它使用另一个HandlerMethodReturnValueHandler
,最有可能RequestResponseBodyMethodProcessor
(处理@ResponseBody
)来处理从@RequestMapping
带注释的处理程序方法返回的值。 (测试此方法的最简单方法是查看删除@ResponseBody
注释时会发生什么。)
查看注册默认HandlerMethodReturnValueHandler
个实例的3.2.x source code of RequestMappingHandlerAdapter
,DeferredResultMethodReturnValueHandler
在RequestResponseBodyMethodProcessor
之前注册,因此将首先处理DeferredResult
返回值
由于您看到不同的行为,我们必须假设您的配置不是您在此处显示的内容。 (请注意,<mvc:annotation-driven/>
注册RequestMappingHandlerAdapter
。)
另请注意,您目前正在/WEB-INF/app-servlet.xml
两次加载配置,一次加载ContextLoaderListener
,一次加载DispatcherServlet
。
完全摆脱您的ContextLoaderListener
和相应的context-param
。在你的例子中不需要它们。
答案 1 :(得分:0)
我知道在这种情况下(spring mvc 3.2.4)不可能是问题,只是为了将来参考: 我在Spring Boot 2.2.0.BUILD-SNAPSHOT(spring 5.1.5)中遇到了同样的问题。 当您尝试使用webflux而不是传统的spring mvc应用返回DeferredResult时,会发生此错误。请记住,在新的webflux api中,您应该使用mono / flux,而不是DeferredResult。