我正在尝试使用Spring的DeferredResult
来执行长轮询。控制器如下所示:
package com.example.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;
import com.example.controller.interfaces.ExampleControllerInterface;
@Component
public class ExampleController implements ExampleControllerInterface{
@Override
public String viewHomePage(HttpServletRequest request, ModelMap model, HttpSession session){
return "index";
}
@ResponseBody
@RequestMapping(value = "/pollContent", method = RequestMethod.GET)
public DeferredResult<String> pollContent(HttpServletRequest request, ModelMap model, HttpSession session){
System.out.println("Polling content.");
final DeferredResult<String> result = new DeferredResult<>();
new Thread(){
public void run(){
try {
System.out.println("Sleeping for 10 seconds.");
Thread.sleep(10000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Setting result.");
result.setResult("testing");
}
}.start();
System.out.println("Returning result.");
return result;
}
}
我正在使用JQuery的ajax()
函数来访问异步pollContent()
控制器函数:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Spring Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
function pollContent() {
$.ajax({url: "pollContent", success: function(result){
console.log("Polled result: " + result);
$("#polledContent").html(result);
}});
}
$(pollContent);
</script>
</head>
<body>
<p>This example uses JQuery's ajax() function to access a Spring controller's async function. After the page loads, it should return in 10 seconds and change the below text:</p>
<p id="polledContent">Loading...</p>
<p>Instead, the call returns immediately and the result is blank.</p>
</body>
</html>
我希望ajax()
调用需要10秒才能返回其成功处理程序,然后使用结果填充polledContent
p
标记。
相反,ajax()
调用立即返回,结果为空。控制器功能会继续设置并返回结果,但该结果似乎不会在客户端的任何地方“转到”。
我正在使用注释配置而不是web.xml,我在配置类中启用了异步:
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@EnableAsync
@ComponentScan({"com.example.controller", "com.example.config"})
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
super.configureDefaultServletHandling(configurer);
}
}
编辑:根据要求,这是记录器输出:
TRACE [http-nio-8080-exec-4] (FrameworkServlet.java:1043) - Bound request context to thread: org.apache.catalina.connector.RequestFacade@49da6e5
DEBUG [http-nio-8080-exec-4] (DispatcherServlet.java:838) - DispatcherServlet with name 'dispatcher' processing GET request for [/SpringExample/]
TRACE [http-nio-8080-exec-4] (DispatcherServlet.java:1095) - Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@3da4a56] in DispatcherServlet with name 'dispatcher'
DEBUG [http-nio-8080-exec-4] (AbstractHandlerMethodMapping.java:246) - Looking up handler method for path /
TRACE [http-nio-8080-exec-4] (AbstractHandlerMethodMapping.java:284) - Found 1 matching mapping(s) for [/] : [{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}]
DEBUG [http-nio-8080-exec-4] (AbstractHandlerMethodMapping.java:251) - Returning handler method [public abstract java.lang.String com.example.controller.interfaces.ExampleControllerInterface.viewHomePage(javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap,javax.servlet.http.HttpSession)]
DEBUG [http-nio-8080-exec-4] (AbstractBeanFactory.java:249) - Returning cached instance of singleton bean 'exampleController'
TRACE [http-nio-8080-exec-4] (DispatcherServlet.java:1135) - Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@5fe4ef74]
TRACE [http-nio-8080-exec-4] (DispatcherServlet.java:1135) - Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@c041485]
TRACE [http-nio-8080-exec-4] (DispatcherServlet.java:1135) - Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@24adb017]
DEBUG [http-nio-8080-exec-4] (DispatcherServlet.java:925) - Last-Modified value for [/SpringExample/] is: -1
TRACE [http-nio-8080-exec-4] (InvocableHandlerMethod.java:130) - Invoking [$Proxy20.viewHomePage] method with arguments [org.apache.catalina.connector.RequestFacade@49da6e5, {}, org.apache.catalina.session.StandardSessionFacade@2f88f324]
TRACE [http-nio-8080-exec-4] (InvocableHandlerMethod.java:134) - Method [viewHomePage] returned [index]
TRACE [http-nio-8080-exec-4] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.ModelAndViewMethodReturnValueHandler@5c39d55e] supports [class java.lang.String]
TRACE [http-nio-8080-exec-4] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.method.annotation.ModelMethodProcessor@20c740be] supports [class java.lang.String]
TRACE [http-nio-8080-exec-4] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.ViewMethodReturnValueHandler@7cd737e7] supports [class java.lang.String]
TRACE [http-nio-8080-exec-4] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor@77170f30] supports [class java.lang.String]
TRACE [http-nio-8080-exec-4] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.HttpHeadersReturnValueHandler@6516a7a9] supports [class java.lang.String]
TRACE [http-nio-8080-exec-4] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.CallableMethodReturnValueHandler@25b98d19] supports [class java.lang.String]
TRACE [http-nio-8080-exec-4] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.DeferredResultMethodReturnValueHandler@2cb6fe5a] supports [class java.lang.String]
TRACE [http-nio-8080-exec-4] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.AsyncTaskMethodReturnValueHandler@b7a0149] supports [class java.lang.String]
TRACE [http-nio-8080-exec-4] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.method.annotation.ModelAttributeMethodProcessor@45accbf1] supports [class java.lang.String]
TRACE [http-nio-8080-exec-4] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor@68f7a9ab] supports [class java.lang.String]
TRACE [http-nio-8080-exec-4] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.ViewNameMethodReturnValueHandler@495843fd] supports [class java.lang.String]
DEBUG [http-nio-8080-exec-4] (DispatcherServlet.java:1218) - Rendering view [org.springframework.web.servlet.view.JstlView: name 'index'; URL [/WEB-INF/jsp/index.jsp]] in DispatcherServlet with name 'dispatcher'
TRACE [http-nio-8080-exec-4] (AbstractView.java:261) - Rendering view with name 'index' with model {} and static attributes {}
DEBUG [http-nio-8080-exec-4] (InternalResourceView.java:207) - Forwarding to resource [/WEB-INF/jsp/index.jsp] in InternalResourceView 'index'
TRACE [http-nio-8080-exec-4] (FrameworkServlet.java:1053) - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@49da6e5
DEBUG [http-nio-8080-exec-4] (FrameworkServlet.java:991) - Successfully completed request
TRACE [http-nio-8080-exec-4] (AbstractApplicationContext.java:331) - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/SpringExample/]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[DB17A129B0B4A37F85D65A1B973EBF3D]; user=[null]; time=[8ms]; status=[OK]
TRACE [http-nio-8080-exec-5] (FrameworkServlet.java:1043) - Bound request context to thread: org.apache.catalina.connector.RequestFacade@49da6e5
DEBUG [http-nio-8080-exec-5] (DispatcherServlet.java:838) - DispatcherServlet with name 'dispatcher' processing GET request for [/SpringExample/pollContent]
TRACE [http-nio-8080-exec-5] (DispatcherServlet.java:1095) - Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@3da4a56] in DispatcherServlet with name 'dispatcher'
DEBUG [http-nio-8080-exec-5] (AbstractHandlerMethodMapping.java:246) - Looking up handler method for path /pollContent
TRACE [http-nio-8080-exec-5] (AbstractHandlerMethodMapping.java:284) - Found 1 matching mapping(s) for [/pollContent] : [{[//pollContent],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}]
DEBUG [http-nio-8080-exec-5] (AbstractHandlerMethodMapping.java:251) - Returning handler method [public abstract org.springframework.web.context.request.async.DeferredResult<java.lang.String> com.example.controller.interfaces.ExampleControllerInterface.pollContent(javax.servlet.http.HttpServletRequest,org.springframework.ui.ModelMap,javax.servlet.http.HttpSession)]
DEBUG [http-nio-8080-exec-5] (AbstractBeanFactory.java:249) - Returning cached instance of singleton bean 'exampleController'
TRACE [http-nio-8080-exec-5] (DispatcherServlet.java:1135) - Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@5fe4ef74]
TRACE [http-nio-8080-exec-5] (DispatcherServlet.java:1135) - Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@c041485]
TRACE [http-nio-8080-exec-5] (DispatcherServlet.java:1135) - Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@24adb017]
DEBUG [http-nio-8080-exec-5] (DispatcherServlet.java:925) - Last-Modified value for [/SpringExample/pollContent] is: -1
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.RequestParamMethodArgumentResolver@757b2cca] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.RequestParamMapMethodArgumentResolver@2c353cf8] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.PathVariableMethodArgumentResolver@208b37e2] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.PathVariableMapMethodArgumentResolver@40d0b02b] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.MatrixVariableMethodArgumentResolver@47d7bd17] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.MatrixVariableMapMethodArgumentResolver@4421f1b1] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor@17a83b8d] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor@6ff67550] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver@36ac04ff] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.RequestHeaderMethodArgumentResolver@b23433c] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.RequestHeaderMapMethodArgumentResolver@b325664] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.ServletCookieValueMethodArgumentResolver@72dcde94] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.ExpressionValueMethodArgumentResolver@426c7ec5] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver@5c68f351] supports [interface javax.servlet.http.HttpServletRequest]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.RequestParamMethodArgumentResolver@757b2cca] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.RequestParamMapMethodArgumentResolver@2c353cf8] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.PathVariableMethodArgumentResolver@208b37e2] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.PathVariableMapMethodArgumentResolver@40d0b02b] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.MatrixVariableMethodArgumentResolver@47d7bd17] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.MatrixVariableMapMethodArgumentResolver@4421f1b1] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor@17a83b8d] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor@6ff67550] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver@36ac04ff] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.RequestHeaderMethodArgumentResolver@b23433c] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.RequestHeaderMapMethodArgumentResolver@b325664] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.ServletCookieValueMethodArgumentResolver@72dcde94] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.ExpressionValueMethodArgumentResolver@426c7ec5] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver@5c68f351] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.ServletResponseMethodArgumentResolver@69f4b041] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor@5d9983f8] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.RedirectAttributesMethodArgumentResolver@423d0dc5] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.ModelMethodProcessor@e87db7b] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.MapMethodProcessor@1f101f10] supports [class org.springframework.ui.ModelMap]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.RequestParamMethodArgumentResolver@757b2cca] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.RequestParamMapMethodArgumentResolver@2c353cf8] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.PathVariableMethodArgumentResolver@208b37e2] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.PathVariableMapMethodArgumentResolver@40d0b02b] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.MatrixVariableMethodArgumentResolver@47d7bd17] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.MatrixVariableMapMethodArgumentResolver@4421f1b1] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor@17a83b8d] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor@6ff67550] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver@36ac04ff] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.RequestHeaderMethodArgumentResolver@b23433c] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.RequestHeaderMapMethodArgumentResolver@b325664] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.ServletCookieValueMethodArgumentResolver@72dcde94] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.method.annotation.ExpressionValueMethodArgumentResolver@426c7ec5] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (HandlerMethodArgumentResolverComposite.java:90) - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver@5c68f351] supports [interface javax.servlet.http.HttpSession]
TRACE [http-nio-8080-exec-5] (InvocableHandlerMethod.java:130) - Invoking [$Proxy20.pollContent] method with arguments [org.apache.catalina.connector.RequestFacade@49da6e5, {}, org.apache.catalina.session.StandardSessionFacade@2f88f324]
TRACE [http-nio-8080-exec-5] (InvocableHandlerMethod.java:134) - Method [pollContent] returned [null]
TRACE [http-nio-8080-exec-5] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.ModelAndViewMethodReturnValueHandler@5c39d55e] supports [org.springframework.web.context.request.async.DeferredResult<java.lang.String>]
TRACE [http-nio-8080-exec-5] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.method.annotation.ModelMethodProcessor@20c740be] supports [org.springframework.web.context.request.async.DeferredResult<java.lang.String>]
TRACE [http-nio-8080-exec-5] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.ViewMethodReturnValueHandler@7cd737e7] supports [org.springframework.web.context.request.async.DeferredResult<java.lang.String>]
TRACE [http-nio-8080-exec-5] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor@77170f30] supports [org.springframework.web.context.request.async.DeferredResult<java.lang.String>]
TRACE [http-nio-8080-exec-5] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.HttpHeadersReturnValueHandler@6516a7a9] supports [org.springframework.web.context.request.async.DeferredResult<java.lang.String>]
TRACE [http-nio-8080-exec-5] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.CallableMethodReturnValueHandler@25b98d19] supports [org.springframework.web.context.request.async.DeferredResult<java.lang.String>]
TRACE [http-nio-8080-exec-5] (HandlerMethodReturnValueHandlerComposite.java:80) - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.DeferredResultMethodReturnValueHandler@2cb6fe5a] supports [org.springframework.web.context.request.async.DeferredResult<java.lang.String>]
DEBUG [http-nio-8080-exec-5] (DispatcherServlet.java:1012) - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
TRACE [http-nio-8080-exec-5] (FrameworkServlet.java:1053) - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@49da6e5
DEBUG [http-nio-8080-exec-5] (FrameworkServlet.java:991) - Successfully completed request
TRACE [http-nio-8080-exec-5] (AbstractApplicationContext.java:331) - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/SpringExample/pollContent]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[DB17A129B0B4A37F85D65A1B973EBF3D]; user=[null]; time=[19ms]; status=[OK]
Polling content.
Returning result.
Sleeping for 10 seconds.
Setting result.
我已经在GitHub上创建了一个maven项目,如果你想自己运行它,可以在这里找到:https://github.com/KevinWorkman/SpringExample
我可能只是误解了使用Spring和ajax执行了多长时间的轮询,或者我可能遗漏了所需的内容。我的问题是:
答案 0 :(得分:1)
您在Spring MVC的堆栈中混合了两个概念:@Async
method calls和asynchronous request processing。虽然两者都涉及术语异步,但它们具有不同的目标(或者更确切地说是不同功能集的一部分)。
@Async
允许在单独的线程上调用方法的代码。 Spring通过代理您的对象并将实际调用委托给另一个线程上的真实对象来实现这一点。给定
@Component
class Real {
@Async
public void method() {}
}
和
Real proxy = ... // get Real from ApplicationContext
proxy.method(); // returns immediately
// continue in this thread
proxy
看起来像
class RealProxy extends Real {
private ExecutorService executorService; // potentiall configured in Async configuration
private Real target; // the actual object
@Override
public void method() {
executorService.submit(() -> target.method());
}
}
使用void
方法很容易理解。但是,使用非void方法,我们需要一个约定。正如上面链接的文档所述
即使是返回值的方法也可以异步调用。 但是,此类方法需要具有
Future
类型的返回 值。
您的方法的返回类型为DeferredResult
,而不是Future
。在这种情况下,Spring并没有抱怨,只是在将调用分派给另一个线程后返回null
。
这是Spring MVC的用武之地。 Spring使用HandlerMethodReturnValueHandler
对象列表来处理来自@RequestMapping
带注释的处理程序方法的返回值。对于DeferredResult
,即DeferredResultMethodReturnValueHandler
。如果它接收null
作为返回值,则它假定请求已被处理,并且不需要进一步完成(这是HandlerMethodReturnValueHandler
s中的常见约定。)
DeferredResult
类似于Java 8的CompletableFuture
类型。这是对结果的承诺。通过返回类型DeferredResult
的值,您承诺Spring MVC会在某个时刻(有希望在它超时之前)获得一个值。与此同时,您已将实际处理调度到另一个线程(将设置该值),MVC堆栈可以回收请求处理线程并让它服务于其他请求。
总而言之,不要使用这两种技术。摆脱@Async
并取消注释您拥有的新Thread
代码(尽管您可以使用专用线程池)。 Spring MVC会收到这个DeferredResult
,它会假设你会兑现你的承诺,将它存放在以后。当你最终setResult
时,这将通知Spring解除捆绑并完成请求处理。
查看您的github项目,您需要启用异步支持
springDispatcher.setAsyncSupported(true);
您也不需要加载ContextLoaderListener
。摆脱它。