gpt4 book ai didi

javascript - 使用 JQuery ajax 和 Spring DeferredResult 进行长轮询返回空白结果

转载 作者:行者123 更新时间:2023-11-30 08:02:00 24 4
gpt4 key购买 nike

我正在尝试使用Spring的DeferredResult来执行长轮询。 Controller 看起来像这样:

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() Controller 函数:

<!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() 调用会立即返回,并且结果为空。 Controller 函数确实继续设置并返回结果,但该结果似乎不会“传送”到客户端的任何地方。

我使用注释配置而不是 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 执行轮询的时间,或者我可能错过了一些需要的东西。我的问题是:

  • 为什么异步 Controller 函数没有向客户端返回正确的结果?
  • 我需要进行哪些更改才能使长轮询正常工作?

最佳答案

您混合了两个概念:@Async method callsasynchronous request processing在 Spring MVC 堆栈中。尽管两者都涉及术语“异步”,但它们具有不同的目标(或者更确切地说是不同功能集的一部分)。

@Async 允许在单独的线程上调用方法的代码。 Spring 通过代理您的对象并将实际调用委托(delegate)给另一个线程上的真实对象来实现这一点。鉴于

@Component
class Real {
@Async
public void method() {}
}

Real proxy = ... // get Real from ApplicationContext
proxy.method(); // returns immediately
// continue in this thread

代理看起来像

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 方法,我们需要一个约定。正如上面链接的文档所述

Even methods that return a value can be invoked asynchronously. However, such methods are required to have a Future typed return value.

您的方法的返回类型为 DeferredResult,它不是 Future。在这种情况下,Spring 不会提示,只是在将调用分派(dispatch)到另一个线程后返回 null

这就是 Spring MVC 发挥作用的地方。 Spring 使用 HandlerMethodReturnValueHandler 对象列表来处理来自 @RequestMapping 带注释的处理程序方法的返回值。对于DeferredResult,即 DeferredResultMethodReturnValueHandler 。如果它收到 null 作为返回值,则假定请求已被处理并且不需要执行任何进一步操作(这是 HandlerMethodReturnValueHandler 之间的常见约定)。

DeferredResult 类似于 Java 8 的 CompletableFuture 类型。这是对结果的 promise 。通过返回 DeferredResult 类型的值,您向 Spring MVC promise 您将在某个时刻(希望在超时之前)获得一个值。与此同时,您已将实际处理分派(dispatch)给另一个线程(它将设置值),并且 MVC 堆栈可以回收请求处理线程并让它服务其他请求。

总而言之,不要同时使用这两种技术。摆脱 @Async 并取消注释您拥有的新 Thread 代码(尽管您可以使用专用的线程池)。 Spring MVC 将收到此 DeferredResult,它会假设您会兑现 promise ,并将其存储起来供以后使用。当您最终setResult时,这将通知Spring取消存储并完成请求处理。

<小时/>

查看您的 github 项目,您需要启用异步支持

springDispatcher.setAsyncSupported(true);

您也不需要加载ContextLoaderListener。摆脱它。

关于javascript - 使用 JQuery ajax 和 Spring DeferredResult 进行长轮询返回空白结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31763512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com