Issue
I have a quartz job running in my web server, configured via Spring as follows:
@Configuration
public class StatisticsJobConfig {
@Bean
public JobDetailBean jobDetailBean() {
JobDetailBean jobDetailBean = new JobDetailBean();
jobDetailBean.setJobClass(StatisticsJob.class);
jobDetailBean.setBeanName("statisticsJobBean");
jobDetailBean.setName("statisticsJob");
jobDetailBean.setGroup("default");
return jobDetailBean;
}
@Bean
public CronTriggerBean cronTriggerBean(@Qualifier("jobDetailBean") JobDetailBean jobDetailBean) throws ParseException {
CronTriggerBean cronTriggerBean = new CronTriggerBean();
cronTriggerBean.setBeanName("cronTriggerBean");
cronTriggerBean.setJobDetail(jobDetailBean);
cronTriggerBean.setCronExpression("0/5 * * * * ?");
return cronTriggerBean;
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean(@Qualifier("cronTriggerBean") CronTriggerBean cronTriggerBean
, @Qualifier ApplicationContext applicationContext) {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setExposeSchedulerInRepository(true);
schedulerFactoryBean.setTriggers(cronTriggerBean);
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
schedulerFactoryBean.setJobFactory(jobFactory);
return schedulerFactoryBean;
}
public void setCronExpression(String cronExpression) {
this.cronExpression = cronExpression;
}
}
(The StatisticsJob class extends QuartzJobBean and implements the executeInternal method).
This job executes successfully as per the cron expression after the server starts. However, I would also like to be able to schedule this job at will, like when a user presses a button. In order to do this, I've created a restful method inside a controller on which I would like to inject the quartz scheduler, like so:
@RequestMapping(value = "/triggerJob", method = RequestMethod.GET)
public ResponseEntity<String> triggerJob(@Qualifier StdSchedulerFactory schedulerFactory) {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
JobDetail jobDetail = new JobDetail("statisticsJob",StatisticsJob.class);
scheduler.scheduleJob(jobDetail,TriggerUtils.makeImmediateTrigger("new",0, 0));
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ResponseEntity<>(HttpStatus.OK);
}
However, the job never gets scheduled, even after the code above is executed. What I am doing wrong? Is there another approach I could try?
Solution
The solution was to autowire the SchedulerFactoryBean itself in my Controller and call the rescheduleJob method on the scheduler that is accessible through it:
@Autowired
private SchedulerFactoryBean schedulerFactory;
@RequestMapping(value = "/triggerJob", method = RequestMethod.GET)
public ResponseEntity<String> triggerJob() {
try {
schedulerFactory.getScheduler().triggerJob("statisticsJob", "default");
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ResponseEntity<>(HttpStatus.OK);
}
Answered By - Tavio
Answer Checked By - Mary Flores (JavaFixing Volunteer)