12. SpringBoot定时任务

SpringBoot定时任务

  1. 定时任务配置

    package com.demo.scheduled;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    @Component
    public class MySchedule {
        @Scheduled(fixedDelay = 1000)
        public void fixedDelay(){
            System.out.println("fixed delay:"+new Date());
        }
        @Scheduled(fixedRate = 2000)
        public void fixedRate(){
            System.out.println("fixed rate:"+new Date());
        }
        @Scheduled(initialDelay = 1000,fixedRate = 2000)
        public void initialDelay(){
            System.out.println("initial delay:"+new Date());
        }
        @Scheduled(cron="0 */1 * * * ?")
        public void cron(){
            System.out.println("cron: "+new Date());
        }
    }
    
  2. 开启定时日志

    // 在SpringBoot启动类上添加注解
    @EnableScheduling
    
原文地址:https://www.cnblogs.com/forelim/p/15398008.html