I just started to create Quartz Scheduled Jobs in a dynamic way. So there is a QuartzConfig class where I create a SchedulerFactoryBean, JobDetailFactoryBean and CronTriggerFactoryBean. Wherein the Job and CronTrigger beans are prototype ones.
@Configuration
public class QuartzConfig {
@Autowired
ApplicationContext context;;
@Bean
public SchedulerFactoryBean quartzScheduler(){
SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
quartzScheduler.setOverwriteExistingJobs(true);
quartzScheduler.setSchedulerName("job-scheduler");
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(context);
quartzScheduler.setJobFactory(jobFactory);
return quartzScheduler;
}
@Bean
@Scope(value = "prototype")
public JobDetailFactoryBean getJobBean(){
JobDetailFactoryBean bean = new JobDetailFactoryBean();
bean.setJobClass(DailyJob.class);
bean.setGroup("daily-group");
bean.setName("daily-name");
bean.setBeanName("daily-name");
bean.getJobDataMap().put("daily", "daily");
return bean;
}
@Bean
@Scope(value = "prototype")
// @Lazy(value = true)
public CronTriggerFactoryBean getCronTriggerBean(String cron){
CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
bean.setCronExpression(cron);
bean.setJobDetail(getJobBean().getObject());
bean.setGroup("daily-group");
return bean;
}
}
In my Controller class I want to create the jobs by request. I autowire SchedulerFactoryBean to set the quartz trigger to the bean.
@Controller
public class JobController {
@Autowired
SchedulerFactoryBean quartzScheduler;
@Autowired
ApplicationContext context;;
@ResponseBody
@RequestMapping("/job/create/daily")
public String dailyJob(){
CronTriggerImpl cron = (CronTriggerImpl) context.getBean("getCronTriggerBean","30 * * ? * MON-FRI");
Trigger[] triggers = { cron };
quartzScheduler.setTriggers(triggers);
return "dailyJob";
}
}
Everything works without creating an error and the Job and the Trigger are set to the quartzScheduler (see it in debug mode). But the Job is never running. What do I miss?
And dont to forget, there is a class which implements Job:
@Component
public class DailyJob implements Job{
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Daily Job runs!");
}
}
TRIGGERStable? If yes, what state do they have? - ledniovSchedulerinstead ofSchedulerFactoryBeanand usescheduler.scheduleJob(cron)? - ledniovorg.quartz.JobPersistenceException: The job (daily-group.daily-name) referenced by the trigger does not exist.Any idea? - Patrick