Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable async supported for a spring MVC application in java configuration file (Not XML)

I know how to enable async support in a XML configuration, I have done so for filters and servlets by adding tag

async-supported>true/async-supported

How to do it in a Java config file. I create a WebInit class which implements WebApplicationInitializer and overrides onStartUp -what should I do next?

public class WebInit implements WebApplicationInitializer {

  @Override
  public void onStartup(ServletContext container) throws ServletException {
      //What to do here, to move from XML to java config
 }
}
like image 622
cpandey05 Avatar asked Sep 14 '14 08:09

cpandey05


People also ask

How do you enable the use of Async annotation in query method?

To enable the asynchronous processing, add the @EnableAsync annotation to the configuration class. The @EnableAsync annotation switches on Spring's ability to run @Async methods in a background thread pool.

Is Spring MVC an asynchronous?

Spring MVC async relies on Servlet APIs which only provides async behavior between container threads and request processing threads but not end to end. Spring WebFlux on the other hand achieves concurrency by a fixed number of threads by using HTTP sockets and pushing chunks of data at a time through the sockets.

Why @async is not working?

@Async has two limitations: It must be applied to public methods only. Self-invocation — calling the async method from within the same class — won't work.


1 Answers

Along the following lines -

ServletRegistration.Dynamic registration = container.addServlet(servletName, myServlet);
registration.setAsyncSupported(true);

EDIT: Sorry, did not realize that you were looking for a Spring specific solution. With Spring MVC you would just extend a AbstractAnnotationConfigDispatcherServletInitializer assuming that your root and web contexts are @Configuration based. This initializer in-turn extends from AbstractDispatcherServletInitializer, this class has asyncSupported flag set by default.

like image 57
Biju Kunjummen Avatar answered Oct 03 '22 14:10

Biju Kunjummen