Spring asynchronous processing, @Async annotation

Spring provides support for asynchronous method execution. This can also be achieved using threads, but it makes the code more complex and sometimes results in more bugs and errors. When we need to execute a simple action in an asynchronous manner, it is a cumbersome process to handle it using threads. There are cases in which it is necessary to perform the operation asynchronously, like sending a message from one machine to another machine. The main advantage of asynchronous processing is that the caller will not have to wait for the completion of the called method. In order to execute a method in a separate thread, you need to annotate the method with the @Async annotation. 

Asynchronous processing support can be enabled by using the @EnableAsync annotation to run the @Async methods in the background thread pool. The following is an example of Java configuration to enable asynchronous processing:

@Configuration
@EnableAsync
public class SpringAppAsyncConfig { ... }

Asynchronous processing can also be enabled by using XML configuration, as follows:

<task:executor id="myappexecutor" pool-size="10" />
<task:annotation-driven executor="myappexecutor"/>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset