Reputation: 42551
Recently I've started to work with Quartz Persistent Job Store with the following properties:
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
I've defined a sample job with cron based trigger in spring:
<bean id="sampleCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="sampleJobDetail"/>
<property name="cronExpression" value="0/5 * * * * ?"/>
I see that jobs is really gets executed each 5 seconds, all good. Now I stop the program, which is a simple console application, no web containers or whatsoever, wait for ~ 30 seconds and rerun my program. What I see is that the job gets triggered a lot of times when the scheduler starts. For example, if the job is implemented as follows:
public class SampleJob implements Serializable, Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Executing the job Job " + new Date());
}
}
The output right after the restart is like this:
Executing the job Job Mon Mar 31 08:34:18 IDT 2014
Executing the job Job Mon Mar 31 08:34:18 IDT 2014
Executing the job Job Mon Mar 31 08:34:18 IDT 2014
And then it works again every 5 seconds.
Executing the job Job Mon Mar 31 08:34:20 IDT 2014
Executing the job Job Mon Mar 31 08:34:25 IDT 2014
....
In real application I'm going to implement the job that will clean up the database (of course it won't run every 5 seconds :)) But I do plan that sometimes the server will be restarted and will stay down for some time, and I would like that this job will execute only once after the restart. Is it possible to do so?
I'm using SchedulerFactoyBean with the following configurations:
<bean id="quartzSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy">
<property name="configLocation" value="classpath:scheduler-quartz.properties"/>
<property name="quartzProperties" ref="qrtzProperties"/>
<property name="autoStartup" value="true" />
<property name="triggers">
<list>
<ref bean="sampleCronTrigger"/>
</list>
</property>
</bean>
Thanks in advance
Upvotes: 3
Views: 5665
Reputation: 64099
You can also try setting the following property
<property name="overwriteExistingJobs" value="true"/>
Upvotes: 4
Reputation: 9569
Try to set misfire instruction on sampleCronTrigger:
<property name="misfireInstructionName" value="MISFIRE_INSTRUCTION_DO_NOTHING"/>
Here is a big article about misfire instructions
Upvotes: 5