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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.