定時任務-----Springboot中使用Scheduled做定時任務----http://www.cnblogs.com/lirenqing/p/6596557.html
阿新 • • 發佈:2018-05-15
clas html RR work blog PE date() span org
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
定時任務-----Springboot中使用Scheduled做定時任務----http://www.cnblogs.com/lirenqing/p/6596557.html