Async methods

There is still a bottleneck in our application; when a user searches ten keywords, each search will be executed sequentially. We could easily improve the speed of our application by using different threads and launching all the searches at the same time.

To enable Spring's asynchronous capabilities, one must use the @EnableAsync annotation. This will transparently execute any method annotated with @Async using a java.util.concurrent.Executor.

It is possible to customize the default executor used by implementing the AsyncConfigurer interface. Let's create a new configuration class called AsyncConfig in the config package:

package masterSpringMvc.config; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; ...

Get Spring MVC: Designing Real-World Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.