资讯专栏INFORMATION COLUMN

SpringCloud(第 047 篇)注解式Async配置异步任务

StonePanda / 3497人阅读

摘要:耗时毫秒耗时毫秒耗时毫秒添加异步任务控制器测试异步任务控制器。

SpringCloud(第 047 篇)注解式Async配置异步任务

-

一、大致介绍

</>复制代码

  1. 1、有时候我们在处理一些任务的时候,需要开启线程去异步去处理,原有逻辑继续往下执行;
  2. 2、当遇到这种场景的时候,线程是可以将我们完成,然后在SpringCloud中也有这样的注解来支撑异步任务处理;
二、实现步骤 2.1 添加 maven 引用包

</>复制代码

  1. 4.0.0
  2. springms-async
  3. 1.0-SNAPSHOT
  4. jar
  5. com.springms.cloud
  6. springms-spring-cloud
  7. 1.0-SNAPSHOT
  8. org.springframework.boot
  9. spring-boot-starter-web
2.2 添加应用配置文件(springms-asyncsrcmainresourcesapplication.yml)

</>复制代码

  1. server:
  2. port: 8345
  3. spring:
  4. application:
  5. name: springms-async #全部小写
2.3 添加异步任务类(springms-asyncsrcmainjavacomspringmscloudtaskAsyncTasks.java)

</>复制代码

  1. package com.springms.cloud.task;
  2. import org.slf4j.LoggerFactory;
  3. import org.springframework.scheduling.annotation.Async;
  4. import org.springframework.scheduling.annotation.AsyncResult;
  5. import org.springframework.stereotype.Component;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Random;
  8. import java.util.concurrent.Future;
  9. /**
  10. * Async实现异步调用。
  11. *
  12. * @author hmilyylimh
  13. *
  14. * @version 0.0.1
  15. *
  16. * @date 2017/10/19
  17. *
  18. */
  19. @Component
  20. public class AsyncTasks {
  21. public static Random random = new Random();
  22. @Async
  23. public Future doTaskOne() throws Exception {
  24. System.out.println("Async, taskOne, Start...");
  25. long start = System.currentTimeMillis();
  26. Thread.sleep(random.nextInt(10000));
  27. long end = System.currentTimeMillis();
  28. System.out.println("Async, taskOne, End, 耗时: " + (end - start) + "毫秒");
  29. return new AsyncResult<>("AsyncTaskOne Finished");
  30. }
  31. @Async
  32. public Future doTaskTwo() throws Exception {
  33. System.out.println("Async, taskTwo, Start");
  34. long start = System.currentTimeMillis();
  35. Thread.sleep(random.nextInt(10000));
  36. long end = System.currentTimeMillis();
  37. System.out.println("Async, taskTwo, End, 耗时: " + (end - start) + "毫秒");
  38. return new AsyncResult<>("AsyncTaskTwo Finished");
  39. }
  40. @Async
  41. public Future doTaskThree() throws Exception {
  42. System.out.println("Async, taskThree, Start");
  43. long start = System.currentTimeMillis();
  44. Thread.sleep(random.nextInt(5000));
  45. long end = System.currentTimeMillis();
  46. System.out.println("Async, taskThree, End, 耗时: " + (end - start) + "毫秒");
  47. return new AsyncResult<>("AsyncTaskThree Finished");
  48. }
  49. }
2.4 添加异步任务Web控制器(springms-asyncsrcmainjavacomspringmscloudcontrollerAsyncTaskController.java)

</>复制代码

  1. package com.springms.cloud.controller;
  2. import com.springms.cloud.task.AsyncTasks;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import java.util.concurrent.Future;
  7. /**
  8. * 测试异步任务Web控制器。
  9. *
  10. * @author hmilyylimh
  11. *
  12. * @version 0.0.1
  13. *
  14. * @date 2017/10/19
  15. *
  16. */
  17. @RestController
  18. public class AsyncTaskController {
  19. @Autowired
  20. AsyncTasks asyncTasks;
  21. /**
  22. * 测试异步任务。
  23. *
  24. * @return
  25. * @throws Exception
  26. */
  27. @GetMapping("/task")
  28. public String task() throws Exception {
  29. long start = System.currentTimeMillis();
  30. Future task1 = asyncTasks.doTaskOne();
  31. Future task2 = asyncTasks.doTaskTwo();
  32. Future task3 = asyncTasks.doTaskThree();
  33. while(true) {
  34. if(task1.isDone() && task2.isDone() && task3.isDone()) {
  35. // 三个任务都调用完成,退出循环等待
  36. break;
  37. }
  38. Thread.sleep(1000);
  39. }
  40. long end = System.currentTimeMillis();
  41. String result = "任务全部完成,总耗时:" + (end - start) + "毫秒";
  42. return result;
  43. }
  44. }
