SpringBoot 异步调用

SpringBoot 异步调用:

1.在方法中使用@Async注解,表示该方法是异步方法

@Component
public class Task {
@Async("mytask")
public Future workerTask() throws InterruptedException {
Random random = new Random();
System.out.println("workerTask()方法开始执行");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
System.out.println("workerTask()方法执行结束,耗时:" + (end - start) + "毫秒");
return new AsyncResult<>("workerTask()方法执行结束 \n");
}
@Async("mytask")
public Future masterTask() throws InterruptedException {
Random random = new Random();
System.out.println("masterTask()方法开始执行");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
System.out.println("masterTask()方法执行结束,耗时:" + (end - start) + "毫秒");
return new AsyncResult<>("masterTask()方法执行结束 \n");
}
@Async("mytask")
public Future orderTask() throws InterruptedException {
Random random = new Random();
System.out.println("orderTask()方法开始执行");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
System.out.println("orderTask()方法执行结束,耗时:" + (end - start) + "毫秒");
return new AsyncResult<>("orderTask()方法执行结束 \n");
}
}

2.使用Future对异步任务进行管理,isDone()方法表示异步任务执行结束

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAsyncDemoApplicationTests {
@Autowired
private Task task;
@Test
public void contextLoads() throws InterruptedException {
}
@Test
public void testTask() throws Exception {
long start = System.currentTimeMillis();
Future worker = task.workerTask();
Future master = task.masterTask();
Future order = task.orderTask();
while (true) {
if (worker.isDone() && master.isDone() && order.isDone()) {
System.out.println("异步任务执行结束");
break;
}
Thread.sleep(1000);
}
long end = System.currentTimeMillis();
System.out.println("所有方法执行结束,耗时:" + (end - start) + "毫秒");
}
}

3.springboot项目入口使用注解@EnableAsync开启异步调用,并使用TaskExecutor配置@Bean让@Async生效

@SpringBootApplication
@EnableAsync
public class SpringAsyncDemoApplication {
/**
* 必须配置,否则Async注解失效
*
* @return
* @date 2017/12/13
*/
@Bean(name = "mytask")
public TaskExecutor workExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setThreadNamePrefix("Async-");
threadPoolTaskExecutor.setCorePoolSize(10);
threadPoolTaskExecutor.setMaxPoolSize(20);
threadPoolTaskExecutor.setQueueCapacity(600);
threadPoolTaskExecutor.afterPropertiesSet();
return threadPoolTaskExecutor;
}
public static void main(String[] args) {
SpringApplication.run(SpringAsyncDemoApplication.class, args);
}
}

4.运行测试类,结果显示:

workerTask()方法开始执行
masterTask()方法开始执行
orderTask()方法开始执行
orderTask()方法执行结束,耗时:4054毫秒
workerTask()方法执行结束,耗时:5054毫秒
masterTask()方法执行结束,耗时:9233毫秒
异步任务执行结束
所有方法执行结束,耗时:10039毫秒
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,860评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,936评论 6 342
  • 是一个咨询对话技术,需要根据收集地图去收集来访者的信息。 是弄清来访者的具体问题,以及现状与期待之间的差距的方法。...
    准儿盖洛普优势阅读 1,265评论 0 0
  • 今天我们来谈一谈“法度”。何为法度?简单说,法就是规章制度,就是纪律规范;而度则是弹性空间,就是自由裁量。 那么为...
    小新哥微课堂阅读 728评论 0 0
  • 循环 忙活完,四点了。 没有家可归。 独自异乡,第十天。 心情不好,广州阴天。 晚安。
    嫿祎阅读 138评论 -1 0