• 【SpringBoot】SpringBoot2.x整合定时任务和异步任务处理


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

    一.项目环境

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

    二.springboot常用定时任务配置

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

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

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

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

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

    @Component
    @Async
    public class AsyncTask {
        
        public void task1() throws InterruptedException{
            long begin = System.currentTimeMillis();
            Thread.sleep(1000L);
            long end = System.currentTimeMillis();
            System.out.println("任务1耗时="+(end-begin));
        }
        
        
        public void task2() throws InterruptedException{
            long begin = System.currentTimeMillis();
            Thread.sleep(2000L);
            long end = System.currentTimeMillis();
            System.out.println("任务2耗时="+(end-begin));
        }
        
        
        public void task3() throws InterruptedException{
            long begin = System.currentTimeMillis();
            Thread.sleep(3000L);
            long end = System.currentTimeMillis();
            System.out.println("任务3耗时="+(end-begin));
        }
        
        
        //获取异步结果
        
        
        public Future<String> task4() throws InterruptedException{
            long begin = System.currentTimeMillis();
            Thread.sleep(2000L);
            long end = System.currentTimeMillis();
            System.out.println("任务4耗时="+(end-begin));
            return new AsyncResult<String>("任务4");
        }
        
        
        public Future<String> task5() throws InterruptedException{
            long begin = System.currentTimeMillis();
            Thread.sleep(3000L);
            long end = System.currentTimeMillis();
            System.out.println("任务5耗时="+(end-begin));
            return new AsyncResult<String>("任务5");
        }
        
        public Future<String> task6() throws InterruptedException{
            long begin = System.currentTimeMillis();
            Thread.sleep(1000L);
            long end = System.currentTimeMillis();
            System.out.println("任务6耗时="+(end-begin));
            return new AsyncResult<String>("任务6");
        }
    }

      测试

    @RestController
    @RequestMapping("/api/v1")
    public class UserController {
    
        
        @Autowired
        private AsyncTask task;
        
        @GetMapping("async_task")
        public JsonData exeTask() throws InterruptedException{
            
            long begin = System.currentTimeMillis();
            
            //        task.task1();
            //        task.task2();
            //        task.task3();
            Future<String> task4 = task.task4();
            Future<String> task5 = task.task5();
            Future<String> task6 = task.task6();
            //这里死循环让主线程挂起,目的是为了计算其他异步任务的执行任务的耗时
            for(;;){
                if (task4.isDone() && task5.isDone() && task6.isDone()) {
                    break;
                }
            }
            long end = System.currentTimeMillis();
            
            long total = end-begin;
            System.out.println("执行总耗时="+total);
            return JsonData.buildSuccess(total);
        }
        
        
    }
  • 相关阅读:
    菜菜小问题——python中print函数 以及单引号、双引号、三引号
    fiddler软件测试——Fiddler抓取https设置详解(图文)
    【转】fiddler抓包时出现了tunnel to ......443 解密HTTPS数据
    小菜菜mysql练习解读分析2——查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
    c语言基础——基本数据类型
    linux 02
    C#中海量数据的批量插入和更新
    linux 基本命令-01
    Linux -01 安装centos
    mysql 锁和隔离事务
  • 原文地址:https://www.cnblogs.com/july-sunny/p/11675052.html
Copyright © 2020-2023  润新知