SpringBoot2.x整合定时任务和异步任务处理

一.项目环境

  springboot2.x本身已经集成了定时任务模块和异步任务,可以直接使用

二.springboot常用定时任务配置

  1.在启动类上使用注解@EnableScheduling开启定时任务,使用@EnableAsync开启异步任务

  1. @SpringBootApplication //一个注解顶下面3个
  2. @EnableScheduling //开启定时任务
  3. @EnableAsync //开启异步任务
  4. public class XdclassApplication {
  5.  
  6. public static void main(String[] args) {
  7. SpringApplication.run(XdclassApplication.class, args);
  8. }
  9. }

  2.使用注解@Schedule声明这是一个定时任务,Springboot启动会扫描到该注解并标记为定时任务

  1. @Component
  2. public class TestTask {
  3.  
  4. @Scheduled(cron="*/1 * * * * *")
  5. public void sum2(){
  6. System.out.println("cron 每秒 当前时间:"+new Date());
  7. }
  8.  
  9. }

  3.使用@Asyn声明这是一个异步任务的方法,如果在类上使用该注解,则该类下所有方法都是异步任务的方法

  1. @Component
  2. @Async
  3. public class AsyncTask {
  4.  
  5. public void task1() throws InterruptedException{
  6. long begin = System.currentTimeMillis();
  7. Thread.sleep(1000L);
  8. long end = System.currentTimeMillis();
  9. System.out.println("任务1耗时="+(end-begin));
  10. }
  11.  
  12. public void task2() throws InterruptedException{
  13. long begin = System.currentTimeMillis();
  14. Thread.sleep(2000L);
  15. long end = System.currentTimeMillis();
  16. System.out.println("任务2耗时="+(end-begin));
  17. }
  18.  
  19. public void task3() throws InterruptedException{
  20. long begin = System.currentTimeMillis();
  21. Thread.sleep(3000L);
  22. long end = System.currentTimeMillis();
  23. System.out.println("任务3耗时="+(end-begin));
  24. }
  25.  
  26. //获取异步结果
  27.  
  28. public Future<String> task4() throws InterruptedException{
  29. long begin = System.currentTimeMillis();
  30. Thread.sleep(2000L);
  31. long end = System.currentTimeMillis();
  32. System.out.println("任务4耗时="+(end-begin));
  33. return new AsyncResult<String>("任务4");
  34. }
  35.  
  36. public Future<String> task5() throws InterruptedException{
  37. long begin = System.currentTimeMillis();
  38. Thread.sleep(3000L);
  39. long end = System.currentTimeMillis();
  40. System.out.println("任务5耗时="+(end-begin));
  41. return new AsyncResult<String>("任务5");
  42. }
  43.  
  44. public Future<String> task6() throws InterruptedException{
  45. long begin = System.currentTimeMillis();
  46. Thread.sleep(1000L);
  47. long end = System.currentTimeMillis();
  48. System.out.println("任务6耗时="+(end-begin));
  49. return new AsyncResult<String>("任务6");
  50. }
  51. }

  测试

  1. @RestController
  2. @RequestMapping("/api/v1")
  3. public class UserController {
  4.  
  5. @Autowired
  6. private AsyncTask task;
  7.  
  8. @GetMapping("async_task")
  9. public JsonData exeTask() throws InterruptedException{
  10.  
  11. long begin = System.currentTimeMillis();
  12.  
  13. // task.task1();
  14. // task.task2();
  15. // task.task3();
  16. Future<String> task4 = task.task4();
  17. Future<String> task5 = task.task5();
  18. Future<String> task6 = task.task6();
  19. //这里死循环让主线程挂起,目的是为了计算其他异步任务的执行任务的耗时
  20. for(;;){
  21. if (task4.isDone() && task5.isDone() && task6.isDone()) {
  22. break;
  23. }
  24. }
  25. long end = System.currentTimeMillis();
  26.  
  27. long total = end-begin;
  28. System.out.println("执行总耗时="+total);
  29. return JsonData.buildSuccess(total);
  30. }
  31.  
  32. }

【SpringBoot】SpringBoot2.x整合定时任务和异步任务处理的更多相关文章

  1. SpringBoot2.x整合定时任务和异步任务处理

    SpringBoot2.x整合定时任务和异步任务处理 一.项目环境 springboot2.x本身已经集成了定时任务模块和异步任务,可以直接使用 二.springboot常用定时任务配置 1.在启动类 ...

  2. 小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解

    笔记 1.SpringBoot定时任务schedule讲解     简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类            ...

  3. SpringBoot整合定时任务和异步任务处理 3节课

    1.SpringBoot定时任务schedule讲解   定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类        ...

  4. SpringBoot整合定时任务和异步任务处理

    SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 timer:配置比较麻烦,时间延后问题, ...

  5. 【SpringBoot】整合定时任务和异步任务

    ========================10.SpringBoot整合定时任务和异步任务处理 =============================== 1.SpringBoot定时任务s ...

  6. SpringBoot整合全局异常处理&SpringBoot整合定时任务Task&SpringBoot整合异步任务

    ============整合全局异常=========== 1.整合web访问的全局异常 如果不做全局异常处理直接访问如果报错,页面会报错500错误,对于界面的显示非常不友好,因此需要做处理. 全局异 ...

  7. 【SpringBoot】息队列介绍和SpringBoot2.x整合RockketMQ、ActiveMQ

    ========================13.消息队列介绍和SpringBoot2.x整合RockketMQ.ActiveMQ ======================= 1.JMS介绍和 ...

  8. 【SpringBoot】SpringBoot2.x整合Shiro(一)

    一:什么是ACL和RBAC: ACL: Access Control List 访问控制列表 以前盛行的一种权限设计,它的核心在于用户直接和权限挂钩 优点:简单易用,开发便捷 缺点:用户和权限直接挂钩 ...

  9. SpringBoot整合定时任务----Scheduled注解实现(一个注解全解决)

    一.使用场景 定时任务在开发中还是比较常见的,比如:定时发送邮件,定时发送信息,定时更新资源,定时更新数据等等... 二.准备工作 在Spring Boot程序中不需要引入其他Maven依赖 (因为s ...

随机推荐

  1. 微信小程序和H5之间相互跳转

    1.微信小程序跳转小程序 wx.navigateToMiniProgram <script src='https://res.wx.qq.com/open/js/jweixin-1.3.0.js ...

  2. 上线 Python 应用仅需一条命令的开源框架:Zappa(详细教程)

    本文面向有 Python Web 基础的小伙伴 作者:HelloGitHub-吱吱 这里是 HelloGitHub 推出的<讲解开源项目>系列,今天要向小伙伴们介绍一个 Python 无服 ...

  3. 如何安装jenkins并简单的使用

    如何安装jenkins并使用 一.jenkins 简介: Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括 : 1.持续的软件版本发布/测试项目: 2.监控外部调用 ...

  4. XUPT-D

    /*     泰泰学长又来玩数字了,泰泰学长想让你帮他求1-n的和,但是这次的求和可不是简单的1+2+...+n. 这次的求和是这样的,如果加到一个数字是2的指数倍,那就不加,反而减掉这个数.    ...

  5. 《进击吧!Blazor!》系列入门教程 第一章 7.图表

    <进击吧!Blazor!>是本人与张善友老师合作的Blazor零基础入门教程视频,此教程能让一个从未接触过Blazor的程序员掌握开发Blazor应用的能力. 视频地址:https://s ...

  6. POJ_2752 Seek the Name, Seek the Fame 【KMP】

    一.题目 POJ2752 二.分析 比较明显的KMP运用. 但是这题不是只找一个,仔细看题后可以发现相当于是在找到最大的满足条件的后缀后,再在这个后缀里面找满足条件的后缀. 可以不断的运用KMP得出答 ...

  7. Codeforces 976C Nested Segments

    题面: 传送门 C. Nested Segments Input file: standard input Output file: standard output Time limit: 2 secon ...

  8. 2019 GDUT Rating Contest I : Problem B. Teamwork

    题面: 传送门 B. Teamwork Input file: standard input Output file: standard output Time limit: 1 second Memor ...

  9. 正则表达式-Python实现

    1.概述: Regular Expression.缩写regex,regexp,R等: 正则表达式是文本处理极为重要的工具.用它可以对字符串按照某种规则进行检索,替换. Shell编程和高级编程语言中 ...

  10. IPFS挖矿赚钱吗?IPFS挖矿是真的吗?

    IPFS一出现就获得了极高的关注度,「让人类信息永存」的口号也让其蒙上了一层神秘的面纱.今天我就来给大家自剖析,一探IPFS技术的真相. IPFS是一个去中心化存储网络,而Filecoin是IPFS激 ...