background
In Java applications, in most cases, the interactive processing is achieved through synchronization; but when dealing with the interaction with third-party systems, it is easy to cause response delays. Most of them were done by multi-threading before. For such tasks, in fact, after Spring 3.x, @Async has been built-in to perfectly solve this problem. This article will complete the introduction of @Async.
- What is an asynchronous call?
Before explaining asynchronous calls, let's first look at the definition of synchronous calls; synchronization is the sequential execution of the entire processing process, when each process is executed, and the result is returned. Asynchronous call is just sending the call instruction, the caller does not need to wait for the called method to be completely executed; instead, it continues to execute the following process.
For example, in a certain call, the three process methods A, B, and C need to be called in sequence; if they are all synchronous calls, they need to be executed sequentially before the process is executed; if B is an asynchronous call In the calling method, after A is executed, B is called, instead of waiting for B to complete, the execution starts to call C. After C is executed, it means that the process is finished. - Conventional asynchronous call processing methods
in Java, generally when dealing with similar scenarios, are based on the creation of independent threads to complete the corresponding asynchronous call logic, through the execution flow between the main thread and different threads, so as to start After the independent thread, the main thread continues execution without stalling and waiting.
Introduction to Spring asynchronous execution Async
The method marked with @Async annotation in Spring is called asynchronous method, which is actually equivalent to ourselves in the current method: new Thread(()-> System.out.println("hello world!") ). br/>The basic method used according to the @Async annotation:
add @Async annotation to the method; br/>The class object of the @Async annotation method used should be a bean object managed by the Spring container; the
asynchronous method class needs to be configured Annotation @EnableAsync
Spring asynchronous execution Async example
br/>@EnableAsync is equivalent to AsyncConfigurationSelector. According to the mode in @Configuration, it is determined whether the asynchronous mode is ProxyAsyncConfiguration or AspectJAsyncConfiguration. The default mode is AdviceMode.PROXY. Register processing bean at this time:
console output after AsyncAnnotationBeanPostProcessor executes the program
calling MyBean#runTask() thread: main
Running task thread: SimpleAsyncTaskExecutor-1
result from task: task result
Spring AsyncConfigurer for asynchronous execution of Async
The async annotation does not specify an executor
- If AsyncConfigurer does not define an executor, it will look for an executor named taskExecutor managed by spring. If not, a NoSuchBeanDefinitionException will be thrown and null will be returned, and then AsyncExecutionInterceptor.getDefaultExecutor will go to a new SimpleAsyncTaskExecutor, but this is not managed by spring
- If AsyncConfigurer defines an executor, this is not a spring managed executor, and you need to listen to the event yourself, and then shut down gracefully
to sum up
In Spring, the @Async annotation is used to mark asynchronous methods. Once the method is marked on the method, when other threads call this method, a new thread will be opened to process the business logic asynchronously. br/>Simply put the @Async annotation on the Bean method and it will run in a different thread, that is to say, the caller does not have to wait for the entire method to complete when executing this method.
The more interesting thing in Spring is that the event mechanism also supports asynchronous processing, if you want to use it like this.
Let's start to use JAVA's annotation configuration to enable the asynchronous processing mechanism, just simply add @EnableAsync annotation to the configuration class.