Search code examples
javaspring-bootcronquartz-scheduler

Why is my Quartz trigger not updating changed Cron expression on restart of my Spring boot application?


I have created a Cron schedule trigger in my spring boot application as follows and it is getting fired perfectly fine. The problem is that when I change the Cron schedule expression in my code below and restart the spring boot application, the Cron schedule trigger is not getting updated and still firing the old Cron schedule expression value.

On inspecting the database tables, I see that the record in the table qrtz_cron_triggers is not getting updated.

The record in the qrtz_cron_triggers table is

"quartzScheduler" "Qrtz_NEReportProcessor_Job_Trigger" "DEFAULT" "0 30 22 ? * *" "Asia/Calcutta"

How to ensure that on restart of my spring boot application, the cron schedule expression value gets updated? My code is below.

    @Bean(name = "nRJobDetail")
    public JobDetail nRJobDetail() {
        return newJob().ofType(NEReportJob.class).storeDurably().withIdentity(JobKey.jobKey("Qrtz_NEReportProcessor_Job_Detail")).withDescription("Invoke NEReportProcessor Job service...").build();
    }

    @Bean
    public Trigger nRTrigger(@Qualifier("nRJobDetail") JobDetail job) {
        return newTrigger().forJob(job).withIdentity(TriggerKey.triggerKey("Qrtz_NEReportProcessor_Job_Trigger")).withDescription("NEReportProcessor trigger")
                .withSchedule(CronScheduleBuilder.cronSchedule("0 00 23 ? * *")
                )
                .build();
    }

Solution

  • I think you forgot to configure

    spring.quartz.overwrite-existing-jobs = true

    Whether configured jobs should overwrite existing job definitions.