Skip to content

Instantly share code, notes, and snippets.

@rkoshy
Created November 9, 2023 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkoshy/03ead52bfe8f6b2d48c573df5be1cde9 to your computer and use it in GitHub Desktop.
Save rkoshy/03ead52bfe8f6b2d48c573df5be1cde9 to your computer and use it in GitHub Desktop.
Enable Async JAX-RS on Thorntail or Wildfly web-apps

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));
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment