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

定时任务-----Springboot中使用Scheduled做定时任务----http://www.cnblogs.com/lirenqing/p/6596557.html
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

相关文章:

  • 2022-12-23
  • 2021-07-28
  • 2022-01-14
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-11
  • 2021-12-10
  • 2022-01-11
  • 2022-12-23
  • 2021-11-21
相关资源
相似解决方案