你將做什麼
在這篇文章中,你將使用spring的@Scheduled
註解構建一個每隔5秒輸出一次當前時間和每分鐘的第5秒輸出一次當前時間的應用。
你需要做什麼
maven引入spring boot的依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-scheduling-tasks</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
創建一個定時任務ScheduledTasks
package com.zooper.demo;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTasks {
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
/**
* 每隔5秒執行一次
*/
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
/**
* 根據cron表達式格式觸發定時任務
* cron表達式格式:
* 1.Seconds Minutes Hours DayofMonth Month DayofWeek Year
* 2.Seconds Minutes Hours DayofMonth Month DayofWeek
* 順序:
* 秒(0~59)
* 分鐘(0~59)
* 小時(0~23)
* 天(月)(0~31,但是你需要考慮你月的天數)
* 月(0~11)
* 天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
* 年份(1970-2099)
*
* 注:其中每個元素可以是一個值(如6),一個連續區間(9-12),一個間隔時間(8-18/4)(/表示每隔4小時),一個列表(1,3,5),通配符。
* 由於"月份中的日期"和"星期中的日期"這兩個元素互斥的,必須要對其中一個設置?.
*/
@Scheduled(cron="5 * * * * ?")
public void cronScheduled() {
log.info("cron : The time is now {}", dateFormat.format(new Date()));
}
}
啓用定時任務
package com.zooper.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}
執行結果:
2017-10-27 17:44:46.294 INFO 280 --- [pool-1-thread-1] com.zooper.demo.ScheduledTasks : The time is now 17:44:46
2017-10-27 17:44:51.294 INFO 280 --- [pool-1-thread-1] com.zooper.demo.ScheduledTasks : The time is now 17:44:51
2017-10-27 17:44:56.295 INFO 280 --- [pool-1-thread-1] com.zooper.demo.ScheduledTasks : The time is now 17:44:56
2017-10-27 17:45:01.299 INFO 280 --- [pool-1-thread-1] com.zooper.demo.ScheduledTasks : The time is now 17:45:01
2017-10-27 17:45:05.003 INFO 280 --- [pool-1-thread-1] com.zooper.demo.ScheduledTasks : cron : The time is now 17:45:05
2017-10-27 17:45:06.294 INFO 280 --- [pool-1-thread-1] com.zooper.demo.ScheduledTasks : The time is now 17:45:06