- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用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 执行轮询的时间,或者我可能错过了一些需要的东西。我的问题是:
最佳答案
您混合了两个概念:@Async
method calls和 asynchronous 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/
有人有 Comet 应用程序 .net 的任何样本吗? 我需要一个示例如何在服务器中保持客户端的连接? 最佳答案 这里也有一些不错的: http://www.frozenmountain.com/we
我想知道是否有 Yii2 专家可以帮助我了解如何最好地使用 ajax 表单与 Yii ajax 验证相结合。我想我可以在不带您阅读我所有代码的情况下解释这个问题。 我正在处理一个促销代码输入表单,用户
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
f:ajax 和 a4j:ajax 标记之间有什么显着差异吗? 我知道 Richfaces 4 中的 a4j:ajax 基于 native f:ajax JSF2 标记,添加了一些 f:ajax 中未
我已经尝试过这样但无法获取数组列表。它返回“null” var data=[]; data[0] = '1'; data[1] = '2'; $.ajax({
在教程中可以看到 jQuery.ajax 和 $.ajax 喜欢这里 http://www.thekludge.com/form-auto-save-with-jquery-serialize/ jQ
过度使用 AJAX 会影响性能吗?在大型 Web 应用程序的上下文中,您如何处理 AJAX 请求以控制异步请求? 最佳答案 过度使用任何东西都会降低性能;在必要时使用 AJAX 将提高性能,特别是如果
似乎我无法使用 Ext.Ajax.request 进行跨域 ajax 调用。看起来 ScriptTag: True 没有任何效果。 这是我的代码: {
我正在使用 Bottle 微框架(但我怀疑我的问题来自它) 首先,如果我定义了一个从/test_redirect 到/x 的简单重定向,它会起作用。所以 Bottle redirect() 在简单的情
任何人都可以指出各种 AJAX 库的统一比较吗?我已经阅读了大约十几种不同的书,我即将开始一个项目,但我对自己是否已经探索了可能性的空间没有信心。 请注意,我不是在要求“我认为 XXX 很棒”——我正
似乎使用 AJAX 的站点和应用程序正在迅速增长。使用 AJAX 的主要原因之一可能是增强用户体验。我担心的是,仅仅因为项目可以使用 AJAX,并不意味着它应该。 可能是为了 UX,AJAX 向站点/
假设我有一个可以通过 Javascript 自定义的“报告”页面。假设我有可以更改的 start_date、end_date 和类型(“简单”或“完整”)。现在 我希望地址栏始终包含当前(自定义) V
我一直在阅读 Ajax 并且希望从 stackoverflow 社区看到我是否正确理解所有内容。 因此,正常的客户端服务器交互是用户在 url 中拉出 Web 浏览器类型,并将 HTTP 请求发送到服
这可能有点牵强,但让我们假设我们需要它以这种方式工作: 我在服务器的 web 根目录中有一个 index.html 文件。该文件中的 javascript 需要向/secure/ajax.php 发出
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 去年关闭。 Improve this
我希望ajax post成功进入主页。由于某种原因,我一直做错事。知道我应该做什么来解决这个问题吗? window.APP_ROOT_URL = ""; Ajax $.ajax({ url: '#{a
我在 2 个不同的函数中有 2 个 ajax 调用。我想用.click来调用这2个函数。 func1 将数据插入数据库,然后 func2 检索数据,所以我的问题是如何等到 func1 完全完成然后只执
我试图在单击按钮后禁用该按钮。我尝试过: $("#ajaxStart").click(function() { $("#ajaxStart").attr("disabled", true);
我试图在每个 Ajax 请求上显示加载动画/微调器 我的 application.js $(document).on("turbolinks:load", function() { window.
我正在显示使用jQplot监视数据的图形。 为了刷新保存该图的div,我每5秒调用一次ajax调用(请参见下面的JavaScript摘录)。 在服务器上,PHP脚本从数据库中检索数据。 成功后,将在5
我是一名优秀的程序员,十分优秀!