user3798196
user3798196

Reputation: 41

Spring mvc 4.0.5 long polling example

I'm trying to implement long polling in spring with DefferedResult. I'm trying to follow this example, from spring, https://github.com/rstoyanchev/spring-mvc-chat

I'm following configuration to the letter (almost, see below). When I'm launching it I got:

HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalStateException: Async support must be enabled on a servlet and for all filters involved in async request processing. This is done in Java code using the Servlet API or by adding "true" to servlet and filter declarations in web.xml.

Which is coming from: StandardServletAsyncWebRequest.

My config classes:

    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);
    }

}

I'm using:

Java 7 Tomcat 7.0.54 Spring 4.0.5

Any help would be much appreciated. Really frustrating with lack of decent examples. p.s. if amount of places where I tried to enable async support seems overwhelming - don't worry I'm just trying to figure out how exactly it should be done.

Thanks, and I hope you can help me.

Upvotes: 2

Views: 4390

Answers (1)

user3798196
user3798196

Reputation: 41

After asking Rossen Stoyanchev for help (great guy, big thanks to him!) he pointed out that I have springSecurityFilterChain in my web.xml, so this one also needs to be configured for async support:

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

Hope it will help the rest of you who has the same problem.

Upvotes: 2

Related Questions