方式一:基于注解@Scheduled實(shí)現(xiàn)簡單定時(shí)器
DemoApplication文件
package com.example.schedule.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(value = "com.example.schedule.schedule")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
MySchedule文件
package com.example.schedule.schedule;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
@EnableScheduling
@Configuration
public class MySchedule {
@Scheduled(fixedRate = 3000)
private void configureTasks1() {
System.out.printf("xxxx");
}
@Scheduled(fixedRate = 6000)
private void configureTasks2() {
System.out.printf("xxxx");
}
}
注解含義
* @Scheduled(cron = "0/5 * * * * ?") //每隔5s執(zhí)行一次
* @Scheduled(fixedDelay = 5000) //上一次執(zhí)行完畢時(shí)間點(diǎn)之后5秒再執(zhí)行
* @Scheduled(fixedDelayString = "5000") //上一次執(zhí)行完畢時(shí)間點(diǎn)之后5秒再執(zhí)行
* @Scheduled(fixedRate = 5000) //上一次開始執(zhí)行時(shí)間點(diǎn)之后5秒再執(zhí)行
* @Scheduled(initialDelay=1000, fixedRate=5000) //第一次延遲1秒后執(zhí)行纱注,之后按fixedRate的規(guī)則每5秒執(zhí)行一次
方式二:基于接口SchedulingConfigurer的動(dòng)態(tài)定時(shí)任務(wù)
package com.example.schedule.schedule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
@Configuration
@EnableScheduling
public class DynamicSchedule implements SchedulingConfigurer {
/**
* 測試數(shù)據(jù),實(shí)際可從數(shù)據(jù)庫獲取
*/
private List<Task> tasks = Arrays.asList(
new Task(1, "任務(wù)1", "*/3 * * * * *"),
// new Task(2, "任務(wù)2", "*/3 * * * * *"),
// new Task(3, "任務(wù)3", "*/3 * * * * *"),
// new Task(4, "任務(wù)4", "*/3 * * * * *"),
// new Task(5, "任務(wù)5", "*/3 * * * * *"),
// new Task(6, "任務(wù)6", "*/3 * * * * *"),
// new Task(7, "任務(wù)7", "*/3 * * * * *"),
// new Task(8, "任務(wù)8", "*/3 * * * * *"),
// new Task(9, "任務(wù)9", "*/3 * * * * *"),
new Task(10, "任務(wù)10", "*/3 * * * * *")
);
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setScheduler(taskScheduler());
tasks.forEach(task -> {
//任務(wù)執(zhí)行線程
Runnable runnable = () -> {
System.out.println("execute task :" + task.getId());
System.out.println(Thread.currentThread());
};
//任務(wù)觸發(fā)器
Trigger trigger = triggerContext -> {
//獲取定時(shí)觸發(fā)器,這里可以每次從數(shù)據(jù)庫獲取最新記錄,更新觸發(fā)器,實(shí)現(xiàn)定時(shí)間隔的動(dòng)態(tài)調(diào)整
CronTrigger cronTrigger = new CronTrigger(task.getCron());
return cronTrigger.nextExecutionTime(triggerContext);
};
//注冊任務(wù)
scheduledTaskRegistrar.addTriggerTask(runnable, trigger);
});
//1
// scheduledTaskRegistrar.addCronTask(new Runnable() {
//
// @Override
// public void run() {
// log.warn(name+" --- > 開始");
// //業(yè)務(wù)邏輯
// log.warn(name+" --- > 結(jié)束");
// }
// }, cron); //加入時(shí)間
}
/**
* 設(shè)置TaskScheduler用于注冊計(jì)劃任務(wù)
*
* @return
*/
@Bean
public Executor taskScheduler() {
ThreadPoolTaskScheduler t = new ThreadPoolTaskScheduler();
t.setPoolSize(2);
t.setThreadNamePrefix("taskScheduler - ");
t.initialize();
return t;
}
static class Task {
/**
* 主鍵ID
*/
private int id;
/**
* 任務(wù)名稱
*/
private String name;
/**
* cron表達(dá)式
*/
private String cron;
public Task(int id, String name, String cron) {
this.id = id;
this.name = name;
this.cron = cron;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCron() {
return cron;
}
public void setCron(String cron) {
this.cron = cron;
}
}
}