quartz原理分析2----job和trigger的存储
作者:互联网
quartz原理分析2----job和trigger的存储
2018年07月16日 16:44:56 李民11 阅读数:254
对于数据库模式的调度任务,job和trigger需要被存储到数据库中。当服务器再次启动的时候调度任务可以继续执行。
下面分析任务的存储代码。
-
public static void main(String[] args) throws SchedulerException {
-
SchedulerFactory sf = new StdSchedulerFactory();
-
Scheduler scheduler = sf.getScheduler();
-
scheduler.pauseJob(JobKey.jobKey("ramJob", "ramGroup"));
-
scheduler.pauseTrigger(TriggerKey.triggerKey("ramTrigger", "ramTriggerGroup"));
-
scheduler.unscheduleJob(TriggerKey.triggerKey("ramTrigger", "ramTriggerGroup"));
-
scheduler.deleteJob(JobKey.jobKey("ramJob", "ramGroup"));
-
//job
-
JobDetail jb = JobBuilder.newJob(RamJob.class).withDescription("description:ram job")
-
.withIdentity("ramJob", "ramGroup").build();
-
//trigger
-
Trigger trigger = TriggerBuilder.newTrigger().withDescription("ram trigger")
-
.withIdentity("ramTrigger", "ramTriggerGroup").startAt(new Date(System.currentTimeMillis() + 3 * 1000))
-
.withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")).build();
-
//包含存储操作
-
scheduler.scheduleJob(jb, trigger);
-
scheduler.start();
-
}
Schedule.scheduleJob方法执行存储操作。
-
public Date scheduleJob(JobDetail jobDetail, Trigger trigger)
-
throws SchedulerException {
-
return sched.scheduleJob(jobDetail, trigger);
-
}
sched是 QuartzScheduler实例,包含存储job和trigger的方法。
-
public Date scheduleJob(JobDetail jobDetail,
-
Trigger trigger) throws SchedulerException {
-
validateState();
-
if (jobDetail == null) {
-
throw new SchedulerException("JobDetail cannot be null");
-
}
-
if (trigger == null) {
-
throw new SchedulerException("Trigger cannot be null");
-
}
-
if (jobDetail.getKey() == null) {
-
throw new SchedulerException("Job's key cannot be null");
-
}
-
if (jobDetail.getJobClass() == null) {
-
throw new SchedulerException("Job's class cannot be null");
-
}
-
OperableTrigger trig = (OperableTrigger)trigger;
-
if (trigger.getJobKey() == null) {
-
trig.setJobKey(jobDetail.getKey());
-
} else if (!trigger.getJobKey().equals(jobDetail.getKey())) {
-
throw new SchedulerException(
-
"Trigger does not reference given job!");
-
}
-
trig.validate();
-
Calendar cal = null;
-
if (trigger.getCalendarName() != null) {
-
cal = resources.getJobStore().retrieveCalendar(trigger.getCalendarName());
-
}
-
Date ft = trig.computeFirstFireTime(cal);
-
if (ft == null) {
-
throw new SchedulerException(
-
"Based on configured schedule, the given trigger '" + trigger.getKey() + "' will never fire.");
-
}
-
resources.getJobStore().storeJobAndTrigger(jobDetail, trig);
-
notifySchedulerListenersJobAdded(jobDetail);
-
notifySchedulerThread(trigger.getNextFireTime().getTime());
-
notifySchedulerListenersSchduled(trigger);
-
return ft;
-
}
resources.getJobStore().storeJobAndTrigger(jobDetail, trig);是执行存储的语句。
resources是QuartzSchedulerResources的实例,此类中包含JobStore的实例,JobStore是专门用于存储job和trigger对象的接口。此处使用JobStoreSupport中的方法。JobStoreSupport实现了JobStore接口。
-
public void storeJobAndTrigger(final JobDetail newJob,
-
final OperableTrigger newTrigger)
-
throws JobPersistenceException {
-
executeInLock(
-
(isLockOnInsert()) ? LOCK_TRIGGER_ACCESS : null,
-
new VoidTransactionCallback() {
-
public void executeVoid(Connection conn) throws JobPersistenceException {
-
storeJob(conn, newJob, false);
-
storeTrigger(conn, newTrigger, newJob, false,
-
Constants.STATE_WAITING, false, false);
-
}
-
});
-
}
storeJob 和 storeTrigger方法使用sql语句插入任务信息。此处不再列出代码。
标签:quartz,job,trigger,SchedulerException,new,null,jobDetail 来源: https://blog.csdn.net/f45056231p/article/details/90204528