概述
对于数据库模式的调度任务,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原理分析2----job和trigger的存储的全部内容,希望文章能够帮你解决quartz原理分析2----job和trigger的存储所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复