• SpringBoot 定时任务


    1.简单的基于注解形式

    package com.syyj.corp.config;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    import java.time.LocalDateTime;
    
    /**
     * @Title: SaticScheduleTask
     * @ProjectName corp
     * @date 2019/5/2917:26
     */
    @Slf4j
    @Component
    @Configuration
    @EnableScheduling
    public class SaticScheduleTask_A {
    //    @Scheduled(cron = "0/5 * * * * ?") 5秒执行
        @Scheduled(fixedRate = 5000)
        private void configureTasks() {
            log.info("执行静态定时任务时间: " + LocalDateTime.now());
        }
    }
    

    2.基于接口实现

    package com.syyj.corp.config;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.scheduling.annotation.SchedulingConfigurer;
    import org.springframework.scheduling.config.ScheduledTaskRegistrar;
    import org.springframework.scheduling.support.CronTrigger;
    import org.springframework.stereotype.Component;
    import org.springframework.util.StringUtils;
    
    import java.time.LocalDateTime;
    
    /**
     * @Title: SaticScheduleTask
     * @ProjectName corp
     * @date 2019/5/2917:26
     */
    @Slf4j
    @Component
    @Configuration
    @EnableScheduling
    public class SaticScheduleTask_B implements SchedulingConfigurer {
    
        @Override
        public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
            taskRegistrar.addTriggerTask(
                    () ->
                            log.info("执行动态定时任务: " + LocalDateTime.now().toLocalTime()),
                    triggerContext -> {
                        //2.1 从数据库获取执行周期
                        String cron = "0/5 * * * * ?";
                        //2.2 合法性校验.
                        if (StringUtils.isEmpty(cron)) {
                            // Omitted Code ..
                        }
                        //2.3 返回执行周期(Date)
                        return new CronTrigger(cron).nextExecutionTime(triggerContext);
                    }
            );
        }
    //    如果在数据库修改时格式出现错误,则定时任务会停止,即使重新修改正确;此时只能重新启动项目才能恢复。
    }
    

    3.多线程定时任务

    package com.syyj.corp.config;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.*;
    import org.springframework.scheduling.config.ScheduledTaskRegistrar;
    import org.springframework.scheduling.support.CronTrigger;
    import org.springframework.stereotype.Component;
    import org.springframework.util.StringUtils;
    
    import java.time.LocalDateTime;
    
    /**
     * @Title: SaticScheduleTask
     * @ProjectName corp
     * @date 2019/5/2917:26
     */
    @Slf4j
    @Component
    @Configuration
    @EnableScheduling
    @EnableAsync
    public class SaticScheduleTask_C {
        @Async
        @Scheduled(fixedDelay = 1000)  //间隔1秒
        public void first() throws InterruptedException {
            System.out.println("第一个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "
    线程 : " + Thread.currentThread().getName());
            System.out.println();
            Thread.sleep(1000 * 10);
        }
    
        @Async
        @Scheduled(fixedDelay = 2000)
        public void second() {
            System.out.println("第二个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "
    线程 : " + Thread.currentThread().getName());
            System.out.println();
        }
    }
    

     参考: https://www.cnblogs.com/mmzs/p/10161936.html 很棒

  • 相关阅读:
    2022北航软件研究生入学考试991考试大纲-数据结构与C
    pgsql 学习
    Java开发必须掌握的 20+ 种 Spring 常用注解
    Spring 学习总结
    Spring MVC快速入门教程
    spring boot与spring mvc的区别是什么?
    Java知识体系最强总结(2020版)
    arthas(阿尔萨斯)使用实践----查看慢方法 /方法耗时等
    JVM --------jmap-----查看堆内存信息、生成heap dump 文件(转储堆内存快照到指定文件)
    [JVM】jstat命令详解---JVM的统计监测工具:jstat 堆内存各部分的使用量,以及加载类的数量。
  • 原文地址:https://www.cnblogs.com/412013cl/p/10945288.html
Copyright © 2020-2023  润新知