Spring mvc 4.0.5长轮询示例

时间:2014-07-02 14:55:19

标签: spring spring-mvc asynchronous config long-polling

我正在尝试使用DefferedResult在Spring中实现长轮询。我试图从春天https://github.com/rstoyanchev/spring-mvc-chat

开始遵循这个例子

我正在跟随信件的配置(差不多,见下文)。当我启动它时,我得到了:

HTTP状态500 - 请求处理失败;嵌套异常是java.lang.IllegalStateException:必须在servlet和异步请求处理中涉及的所有过滤器上启用异步支持。这是使用Servlet API在Java代码中完成的,或者在web.xml中向servlet和过滤器声明添加“true”。

哪个来自:StandardServletAsyncWebRequest。

我的配置类:

    public class MVCInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses () {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebMvcConfig.class, PresentationConfig.class, SecurityConfig.class, EmailConfig.class };
    }

    @Override
    protected String[] getServletMappings () {
        return new String[] { "/" };
    }

    @Override
    protected void customizeRegistration(Dynamic registration) {
        registration.setAsyncSupported(true);
    }

    @Override
    protected boolean isAsyncSupported () {
        return true;
    }

}


@Configuration
@EnableWebMvc
@EnableAsync
@ComponentScan (basePackages = { "com.xxx.presentation" })
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    private static final Log log = LogFactory.getLog(WebMvcConfig.class);

    @Override
    public void configureAsyncSupport (AsyncSupportConfigurer configurer) {

        configurer.setDefaultTimeout(30 * 1000L);
    }

    public void addViewControllers (ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("teledetailer");
    }

    @Override
    public void addResourceHandlers (ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/images/**").addResourceLocations("/images/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
    }

    @Bean
    public ViewResolver viewResolver () {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

    @Override
    public void addReturnValueHandlers (List<HandlerMethodReturnValueHandler> returnValueHandlers) {
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    messageConverters.add(new StringHttpMessageConverter());
    messageConverters.add(new MappingJackson2HttpMessageConverter());
    returnValueHandlers.add(new RequestResponseBodyMethodProcessor(messageConverters));
        super.addReturnValueHandlers(returnValueHandlers);
    }

}

我正在使用:

Java 7 Tomcat 7.0.54 Spring 4.0.5

非常感谢任何帮助。真的令人沮丧的是缺乏体面的例子。 附:如果我尝试启用异步支持的地方数量似乎压倒性的 - 不要担心我只是想弄清楚应该如何完成它。

谢谢,我希望你能帮助我。

1 个答案:

答案 0 :(得分:2)

在向Rossen Stoyanchev寻求帮助之后(好人,非常感谢他!)他指出我的web.xml中有springSecurityFilterChain,所以这个也需要配置为异步支持:

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <async-supported>true</async-supported>
</filter>

希望它会帮助其他遇到同样问题的人。