quartz動態任務排程實現
阿新 • • 發佈:2019-02-03
以maven工程為講解例子:
pom.xml
<!--quartz--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> <exclusions> <exclusion> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
QuartzScheduleUtil.java(排程工具類)
測試類TIMER.JAVApackage com.wlt.quartz.util; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; /** * Created by wlt on 2018/2/20. */ public class QuartzScheduleUtil { private Scheduler scheduler; public Scheduler getScheduler() { return scheduler; } public void setScheduler(Scheduler scheduler) { this.scheduler = scheduler; } public QuartzScheduleUtil(){ } public QuartzScheduleUtil(Scheduler scheduler){ this.scheduler = scheduler; } /** * 啟動一個排程物件 * @throws SchedulerException */ public void start() throws SchedulerException { if (!scheduler.isShutdown()) { scheduler.start(); } } /** * 判斷排程器是否關閉 * @return */ private boolean isShutdown(){ try { return scheduler.isShutdown(); } catch (SchedulerException e) { e.printStackTrace(); } return false; } /** * 排程是否啟動 * @return * @throws SchedulerException */ public boolean isStarted() throws SchedulerException { return scheduler.isStarted(); } /** * 關閉排程器 * @throws SchedulerException */ public void shutdown() throws SchedulerException { scheduler.shutdown(); } /** * 暫停排程任務 * @return * @throws SchedulerException */ public void unscheduleJob(QuartzScheduleJob job) throws SchedulerException{ if (!isShutdown()) { scheduler.pauseJob(new JobKey(job.getJobName(), job.getJobGroup())); scheduler.pauseTrigger(new TriggerKey(job.getJobName(), job.getTriggerGroup())); } } /** * 重啟任務排程 * @param job */ public void resumeJob(QuartzScheduleJob job){ try { if (!isShutdown()) { scheduler.resumeJob(new JobKey(job.getJobName(), job.getJobGroup())); scheduler.resumeTrigger(new TriggerKey(job.getJobName(), job.getTriggerGroup())); } } catch (SchedulerException e) { e.printStackTrace(); } } /** * 暫停觸發器 * @param job */ public void pauseTrigger(QuartzScheduleJob job) { try { if (!isShutdown()) { scheduler.pauseTrigger(new TriggerKey(job.getJobName(), job.getTriggerGroup()));// 停止觸發器 } } catch (SchedulerException e) { throw new RuntimeException(e); } } /** * 恢復觸發器 * @param job */ public void resumeTrigger(QuartzScheduleJob job) { try { if (!isShutdown()) { scheduler.resumeTrigger(new TriggerKey(job.getJobName(), job.getTriggerGroup()));// 重啟觸發器 } } catch (SchedulerException e) { throw new RuntimeException(e); } } /** * 刪除觸發器 * @param job * @return */ public boolean removeTrigdger(QuartzScheduleJob job) { TriggerKey triggerKey = new TriggerKey(job.getJobName(), job.getTriggerGroup()); try { if (!isShutdown()) { scheduler.pauseTrigger(triggerKey);// 停止觸發器 } return scheduler.unscheduleJob(triggerKey);// 移除觸發器 } catch (SchedulerException e) { throw new RuntimeException(e); } } /** * 暫停排程中所有的job * 恢復任務時,停止時間段之間的任務會被執行 */ public void pauseAll() { try { if (!isShutdown()) { scheduler.pauseAll(); } } catch (Exception e) { throw new RuntimeException(e); } } /** * 恢復排程中所有的job * @throws SchedulerException */ public void resumeAll() throws SchedulerException { if (!isShutdown()) { scheduler.resumeAll(); } } /** * 刪除一個任務 * @param job * @throws SchedulerException */ public void removeJob(QuartzScheduleJob job) throws SchedulerException{ TriggerKey triggerKey = new TriggerKey(job.getJobName(), job.getTriggerGroup()); if (!isShutdown()) { scheduler.pauseTrigger(triggerKey);//停止觸發器 scheduler.unscheduleJob(triggerKey);//移除觸發器 scheduler.deleteJob(new JobKey(job.getJobName(), job.getJobGroup()));//刪除任務 } } /** * 建立一個排程物件 * * @return * @throws SchedulerException */ public static Scheduler newScheduler() { SchedulerFactory sf = new StdSchedulerFactory(); Scheduler scheduler = null; try { scheduler = sf.getScheduler(); } catch (SchedulerException e) { e.printStackTrace(); } return scheduler; } }
package com.wlt.quartz; import com.wlt.quartz.job.HelloJob; import com.wlt.quartz.util.QuartzScheduleJob; import com.wlt.quartz.util.QuartzScheduleUtil; import org.junit.Test; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.io.IOException; import static org.quartz.CronScheduleBuilder.cronSchedule; import static org.quartz.JobBuilder.newJob; import static org.quartz.TriggerBuilder.newTrigger; /** * Created by wlt on 2018/2/20. */ public class Timer { @Test public void initJob() throws SchedulerException, IOException, InterruptedException { // Grab the Scheduler instance from the Factory QuartzScheduleUtil quartzScheduleUtil = new QuartzScheduleUtil(QuartzScheduleUtil.newScheduler()); quartzScheduleUtil.start(); String jobName = "wltTimer"; String jobGroup = "job-group"; String triggerGroup = "trigger-group"; String triggerRule = "*/5 * * * * ?"; QuartzScheduleJob quartzScheduleJob = new QuartzScheduleJob(); quartzScheduleJob.setJobGroup(jobGroup); quartzScheduleJob.setJobName(jobName); quartzScheduleJob.setTriggerGroup(triggerGroup); JobDetail job = newJob(HelloJob.class) .withIdentity(jobName, jobGroup) .build(); Trigger trigger = newTrigger() .withIdentity(jobName, triggerGroup) .withSchedule(cronSchedule(triggerRule)) .build(); try { quartzScheduleUtil.getScheduler().scheduleJob(job, trigger); System.out.println("初始化定時任務success"); System.out.println(quartzScheduleUtil.isStarted()); quartzScheduleUtil.pauseTrigger(quartzScheduleJob); Thread.sleep(6000); quartzScheduleUtil.resumeTrigger(quartzScheduleJob); } catch (SchedulerException e) { System.out.println("初始化定時任務失敗"); } System.in.read(); // 按任意鍵退出 } }
測試任務類HelloJob.java
package com.wlt.quartz.job;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import java.util.Date;
/**
* Created by wlt on 2018/2/20.
*/
//下面註解代表job是有狀態的,即多次執行的job,可以共享變數,預設是每次都會啟動一個全新的job,即無狀態job
//@PersistJobDataAfterExecution
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println(new Date().getSeconds());
}
}