Home  >  Article  >  Java  >  Detailed explanation of @Async usage in Spring and simple example introduction

Detailed explanation of @Async usage in Spring and simple example introduction

黄舟
黄舟Original
2017-03-07 10:12:131628browse

This article mainly introduces the detailed explanation and simple examples of @Async usage in Spring. Friends in need can refer to

@Async usage in Spring

Introduction: In most Java applications, interaction processing is implemented through synchronization in most cases; however, when interacting with third-party systems, it is easy to cause slow response. In the past, most of them used multiple Threads are used to complete such tasks. In fact, after spring 3.x, @Async has been built in to perfectly solve this problem. This article will completely introduce the usage of @Async.

1. What is asynchronous call?

Before explaining asynchronous calls, let’s first look at the definition of synchronous calls; synchronization means that the entire processing process is executed sequentially, and when each process is completed, the results are returned. Asynchronous calls only send the calling instructions, and 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, three process methods A, B, and C need to be called sequentially; if they are all synchronous calls, they need to be executed sequentially before the process execution is completed. ; If B is an asynchronous calling method, after executing A, calling B does not wait for B to complete, but starts executing and calls C. After C is executed, it means that the process is completed.

2. Conventional asynchronous call processing method

In Java, when dealing with similar scenarios, it is usually based on creating independent threads to complete the corresponding Asynchronous calling logic passes the execution process between the main thread and different threads, so that after starting an independent thread, the main thread continues to execute without stalling and waiting.

3. Introduction to @Async

In Spring, methods based on @Async annotation are called asynchronous methods; when these methods are executed, they will Executed in a separate thread, the caller does not need to wait for its completion before continuing other operations.

How to enable @Async in Spring

Enabling method based on Java configuration:

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

Based on XML configuration file The enabling method is configured as follows:

<task:executor id="myexecutor" pool-size="5" /> 
<task:annotation-driven executor="myexecutor"/>

The above are the two definition methods.

4. Based on @Async no return value call

The example is as follows:

@Async //标注使用 
public void asyncMethodWithVoidReturnType() { 
  System.out.println("Execute method asynchronously. " 
   + Thread.currentThread().getName()); 
}

The method used is very Simple, one label can solve all problems.

5. Call based on @Async return value

Examples are as follows:

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

It can be found from the above example that the returned data type is Future type, which is an interface. The specific result type is AsyncResult, which needs attention.

Example of calling an asynchronous method that returns a result:

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

Analysis: The result information of these asynchronous methods is obtained by continuously It is implemented by checking the status of Future to obtain whether the current asynchronous method has been executed.

6. Exception handling mechanism based on @Async calls

In an asynchronous method, if an exception occurs, it is impossible for the caller to Perceived. If exception handling is indeed required, handle it as follows:

1. Customize the task executor that implements AsyncTaskExecutor

Define the logic and method of handling specific exceptions here.

2. Configure a custom TaskExecutor to replace the built-in task executor

Example step 1, custom TaskExecutor

public class ExceptionHandlingAsyncTaskExecutor implements AsyncTaskExecutor { 
  private AsyncTaskExecutor executor; 
  public ExceptionHandlingAsyncTaskExecutor(AsyncTaskExecutor executor) { 
    this.executor = executor; 
   } 
   ////用独立的线程来包装,@Async其本质就是如此 
  public void execute(Runnable task) {    
   executor.execute(createWrappedRunnable(task)); 
  } 
  public void execute(Runnable task, long startTimeout) { 
    /用独立的线程来包装,@Async其本质就是如此 
    executor.execute(createWrappedRunnable(task), startTimeout);      
  }  
  public Future submit(Runnable task) { return executor.submit(createWrappedRunnable(task)); 
    //用独立的线程来包装,@Async其本质就是如此。 
  }  
  public Future submit(final Callable task) { 
   //用独立的线程来包装,@Async其本质就是如此。 
    return executor.submit(createCallable(task));  
  }  
   
  private Callable createCallable(final Callable task) {  
    return new Callable() {  
      public T call() throws Exception {  
         try {  
           return task.call();  
         } catch (Exception ex) {  
           handle(ex);  
           throw ex;  
          }  
         }  
    };  
  } 
 
  private Runnable createWrappedRunnable(final Runnable task) {  
     return new Runnable() {  
       public void run() {  
         try { 
           task.run();  
         } catch (Exception ex) {  
           handle(ex);  
          }  
      } 
    };  
  }  
  private void handle(Exception ex) { 
   //具体的异常逻辑处理的地方 
   System.err.println("Error during @Async execution: " + ex); 
  } 
}

Analysis: It can be found that it implements AsyncTaskExecutor, using independent threads to perform specific method operations. In createCallable and createWrapperRunnable, the exception handling method and mechanism are defined.

handle() is where we need to focus on exception handling in the future.

Contents in the configuration file:

<task:annotation-driven executor="exceptionHandlingTaskExecutor" scheduler="defaultTaskScheduler" /> 
<bean id="exceptionHandlingTaskExecutor" class="nl.jborsje.blog.examples.ExceptionHandlingAsyncTaskExecutor"> 
  <constructor-arg ref="defaultTaskExecutor" /> 
</bean> 
<task:executor id="defaultTaskExecutor" pool-size="5" /> 
<task:scheduler id="defaultTaskScheduler" pool-size="1" />

Analysis: The configuration here uses a custom taskExecutor to replace the default TaskExecutor.

7. Transaction processing mechanism in @Async calls

The method marked with @Async is also marked with @Transactional; in it When a database operation is called, transaction management control will not be available because it is an operation based on asynchronous processing.

So how to add transaction management to these operations? Methods that require transaction management operations can be placed inside the asynchronous method, and @Transactional.

is added to the internally called method. For example: Method A is annotated with @Async/@Transactional, but a transaction cannot be generated. control purposes.

Method B is annotated with @Async. C and D are called in B. C/D are annotated with @Transactional respectively, which can achieve the purpose of transaction control.

8. Summary

Through the above description, we should know the methods and precautions for using @Async.

The above is a detailed explanation of the usage of @Async in Spring and a simple example introduction. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn