[英]Quartz scheduling a job on every Application Run in Spring boot
I have a spring quartz application.我有一个 spring 石英应用程序。
It is scheduling a cron every time I run the application.每次我运行应用程序时它都会安排一个 cron。 I want it not to set the cron again if job already exist.
如果作业已经存在,我希望它不要再次设置 cron。
Code for scheduling调度代码
@PostConstruct
public void assignJobs() throws IOException, SchedulerException {
JobDetail jobDetail = JobBuilder.newJob(CollectTransactionDataJob.class)
.withIdentity(UUID.randomUUID().toString(), CollectTransactionDataJob.GROUP)
.withDescription(CollectTransactionDataJob.DESCRIPTION)
.storeDurably()
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.forJob(jobDetail)
.withIdentity(jobDetail.getKey().getName(), CollectTransactionDataJob.TRIGGER_GROUP)
.withDescription(CollectTransactionDataJob.TRIGGER_DESCRIPTION)
.withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(10, 30))
.build();
Scheduler scheduler = quartzConfig.schedulerFactoryBean().getScheduler();
scheduler.scheduleJob(jobDetail, trigger);
}
Expected behaviour:预期行为:
If CRON time is 10:30
Current time is 10:00
Between 10:00 and 10:30 the apllication in RUN 2 times
Then at 10:30, the CRON should RUN 1 time.
Observed behaviour观察到的行为
At 10:30, the CRON RUNs 2 times.
Quartz properties石英属性
org.quartz.jobStore.class = org.springframework.scheduling.quartz.LocalDataSourceJobStore
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
Below code snippet is working fine.下面的代码片段工作正常。
Thanks @ScaryWombat for suggestion感谢@ScaryWombat 的建议
3 param method of scheduleJob
had to be used with REPLACE boolean as true
. scheduleJob
的 3 参数方法必须与 REPLACE boolean 一起使用为true
。
@PostConstruct
public void assignJobs() throws IOException, SchedulerException {
JobDetail jobDetail = JobBuilder.newJob(CollectTransactionDataJob.class)
.withIdentity(CollectTransactionDataJob.NAME, CollectTransactionDataJob.GROUP)
.withDescription(CollectTransactionDataJob.DESCRIPTION)
.storeDurably()
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.forJob(jobDetail)
.withIdentity(jobDetail.getKey().getName(), CollectTransactionDataJob.TRIGGER_GROUP)
.withDescription(CollectTransactionDataJob.TRIGGER_DESCRIPTION)
.withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(12, 41))
.build();
Set<Trigger> setTrigger = new HashSet<>();
setTrigger.add(trigger);
Scheduler scheduler = quartzConfig.schedulerFactoryBean().getScheduler();
scheduler.scheduleJob(jobDetail, setTrigger, true); // enable REPLACE here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.