Quartz是OpenSymphony開源組織在Job scheduling領域又一個開源項目。
(1)我們需要明白 Quartz 的幾個核心概念。
- Job 表示一個工作,要執行的具體內容。此接口中只有一個方法,如下:
void execute(JobExecutionContext context)
- JobDetail 表示一個具體的可執行的調度程序,Job 是這個可執行程調度程序所要執行的內容,另外 JobDetail 還包含了這個任務調度的方案和策略。
- Trigger 代表一個調度參數的配置,什麼時候去調。
- Scheduler 代表一個調度容器,一個調度容器中可以註冊多個 JobDetail 和 Trigger。當 Trigger 與 JobDetail 組合,就可以被 Scheduler 容器調度了。
(2)springboot2運用quartz,pom.xml加載spring-boot-starter-quartz架包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
2.1 簡單固定多個定時任務
@Component
@Configurable
@EnableScheduling
public class SchedulerTask {
private int count = 1;
private int count2 = 1;
@Scheduled(cron="*/15 * * * * ?")
public void process(){
System.out.println("SchedulerTask作業調度1執行次數"+(count++));
}
@Scheduled(fixedRate = 10000)
public void reportCurrentTime() {
System.out.println("SchedulerTask作業調度2執行次數"+(count2++));
}
Application類run運行
SchedulerTask作業調度1執行次數6
SchedulerTask作業調度2執行次數7