1、在pom.xml中增加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2、在主类上开启注解
package com.rick; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication @EnableAsync public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
3、新建任务测试类
package com.rick.task; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Component; import java.util.Random; import java.util.concurrent.Future; /** * Desc : 任务类 * User : RICK * Time : 2017/8/29 9:56 */ @Component public class Task1 { public Random random = new Random(); /** * Desc : @Async所修饰的函数不要定义为static类型,否则异步调用不会生效 * 这里通过返回Future<T>来返回异步调用的结果,实现异步回调 * User : RICK * Time : 2017/8/29 10:30 */ //任务一 @Async public Future<String> doTaskOne() throws Exception{ System.out.println("开始做任务一"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); System.out.println("完成任务一,耗时:" + (end - start) + "毫秒"); return new AsyncResult<>("test1 is done!"); } //任务二; @Async public Future<String> doTaskTwo() throws Exception { System.out.println("开始做任务二"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); System.out.println("完成任务二,耗时:" + (end - start) + "毫秒"); return new AsyncResult<>("test2 is done!"); } //任务3; @Async public Future<String> doTaskThree() throws Exception { System.out.println("开始做任务三"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); System.out.println("完成任务三,耗时:" + (end - start) + "毫秒"); return new AsyncResult<>("test3 is done!"); } }
4、创建测试控制器
package com.rick.controller; import com.rick.task.Task1; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.Future; @RestController public class TaskController { @Autowired private Task1 task1; @RequestMapping("/task1") public String task1() throws Exception{ Future<String> test1 = task1.doTaskOne(); Future<String> test2 = task1.doTaskTwo(); Future<String> test3 = task1.doTaskThree(); while (true){ if(test1.isDone()){ System.out.println("====================test1 is done========================="); } if(test2.isDone()){ System.out.println("====================test2 is done========================="); } if(test3.isDone()){ System.out.println("====================test3 is done========================="); } if(test1.isDone() && test2.isDone() && test3.isDone()){ break; } Thread.sleep(1000); } return "task1"; } }
5、启动项目测试http://localhost:8080/task1
项目代码:https://github.com/zrbfree/spring-boot-async.git
时间: 2024-11-06 03:29:07