I am working on a job thats needs to run every 6 hours, I have used this guide
My job is being executed every couple of minutes while i dont expect it to run this often, i have set the trigger to run every 6 hours with
.withSchedule(simpleSchedule().repeatForever().withIntervalInHours(SIX_HOURS))
So why Spring Quart Scheduler Job running more times than the intervals as expected.
And created tables for spring quartz as described here
And this is my Code
quartz maven version:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
quart.properties
org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
spring.quartz.job-store-type=jdbc
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.scheduler.instanceName = DirectDataLoader
org.quartz.threadPool.threadCount = 3
The Job class
@Component
public class AbortStuckExecutionLogsJob implements Job {
private final HistoryLogService historyLogService;
@Autowired
public AbortStuckExecutionLogsJob(HistoryLogService historyLogService) {
this.historyLogService = historyLogService;
}
@Override
@TimberLogTask(name = ABORT_ZOMBIE_EXECUTION_LOGS)
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
historyLogService.abortStuckExecutionLogs();
}
}
SpringQuartzScheduler class:
@Configuration
@EnableAutoConfiguration
public class SpringQuartzScheduler {
public static final int SIX_HOURS = 6;
private final ApplicationContext applicationContext;
@Autowired
public SpringQuartzScheduler(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Bean
public Trigger trigger(JobDetail job) {
return TriggerBuilder.newTrigger().forJob(job)
.withIdentity("abort_stuck_jobs")
.withDescription("abort zombies executions trigger")
.withSchedule(simpleSchedule().repeatForever().withIntervalInHours(SIX_HOURS))
.build();
}
@Bean
public JobDetail jobDetail() {
JobDetail jobDetail = JobBuilder.newJob().ofType(AbortStuckExecutionLogsJob.class)
.storeDurably()
.withIdentity("abort_stuck_jobs")
.build();
return jobDetail;
}
@Bean
public SchedulerFactoryBean scheduler(Trigger trigger, JobDetail job, DataSource quartzDataSource) {
SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
schedulerFactory.setConfigLocation(new ClassPathResource("quartz.properties"));
schedulerFactory.setJobFactory(springBeanJobFactory());
schedulerFactory.setJobDetails(job);
schedulerFactory.setTriggers(trigger);
schedulerFactory.setDataSource(quartzDataSource);
return schedulerFactory;
}
@Bean
public SpringBeanJobFactory springBeanJobFactory() {
SpringBeanJobFactory jobFactory = new SpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
}