SpringBoot學習筆記(12):計劃任務
阿新 • • 發佈:2018-07-05
當前 frame mode oot odin 構建java項目 ati pre string
SpringBoot學習筆記(12):計劃任務
快速開始
本指南將指導您完成使用Spring安排任務的步驟。
參考教程:
https://spring.io/guides/gs/scheduling-tasks/
https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#scheduling
使用Maven進行構建
首先,設置一個基本的構建腳本。在使用Spring構建應用程序時,您可以使用任何您喜歡的構建系統,但此處包含了使用Maven所需的代碼。如果您不熟悉Maven,請參閱使用Maven構建Java項目。<?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>2.0.3.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>
創建一個計劃任務
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; @Component public class ScheduledTasks { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 1000) public void reportCurrentTime() { System.out.println(dateFormat.format(new Date())); } }
@Schedule註釋定義特定方法何時運行。註意:此示例使用fixedRate,它指定從每次調用的開始時間開始測量的方法調用之間的間隔。還有其他選項,例如fixedDelay,它指定從完成任務開始測量的調用之間的間隔。您還可以使用@Scheduled(cron =“...”)表達式進行更復雜的任務調度。
如果您需要更為復雜的時間調度,請參考:
https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#scheduling-annotation-support-scheduled
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html
http://www.manpagez.com/man/5/crontab/
"0 0 * * * *" = the top of every hour of every day. "*/10 * * * * *" = every ten seconds. "0 0 8-10 * * *" = 8, 9 and 10 o‘clock of every day. "0 0 6,19 * * *" = 6:00 AM and 7:00 PM every day. "0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 every day. "0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays "0 0 0 25 12 ?" = every Christmas Day at midnight
啟用計劃
@EnableScheduling確保創建後臺任務執行程序。沒有它,沒有任何安排。
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); } }
測試
在控制臺按照規定間隔打印當前時間
SpringBoot學習筆記(12):計劃任務