Preface
This article mainly introduces the relevant content about Spring Boot integration's asynchronous call to Async. It is shared for your reference and learning. I won't say much below, let's take a look at the detailed introduction together.
What is an asynchronous call?
Asynchronous calls are relative to synchronous calls. Synchronous calls refer to the program being executed step by step in a predetermined order. Each step must be executed until the previous step is executed. Asynchronous calls do not need to wait for the previous program to be executed.
Asynchronous processing method
How to implement asynchronous calls?
Multithreading is a keyword that many people think of at first. Yes, multithreading is a way to implement asynchronous calls.
In non-spring projects, we want to implement asynchronous calls are to use multi-threading methods, we can implement the Runable interface ourselves or integrate the Thread class, or use the Executors thread pool provided by jdk1.5 or above.
StrngBoot provides a very convenient way to execute asynchronous calls.
Follow the official example
Enter the code
maven dependencies:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
Start the class: Add @EnableAsync annotation
@SpringBootApplication @EnableAsync public class Application{ public static void main(String[] args) { SpringApplication.run(Application.class, args); } }Controller
Just add @Async annotation on the method you need to execute asynchronously
@RestController @RequestMapping("") public class AsyncTaskController { @RequestMapping("") public String doTask() throws InterruptedException{ long currentTimeMillis = System.currentTimeMillis(); this.task1(); this.task2(); this.task3(); long currentTimeMillis1 = System.currentTimeMillis(); return "Task task total time consumed:"+(currentTimeMillis1-currentTimeMillis)+"ms"; } @Async public void task1() throws InterruptedException{ long currentTimeMillis = System.currentTimeMillis(); Thread.sleep(1000); long currentTimeMillis1 = System.currentTimeMillis(); System.out.println("task1 task time consumed:"+(currentTimeMillis1-currentTimeMillis)+"ms"); } @Async public void task2() throws InterruptedException{ long currentTimeMillis = System.currentTimeMillis(); Thread.sleep(2000); long currentTimeMillis1 = System.currentTimeMillis(); System.out.println("task2 task time-consuming:"+(currentTimeMillis1-currentTimeMillis)+"ms"); } @Async public void task3() throws InterruptedException{ long currentTimeMillis = System.currentTimeMillis(); Thread.sleep(3000); long currentTimeMillis1 = System.currentTimeMillis(); System.out.println("task3 task time-consuming:"+(currentTimeMillis1-currentTimeMillis)+"ms"); } }Main function runs the spirngboot project. After startup is completed, the browser accesses: http://localhost:8080/
Console:
Task1 task time: 1012ms Task2 task time: 2009ms Task3 task time: 3004ms
After waiting for a while, the output is entered:
Total task time: 6002ms
Asynchronously not executed!
Could it be that the code is written incorrectly? I checked it several times and found no obvious errors. I remember that spring also had similar problems with @Transactional annotation. When spring scans a class with @Transactional annotation method, a proxy class is generated, and the proxy class starts and closes the transaction. In the same class, the method call is executed in the class body, and spring cannot intercept this method call.
Suddenly, put the asynchronous task into a class separately, and adjust the code to enter:
Controller
@RequestMapping("") @RestController public class AsyncTaskController { @Autowired private AsyncTask asyncTask; @RequestMapping("") public String doTask() throws InterruptedException{ long currentTimeMillis = System.currentTimeMillis(); asyncTask.task1(); asyncTask.task2(); asyncTask.task3(); long currentTimeMillis1 = System.currentTimeMillis(); return "task task total time-consuming:"+(currentTimeMillis1-currentTimeMillis)+"ms"; } } Asynchronous task class
@Component public class AsyncTask { @Async public void task1() throws InterruptedException{ long currentTimeMillis = System.currentTimeMillis(); Thread.sleep(1000); long currentTimeMillis1 = System.currentTimeMillis(); System.out.println("task1 task time-consuming:"+(currentTimeMillis1-currentTimeMillis)+"ms"); } @Async public void task2() throws InterruptedException{ long currentTimeMillis = System.currentTimeMillis(); Thread.sleep(2000); long currentTimeMillis1 = System.currentTimeMillis(); System.out.println("task2 task time-consuming:"+(currentTimeMillis1-currentTimeMillis)+"ms"); } @Async public void task3() throws InterruptedException{ long currentTimeMillis = System.currentTimeMillis(); Thread.sleep(3000); long currentTimeMillis1 = System.currentTimeMillis(); System.out.println("task3 task time-consuming:"+(currentTimeMillis1-currentTimeMillis)+"ms"); } }Console:
Task1 task time: 1012ms Task2 task time: 2009ms Task3 task time: 3004ms
Enter the browser results:
Total task time: 19ms
The asynchronous call succeeded!
How do you know when the three asynchronous tasks are executed and what are the results of the execution? You can use the method of adding a Faature callback to judge
The code is entered:
Asynchronous task class
@Component public class AsyncTask { @Async public Future<String> task1() throws InterruptedException{ long currentTimeMillis = System.currentTimeMillis(); Thread.sleep(1000); long currentTimeMillis1 = System.currentTimeMillis(); System.out.println("task1 task time-consuming:"+(currentTimeMillis1-currentTimeMillis)+"ms"); return new AsyncResult<String>("task1 execution is completed"); } @Async public Future<String> task2() throws InterruptedException{ long currentTimeMillis = System.currentTimeMillis(); Thread.sleep(2000); long currentTimeMillis1 = System.currentTimeMillis(); System.out.println("task2 task time-consuming:"+(currentTimeMillis1-currentTimeMillis)+"ms"); return new AsyncResult<String>("task2 execution is completed"); } @Async public Future<String> task3() throws InterruptedException{ long currentTimeMillis = System.currentTimeMillis(); Thread.sleep(3000); long currentTimeMillis1 = System.currentTimeMillis(); System.out.println("task3 task time-consuming:"+(currentTimeMillis1-currentTimeMillis)+"ms"); return new AsyncResult<String>("task3 execution completed"); } } Controller
@RequestMapping("") @RestController public class AsyncTaskController { @Autowired private AsyncTask asyncTask; @RequestMapping("") public String doTask() throws InterruptedException{ long currentTimeMillis = System.currentTimeMillis(); Future<String> task1 = asyncTask.task1(); Future<String> task2 = asyncTask.task2(); Future<String> task3 = asyncTask.task3(); String result = null; for (;;) { if(task1.isDone() && task2.isDone() && task3.isDone()) { // All three tasks are called to complete, exit the loop and wait for break; } Thread.sleep(1000); } long currentTimeMillis1 = System.currentTimeMillis(); result = "Total time-consuming of tasks:"+(currentTimeMillis1-currentTimeMillis)+"ms"; return result; } }Console output:
Task1 task time: 1000ms Task2 task time: 2001ms Task3 task time: 3001ms
Browser output:
Total task time: 4015ms
The asynchronous call is successful and the program returns the result only when all tasks are completed!
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.