Notes @Async solve the problem of asynchronous call

Preamble: Spring in @Async

According to Spring documentation, the default uses a single-threaded mode. So in Java applications, most cases are achieved through interactive processing synchronized manner.

Then when the execution of multiple tasks is bound to affect each other. For example, if A task execution time is longer, then B must wait until the task is finished A task will start execution. Another example is when dealing with third-party systems to interact, likely to cause slow response to the situation, before most of them are using multiple threads to complete such tasks, in fact, after spring3.x, has built the perfect solution to this @Async problem.

1. What is an asynchronous call?

Before I explain, let’s look at two definitions:

Synchronous call: execution order, wait on a task is finished

The entire process is performed sequentially, when the various processes are executed, and returns the result.

Asynchronous call: receiving the instruction is executed without waiting

It is just to send a command to call, the caller without waiting for the method to be called completely finished; but continue with the following procedure.

For example, in a call, call A, B, C during three sequential method:
    As they are synchronous call, then they need to be finished after the order, the process is finished side count; if B is an asynchronous invocation method, after running A, B calls, without waiting for the completion of B, and the implementation start calling C, until after the C is finished, it means that the process is finished.


2. The conventional asynchronous call handling

After time in Java, usually in the treatment of similar scenes are created based on a separate thread asynchronous call to complete the corresponding logic through the execution flow between the main thread and the different threads, so start a separate thread, the main thread to continue without creating stagnant situation of waiting. Or use TaskExecutor perform asynchronous thread, see

3. How to enable @Async in Spring?

3.0, @ Async introduction

In Spring, the method @Async based tagging, called asynchronous methods; these methods when executed, will be executed in a separate thread, the caller without waiting for its completion, we can continue with other operations.

3.1, enable @Async comment

3.1.1, Java-based way to enable configuration:

public class SpringAsyncConfig { ... }  

3.1.2, based SpringBoot configuration mode is enabled:

public class SpringBootApplication {
    public static void main(String[] args) {, args);

3.2, using @Async annotation, declare an asynchronous method call

3.2.0 In the method using no return value:

Affirming asynchronous method to invoke methods on

    @Async //标注使用
    public void downloadFile() throws Exception { ... } 

3.2.1, the return value using methods:

public Future asyncMethodWithReturnType() {  
    System.out.println("Execute method asynchronously - "  + Thread.currentThread().getName());  
    try {  
        return new AsyncResult("hello world !!!!");  
    } catch (InterruptedException e) {  
    return null;  

Can be found in the above example, the data type returned by Future type, which is an interface. Specific results types AsyncResult, this is the place to note.

Examples of asynchronous method call returns a result:

public void testAsyncAnnotationForMethodsWithReturnType()  
   throws InterruptedException, ExecutionException {  
    System.out.println("Invoking an asynchronous method. "   + Thread.currentThread().getName());  
    Future future = asyncAnnotationExample.asyncMethodWithReturnType();  
    while (true) {  ///这里使用了循环判断,等待获取结果信息  
        if (future.isDone()) {  //判断是否执行完毕  
            System.out.println("Result from asynchronous process - " + future.get());  
        System.out.println("Continue doing something else. ");  

These asynchronous methods to obtain information about the result, is to get the current asynchronous method if finished achieved by constantly checking Future state.

4. @Async call exception handling mechanism based on

In the asynchronous method, if an exception occurs, the caller caller, it can not be perceived. If you do need for exception handling, the process is performed as follows:

    Custom implementation AsyncTaskExecutor task executor
            It is defined herein as exception handling logic and specific manner.

    Configuring TaskExecutor replaced by a custom built task executor
            Example Step 1, custom TaskExecutor

public class ExceptionHandlingAsyncTaskExecutor implements AsyncTaskExecutor {  
    private AsyncTaskExecutor executor;  
    public ExceptionHandlingAsyncTaskExecutor(AsyncTaskExecutor executor) {  
        this.executor = executor;  
    public void execute(Runnable task) {       
    public void execute(Runnable task, long startTimeout) {  
       executor.execute(createWrappedRunnable(task), startTimeout);           
    public Future submit(Runnable task) { return executor.submit(createWrappedRunnable(task));  
    public Future submit(final Callable task) {  
       return executor.submit(createCallable(task));   
    private Callable createCallable(final Callable task) {   
        return new Callable() {   
            public T call() throws Exception {   
                 try {   
                 } catch (Exception ex) {   
                     throw ex;   
    private Runnable createWrappedRunnable(final Runnable task) {   
         return new Runnable() {   
             public void run() {   
                 try {  
                  } catch (Exception ex) {   
    private void handle(Exception ex) {  
      System.err.println("Error during @Async execution: " + ex);  

Analysis: it may be found to achieve AsyncTaskExecutor, performed with a separate thread for each specific operation method. In createCallable and createWrapperRunnable, the definition of exception handling methods and mechanisms.

handle () is where exception handling in the future we need to focus on.
    xml configuration file contents:


It can also be used in the form of annotations to configure it to register the bean.

Transaction processing 5. @Async invocation

In the method @Async labeling, as well as the @Transactional be labeled; when they call database operation, the control transaction management will not produce, the reason lies in its operation is based on asynchronous processing.

How to add that the transaction manager to do these operations?

The method requires transaction management operations can be placed inside the asynchronous method, add @Transactional on internal method is called


Method A, using the @ Async / @ Transactional be marked, but not the purpose of generating transaction control.

Method B, used to mark the @Async, B calls the object C, D, C / D are respectively denoted by the @Transactional made, transaction control can be realized.

6. Reference article:


Leave a Reply