Spring 4 websocket + Tomcat 7.54 async-supported not working

北城以北 提交于 2019-11-29 11:26:38

I guess you don't show entire web.xml.

<async-supported>true</async-supported> should be configured for <filter>, too.

UPDATE

Well, your issue is very simple:

<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>

You really should map for all requests, not only the root.

magiccrafter

The <async-supported>true</async-supported> should be included in both the servlet and filter tags. Use the following snippet as reference:

<web-app ...>
    ...
    <servlet>
        <servlet-name>instantaction</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:META-INF/spring/web/my-servlet-config.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        ...
    </servlet-mapping>

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

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

Add the below in nginx.conf file if you are using the reverse proxy.

  # For WebSocket upgrade header
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

Try upgrade you jdk and tomcat version. I encountered this problem also, I upgrade jdk from 1.7 to 1.8, upgrade tomcat form 7.0.54 to 7.0.75, and resolved this problem.

Make sure no other injected component disables async support.


DETAILS

I learned that Spring comes with asynchronous support by default.

And (remotely related) sync logging configuration may disable async handling for entire service.

Specifically, my logback integration was missing IMPORTANT line:

public EmbeddedServletContainerCustomizer containerCustomizer(
    final String logbackAccessClasspathConfig
) {
    return container -> {
        if (container instanceof TomcatEmbeddedServletContainerFactory) {
            ((TomcatEmbeddedServletContainerFactory) container)
                .addContextCustomizers(context -> {
                    LogbackValve logbackValve = new LogbackValve();
                    logbackValve.setFilename(logbackAccessClasspathConfig);

                    // IMPORTANT:
                    logbackValve.setAsyncSupported(true);

                    context.getPipeline().addValve(logbackValve);
                }
            );
        }
    };
}

Thanks to other answer:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!