I spent a lot of time trying to figure out how to setup Thorntail/Wildfly JAX-RS apps to allow async operations. Most configuration
examples created one problem or another. I finally came across the idea that you could simply activate it by registering a
ServletContextListener
and changing the setting on the fly during startup.
package com.platform28.service.asyncmessaging.restapi;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.WebListener;
@WebListener
public class AsyncInitializer implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
servletContext.getFilterRegistrations().forEach(
(filterName, fr) -> ((FilterRegistration.Dynamic) fr).setAsyncSupported(true));
servletContext.getServletRegistrations().forEach(
(servletName, sr) -> ((ServletRegistration.Dynamic) sr).setAsyncSupported(true));
}
}