你将做什么
在这篇文章中,你将使用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