Spring定時任務
兩步實現定時任務
- 開啓定時任務註解
- 設置執行時間
package top.onefine.schedule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.text.DateFormat;
import java.util.Date;
@EnableScheduling // 開啓定時任務註解
@SpringBootApplication
public class ScheduleApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduleApplication.class, args);
}
// 半小時執行一次
// @Scheduled(fixedRate = 30 * 60 * 1000) // 以毫秒爲單位,啓動時就會執行一次
@Scheduled(fixedRate = 3 * 1000)
public void playSomething1() {
System.out.println("執行任務1:" + DateFormat.getDateInstance().format(new Date()));
}
// 四小時執行一次
// @Scheduled(fixedRate = 4 * 60 * 60 * 1000)
@Scheduled(fixedRate = 6 * 1000)
public void playSomething12() {
System.out.println("執行任務2:" + DateFormat.getDateInstance().format(new Date()));
}
}
cron表達式
package top.onefine.schedule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.text.DateFormat;
import java.util.Date;
@EnableScheduling // 開啓定時任務註解
@SpringBootApplication
public class ScheduleApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduleApplication.class, args);
}
@Scheduled(cron = "0 0/30 9-22 * * ?") // 每天9點到22點每隔30分鐘執行一次
public void playSomething1() {
System.out.println("執行任務1:" + DateFormat.getDateInstance().format(new Date()));
}
@Scheduled(cron = "0 0 9-22/4 * * ?") // 每天9點到22點每隔4小時執行一次
public void playSomething12() {
System.out.println("執行任務2:" + DateFormat.getDateInstance().format(new Date()));
}
}
在線生成cron表達式:https://cron.qqe2.com/
cron表達式在spring中由六個部分組成(最後一個部分不被spring支持),每個部分之間由空格分隔開來。
注
dayofweek中1表示星期天,2表示星期一…7表示星期六。
注:
以1: 5爲例,分鐘上5/20表示1: 5觸發,1: 25觸發,1: 45觸發;1點就結束了!下一次從2點開始,2: 5觸發,2: 25觸發…
異步多線程實現
定時任務默認是單線程的定時任務,如果任務持續時間較長,就會將後續定時任務拖延,導致丟失任務。
兩步實現
- 開啓異步註解
- 設置異步執行
package top.onefine.schedule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.text.DateFormat;
import java.util.Date;
@EnableScheduling // 開啓定時任務註解
@EnableAsync // 開啓異步註解
@SpringBootApplication
public class ScheduleApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduleApplication.class, args);
}
@Async // 設置異步執行
// @Scheduled(cron = "1 0 0 0 0 ?")
@Scheduled(fixedRate = 1000)
public void playSomething1() {
System.out.println(Thread.currentThread().getName() + "執行任務1:" + DateFormat.getDateInstance().format(new Date()));
}
@Async // 設置異步執行
// @Scheduled(cron = "2 0 0 0 0 ?")
@Scheduled(fixedRate = 2000)
public void playSomething12() {
System.out.println(Thread.currentThread().getName() + "執行任務2:" + DateFormat.getDateInstance().format(new Date()));
}
}