說明
Quartz是一個任務(wù)調(diào)度框架,基于一定的時間規(guī)則實現(xiàn)某項定時任務(wù)。
幾個需要了解的對象:
- Scheduler:任務(wù)調(diào)度器
- Job:任務(wù)
- jobDetail:任務(wù)的實例
- Trigger:觸發(fā)器,任務(wù)在什么時候會執(zhí)行
一圾旨、添加maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
二毒租、添加一個任務(wù)
public class UserJob extends QuartzJobBean {
//任務(wù)傳入的參數(shù)
private Integer uid;
public void setUid(Integer uid) {
this.uid = uid;
}
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
System.out.println(String.format("Hello %s!", this.uid));
}
}
三、添加對任務(wù)的操作
@Service
public class QuartzUserService {
private static final String JOB_GROUP = "event_job_group";
private static final String TRIGGER_GROUP = "event_trigger_group";
@Autowired
private Scheduler scheduler;
/**
* 添加一個任務(wù)
* 1是整、需要獲取到任務(wù)調(diào)度器Scheduler
* 2、定義jobDetail
* 3民假、定義trigger
* 4浮入、使用Scheduler添加任務(wù)
* @param user
* @throws SchedulerException
*/
public void addQuartz(User user) throws SchedulerException {
//任務(wù)開始時間
Date triggerDate = user.getStartTime();
//定義jobDetail
JobDetail job = JobBuilder.newJob(UserJob.class)
.withIdentity(user.getUid().toString(), JOB_GROUP)
.usingJobData("uid", user.getUid())
.build();
//cron表達(dá)式 表示每隔i秒執(zhí)行
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(String.format("0/%d * * * * ? ",10)).withMisfireHandlingInstructionDoNothing();
//定義trigger
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(user.getUid().toString(), TRIGGER_GROUP)
.startAt(triggerDate)
.withSchedule(scheduleBuilder)
.build();
//使用Scheduler添加任務(wù)
scheduler.scheduleJob(job, trigger);
}
/**
* 刪除定時任務(wù)
* 1、獲取到定時任務(wù)調(diào)度器
* 2羊异、停止觸發(fā)器
* 3事秀、刪除觸發(fā)器
* 4、刪除任務(wù)
* @param uid
*/
public void deleteJob(Integer uid) throws SchedulerException {
//停止觸發(fā)器
TriggerKey triggerKey = TriggerKey.triggerKey(uid.toString(), TRIGGER_GROUP);
scheduler.pauseTrigger(triggerKey);
//刪除觸發(fā)器
scheduler.unscheduleJob(triggerKey);
//刪除任務(wù)
JobKey jobKey = JobKey.jobKey(uid.toString(),JOB_GROUP);
scheduler.deleteJob(jobKey);
}
/**
* 修改一個任務(wù)
* @param uid
* @throws SchedulerException
*/
public void updateJob(Integer uid) throws SchedulerException {
//查詢?nèi)蝿?wù)
TriggerKey triggerKey = TriggerKey.triggerKey(uid.toString(), TRIGGER_GROUP);
CronTrigger trigger = (CronTrigger)scheduler.getTrigger(triggerKey);
if(trigger == null) {
return;
}
//cron表達(dá)式 表示每隔i秒執(zhí)行
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(String.format("0/%d * * * * ? ",8)).withMisfireHandlingInstructionDoNothing();
//重新定義trigger
trigger = TriggerBuilder.newTrigger()
.withIdentity(triggerKey)
.withSchedule(scheduleBuilder)
.build();
//修改任務(wù)
scheduler.rescheduleJob(triggerKey, trigger);
}
/**
* 暫停所有的定時任務(wù)
* @throws SchedulerException
*/
public void standby() throws SchedulerException {
scheduler.standby();
}
/**
* 啟動所有的定時任務(wù)
* @throws SchedulerException
*/
public void startJobs() throws SchedulerException {
if(!scheduler.isShutdown()) {
scheduler.start();
}
}
/**
* 關(guān)閉所有的定時任務(wù)
* @throws SchedulerException
*/
public void shutdownJobs() throws SchedulerException {
if(!scheduler.isShutdown()) {
scheduler.shutdown();
}
}
}