2.5 添加微服务启动类(springms-schedulesrcmainjavacomspringmscloudMsScheduleApplication.java)

</>复制代码

  1. package com.springms.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.scheduling.annotation.EnableAsync;
  5. /**
  6. * 注解式Async配置异步任务;
  7. *
  8. * @author hmilyylimh
  9. *
  10. * @version 0.0.1
  11. *
  12. * @date 2017/10/19
  13. *
  14. */
  15. @SpringBootApplication
  16. @EnableAsync
  17. public class MsAsyncApplication {
  18. public static void main(String[] args) {
  19. SpringApplication.run(MsAsyncApplication.class, args);
  20. System.out.println("【【【【【【 Async异步任务微服务 】】】】】】已启动.");
  21. }
  22. }
三、测试

</>复制代码

  1. /****************************************************************************************
  2. 一、简单用户链接Mysql数据库微服务(Async实现异步调用):
  3. 1、添加注解 EnableAsync、Async 以及任务类上注解 Component ;
  4. 2、启动 springms-async 模块服务,启动1个端口;
  5. 3、然后在浏览器输入地址 http://localhost:8345/task 然后等待大约10多秒后,成功打印所有信息,一切正常;
  6. 总结:说明 Async 异步任务配置生效了;
  7. ****************************************************************************************/
四、下载地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接

欢迎关注,您的肯定是对我最大的支持!!!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67835.html

相关文章

  • SpringCloud 046 注解Schedule配置定时任务,不支持任务调度

    摘要:当前时间打印当前时间定时任务触发,操作多个添加数据,事务中任一异常,都可以正常导致数据回滚。当前时间当前时间添加微服务启动类注解式配置定时任务,不支持任务调度。 SpringCloud(第 046 篇)注解式Schedule配置定时任务,不支持任务调度 - 一、大致介绍 1、很多时候我们需要隔一定的时间去执行某个任务,为了实现这样的需求通常最普通的方式就是利用多线程来实现; 2、但是有...

    masturbator 评论0 收藏0
  • SpringCloud 027 )集成异构微服务系统到 SpringCloud 生态圈中(比如

    摘要:注意注解能注册到服务上,是因为该注解包含了客户端的注解,该是一个复合注解。包含了客户端注解,同时也包含了断路器模块注解,还包含了网关模块。 SpringCloud(第 027 篇)集成异构微服务系统到 SpringCloud 生态圈中(比如集成 nodejs 微服务) - 一、大致介绍 1、在一些稍微复杂点系统中,往往都不是单一代码写的服务,而恰恰相反集成了各种语言写的系统,并且我们还...

    caozhijian 评论0 收藏0
  • SpringCloud 009 )简单 Quartz 微服务,不支持分布

    摘要:添加任务成功运行任务名称添加定时任务服务定时任务服务。触发器计划列表添加测试任务类测试任务类被任务调度后执行该任务类。声明一个静态变量保存添加启动类简单微服务,不支持分布式。 SpringCloud(第 009 篇)简单 Quartz 微服务,不支持分布式 - 一、大致介绍 1、本章节仅仅只是为了测试 Quartz 在微服务中的使用情况; 2、其实若只是简单的实现任务调用而言的话,Sp...

    awkj 评论0 收藏0
  • SpringCloud 054 )简单 Quartz-Cluster 微服务,采用注解配置 Q

    摘要:加载配置文件失败加载配置文件失败添加定时调度任务定时调度任务添加定时调度任务定时调度任务执行的张表入数据库添加启动类简单微服务,采用注解配置分布式集群。 SpringCloud(第 054 篇)简单 Quartz-Cluster 微服务,采用注解配置 Quartz 分布式集群 - 一、大致介绍 1、因网友提到有没有采用注解式配置的Quartz例子,因此本人就贴上了这样一个样例; 2、至...

    isLishude 评论0 收藏0

发表评论

0条评论

StonePanda

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<