雖然定時(shí)任務(wù)可以嵌?到web應(yīng)?程序和WAR?件中,但下?演??種更簡單的?法創(chuàng)建了?個(gè)獨(dú)?的應(yīng)?程序。您將所有的內(nèi)容打包在?個(gè)單?的、可執(zhí)?的JAR?件中,??個(gè)傳統(tǒng)Java main()?法驅(qū)動(dòng)。這也就是springboot的啟動(dòng)類。
一、入門案例
1.1 主程序
package com.zqh;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class VueApplication {
public static void main(String[] args) {
SpringApplication.run(VueApplication.class,args);
}
}
1.2 任務(wù)類代碼
@Component
public class MyScheduled {
/**
* fixedRate: 固定時(shí)間去執(zhí)行。
* fixedDelay: 固定的延遲。
*
* cron有六個(gè)站位符號(hào): 第一個(gè)表示秒,第二個(gè)是分,第三個(gè)小時(shí),第四是日,
* 第五個(gè)是月份,第六個(gè)是星期
* 0/2 * * * * * 每兩秒執(zhí)行一次
* 0 25 11 * * * 每天11:25執(zhí)行
* 0 0/5 11 * * * 每天的11點(diǎn),每隔5分鐘執(zhí)行一次
* 0 0 20 * * * 每天晚上8點(diǎn)鐘執(zhí)行
* 0 0 8,20 * * * 每天早晚8點(diǎn)執(zhí)行一次
* 0 0 8-20 * * * 每天早上8點(diǎn)到晚8點(diǎn),每個(gè)小時(shí)執(zhí)行一次
* 0 0 12 L * * * 每個(gè)月的最后一天12點(diǎn)鐘執(zhí)行。
*/
@Scheduled(fixedRate = 5000)
public void test1() {
System.out.println("定時(shí)任務(wù)觸發(fā)了");
}
}
二、 參數(shù)介紹
- <font color="red">fixedRate</font>:表示從調(diào)用開始每次延遲多少毫秒繼續(xù)調(diào)用(如果有一個(gè)任務(wù)超時(shí)了,則等待此任務(wù)執(zhí)行完成再執(zhí)行下個(gè)任務(wù))
- <font color="red">fixedDelay</font>:表示一個(gè)任務(wù)結(jié)束后延遲多少秒繼續(xù)下一個(gè)周期
- <font color="red">initialDelay</font>:表示在第一次執(zhí)行fixedRate()或fixedDelay()任務(wù)之前延遲的毫秒數(shù)
//單位是毫秒,表?第?次執(zhí)?fixedDelay()任務(wù)之前先延遲10秒
@Scheduled(fixedDelay=5000, initialDelay=10000)
- <font color="red">@Scheduled(cron="* * * * *")</font>:
"0 0 * * * *" = 每天每時(shí)整點(diǎn)
"*/10 * * * * *" = 每?秒(10:20:00, 10:20:10, 10:20:20 ...)觸發(fā)
"0 0 8-10 * * *" = 每天早上8:00、9:00 和 10:00 觸發(fā)
"0 0 6,19 * * *" = 每天6:00 和 19:00 觸發(fā)
"0 0/30 8-10 * * *" = 每天8:00, 8:30, 9:00, 9:30, 10:00 和 10:30 觸發(fā)
"0 0 9-17 * * MON-FRI" = 朝九晚五(周??周五9:00-17:00的整點(diǎn))觸發(fā)
"0 0 0 25 12 ?" = 圣誕節(jié)(每年的12?25?00:00)觸發(fā)
"0 15 10 L * ?" = 每?最后??的上午10:15觸發(fā)
"0 15 10 ? * 6L" = 每?的最后?個(gè)星期五上午10:15觸發(fā)
"0 15 10 ? * 6#3" = 每?的第三個(gè)星期五上午10:15觸發(fā) 1234567891011
三、并行執(zhí)行任務(wù)
??上面的方式中,如果我們開啟多個(gè)定時(shí)任務(wù),他們會(huì)使用同一個(gè)線程開啟任務(wù),可能會(huì)因?yàn)槟硞€(gè)任務(wù)阻塞或異常而導(dǎo)致其他任務(wù)沒有執(zhí)行。所以我們可以使用多線程并行執(zhí)行任務(wù),只需要添加一個(gè)線程池即可。
3.1 并行類
@Component
public class MySheduleConfig implements SchedulingConfigurer {
public Executor getExecutor() {
return Executors.newScheduledThreadPool(3); //開啟特定的任務(wù)線程,開啟3個(gè)
}
//
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(getExecutor());
}
}
3.2 任務(wù)類
@Component
public class MySchedule {
@Scheduled(cron = "0/5 * * * * *")
public void doSomething() {
System.out.println("每五秒執(zhí)行的任務(wù)線程名:" + Thread.currentThread().getName());
System.out.println("定時(shí)任務(wù)開始................");
}
@Scheduled(cron = "0/3 * * * * *")
public void doSomething1() {
System.out.println("每3秒執(zhí)行的任務(wù)線程名:" + Thread.currentThread().getName());
System.out.println("定時(shí)任務(wù)每3秒執(zhí)行一次次,任務(wù)開始執(zhí)行...");
}
@Scheduled(cron = "0/10 * * * * *")
public void doSomething2() {
System.out.println("每10秒執(zhí)行的任務(wù)線程名:" + Thread.currentThread().getName());
System.out.println("定時(shí)任務(wù)每天早8點(diǎn)到晚8點(diǎn),每20分鐘執(zhí)行一次,開始");
}
}