<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.itheima</groupId> <artifactId>chapter091</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> </plugins> </build> </project>
server:
port: 8088
package com.itheima.service; import java.util.concurrent.Future; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Service; @Service public class MyAsyncService { /** * 模拟无返回值的异步任务处理 * * @throws Exception */ @Async public void sendSMS() throws Exception { System.out.println("调用短信验证码业务方法..."); Long startTime = System.currentTimeMillis(); Thread.sleep(5000); Long endTime = System.currentTimeMillis(); System.out.println("短信业务执行完成耗时:" + (endTime - startTime)); } /** * 模拟有返回值的异步任务处理 * * @return * @throws Exception */ @Async public Future<Integer> processA() throws Exception { System.out.println("开始分析并统计业务A数据..."); Long startTime = System.currentTimeMillis(); Thread.sleep(4000); // 模拟定义一个假的统计结果 int count = 123456; Long endTime = System.currentTimeMillis(); System.out.println("业务A数据统计耗时:" + (endTime - startTime)); return new AsyncResult<Integer>(count); } @Async public Future<Integer> processB() throws Exception { System.out.println("开始分析并统计业务B数据..."); Long startTime = System.currentTimeMillis(); Thread.sleep(5000); // 模拟定义一个假的统计结果 int count = 654321; Long endTime = System.currentTimeMillis(); System.out.println("业务B数据统计耗时:" + (endTime - startTime)); return new AsyncResult<Integer>(count); } }
package com.itheima.controller; import java.util.concurrent.Future; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.itheima.service.MyAsyncService; @RestController public class MyAsyncController { @Autowired private MyAsyncService myService; @GetMapping("/sendSMS") public String sendSMS() throws Exception { Long startTime = System.currentTimeMillis(); myService.sendSMS(); Long endTime = System.currentTimeMillis(); System.out.println("主流程耗时: " + (endTime - startTime)); return "success"; } @GetMapping("/statistics") public String statistics() throws Exception { Long startTime = System.currentTimeMillis(); Future<Integer> futureA = myService.processA(); Future<Integer> futureB = myService.processB(); int total = futureA.get() + futureB.get(); System.out.println("异步任务数据统计汇总结果: " + total); Long endTime = System.currentTimeMillis(); System.out.println("主流程耗时: " + (endTime - startTime)); return "success"; } }
package com.itheima; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @EnableAsync @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }