We are using Spring 4.2.1 and quartz 2.2.1. There are 2 jobs now but we will have more jobs in the future. We want to have ability to turn on/off for each job. There are a couple of approaches for this. One is to create job, jobGroup, trigger and specify the cronExpression in Java code rather than in Spring configuration file. This approach works but it seems to be that it does not sue the Spring Quartz integration and we basically managed the quartz ourselves. Please scroll down after the ScheduledJobUtility class for second approach.
public class ScheduledJobUtility {
public static Scheduler defineAndScheduleJob(String jobName, String jobGroup, String triggerName, String cronExpression, boolean isJobEnabled,Class clazz) throws SchedulerException {
Scheduler scheduler = null;
JobDetail job = newJob(clazz).withIdentity(jobName, jobGroup).build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, jobGroup).withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();
// Schedule the job with the trigger
scheduler = StdSchedulerFactory.getDefaultScheduler();
if(!scheduler.isStarted()) {
scheduler.start();
}
if(isJobEnabled) {
scheduler.scheduleJob(job, trigger);
}
return scheduler;
}
}
The second approach is to find a way to either use the existing method in Spring 4.x "org.springframework.scheduling.quartz" package. I have been trying to add my own logic in the afterPropertiesSet() method
private boolean enableJob1 = true;
private boolean enableJob2 = false;
// @Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
Scheduler scheduler = super.getScheduler();
if (enableJob1) {
JobDetail jobDetail1 = scheduler.getJobDetail(JobKey.jobKey("Job1", "JobGroup1");
Trigger trigger1 = scheduler.getTrigger(TriggerKey.triggerKey("myTrigger1","jobGroup1"));
super.getScheduler().scheduleJob(jobDetail1, trigger1);
}
if (enableRetryRenditions) {
if (enableJob2) {
JobDetail jobDetail2 = scheduler.getJobDetail(JobKey.jobKey("Job2", "JobGroup2");
Trigger trigger2 = scheduler.getTrigger(TriggerKey.triggerKey("myTrigger2","jobGroup2"));
super.getScheduler().scheduleJob(jobDetail2, trigger2);
}
}
}
I found this, http://quartz-scheduler.org/documentation/quartz-2.1.x/cookbook/UnscheduleJob
// Unschedule a particular trigger from the job (a job may have more than one trigger)
scheduler.unscheduleJob(triggerKey("trigger1", "group1"));
MySchedulerFactoryBean extends SchedulerFactoryBean"org.springframework.scheduling.quartz.SchedulerFactoryBean and is used in the configuration file
<bean class="com.spring.utility.MySchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="jobDetail1"/>
<ref bean="jobDetail2"/>
</list>
</property>
<property name="triggers">
<list>
<ref bean="cronTrigger1"/>
<ref bean="cronTrigger2"/>
</list>`enter code here`
</property>
<property name="autoStartup" value="true" />
</bean>
I did quite a bit search but could not find too much discussions on turn on/off each job. I have been looking into the "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" and "org.springframework.scheduling.quartz.CronTriggerFactoryBean" to see if I can extend that class and add my own logic to turn on and off individual job. There is a property ""autoStartup" for "org.springframework.scheduling.quartz.SchedulerFactoryBean" but I do not want to create instance of SchedulerFactoryBean for each job. That would be messy for 50 jobs.