Spring Async
In this section, we will see the asynchronous execution support in Spring. In certain cases, we need to execute some tasks asynchronously, because the result of that task does not require the user, so we can process that task in a separate thread. The main benefit of asynchronous programming is that we can increase the performance and responsiveness of our application.
Spring provides annotation support for asynchronous method execution via @EnableAsync
and @Async
. Let's discuss them in detail.
The @EnableAsync annotation
We can enable asynchronous processing by simply adding @EnableAsync
to a configuration class, as follows:
@Configuration @EnableAsync public class AppConfig { @Bean public AsyncTask asyncTask() { return new AsyncTask(); } }
In the previous code, we have not provided TaskExecutor
as a bean, so Spring will use a default SimpleAsyncTaskExecutor
implicitly.
The @Async annotation
Once asynchronous processing is enabled, then the methods that are annotated with...