定时任务-----Springboot中使用Scheduled做定时任务----http://www.cnblogs.com/lirenqing/p/6596557.html

Springboot中使用Scheduled做定时任务---http://www.cnblogs.com/lirenqing/p/6596557.html

已经验证的方案:

pom文件加入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

ExampleTimer.java

复制代码
package com.example;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ExampleTimer {
     SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

        @Scheduled(fixedRate = 10000)
        public void timerRate() {
            System.out.println(dateFormat.format(new Date()));
        }
        
        //第一次延迟1秒执行,当执行完后2秒再执行
        @Scheduled(initialDelay = 1000, fixedDelay = 2000)
        public void timerInit() {
            System.out.println("init : "+dateFormat.format(new Date()));
        }

        //每天20点16分50秒时执行
        @Scheduled(cron = "50 16 20 * * ?")
        public void timerCron() {
            System.out.println("current time : "+ dateFormat.format(new Date()));
        }
}

3、启动应用程序 

启动程序,需要增加@EnableScheduling注解.

SpringBootScheduledApplication.java

原文地址:https://www.cnblogs.com/czlovezmt/p/9042373.html