Question

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

Was it helpful?

Solution 2

You can also try setting the following property

<property name="overwriteExistingJobs" value="true"/>

OTHER TIPS

Try to set misfire instruction on sampleCronTrigger:

   <property name="misfireInstructionName" value="MISFIRE_INSTRUCTION_DO_NOTHING"/>

Here is a big article about misfire instructions

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top