zoukankan      html  css  js  c++  java
  • Spring基于SchedulingConfigurer实现定时任务

      Spring 基于 SchedulingConfigurer 实现定时任务,代码如下:

    import org.springframework.scheduling.annotation.SchedulingConfigurer;
    import org.springframework.scheduling.config.ScheduledTaskRegistrar;
    import org.springframework.scheduling.support.CronTrigger;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    /**
     * Spring基于SchedulingConfigurer实现定时任务
     */
    @Component
    public class TestTask implements SchedulingConfigurer {
    
        // 执行定时任务时间(0 0 2 * * ? 表示上午2点,0 30 9 * * ? 表示上午九点30分)
        private String cron = "0 0 2 * * ?";
    
        @Override
        public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
            taskRegistrar.addTriggerTask(() -> {
                // 定时任务要执行的内容
                System.out.println("【开始执行定时任务。。。】");
            }, triggerContext -> {
                // 定时任务触发,可修改定时任务的执行周期
                CronTrigger trigger = new CronTrigger(cron);
                Date nextExecDate = trigger.nextExecutionTime(triggerContext);
                return nextExecDate;
            });
        }
    }
    

      

  • 相关阅读:
    为django项目创建虚拟环境
    linux下安装python
    使用scrapy-crawlSpider 爬取tencent 招聘
    linux基础3
    Scrapy
    scrapy-Redis 分布式爬虫
    scrapy-redis(一)
    Linux中文件上传使用rz
    centos 7 安装nginx
    MySQL 5.7 zip 文件安装过程
  • 原文地址:https://www.cnblogs.com/Big-Boss/p/11707891.html
Copyright © 2011-2022 走看看