Java – Quartz CRON triggers job only once

cronexpressionjavaquartz-schedulerspring-batch

i have a spring batch job which needs to be scheduled at specific hour of the day. I have setup a Quartz CRON scheduler to accomplish this. However i see that the job is getting triggered only once.

What could be wrong ?

following is the XML file snippet –

<batch:job id="getFleetUpdatesJob" job-repository="jobRepository">
       <batch:step id="step0">
                <batch:tasklet ref="fleetUpdatesID" transaction-manager="jobRepository-transactionManager" />
       </batch:step>        
       <!-- <batch:step id="step1" next="step2"> 
                <batch:tasklet ref="world" transaction-manager="jobRepository-transactionManager" />
       </batch:step> -->       
    </batch:job>    

    <!--  Quartz related beans START  -->

    <bean name="updateDataFeedJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
      <property name="jobClass" value="<package>.schedule.UpdateDataFeedJob" />  
    </bean> 

    <bean id="cronTriggerId" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="updateDataFeedJobDetail" />
        <!-- run every morning at 3AM -->
        <!--  <property name="cronExpression" value="0 0 3 * * ?" /> -->

        <!-- run the job at 8pm everyday -->
        <property name="cronExpression" value="0 0 20 * * ?" />
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTriggerId" />
            </list>
        </property>
    </bean>
    <!--  Quartz related beans END  -->

Best Answer

I suspect the cron expression is working correctly, however a Spring Batch job will only execute once unless it's parameters change.

You can verify this if you get a stack trace similar to:

2011-08-18 00:40:26,155 INFO [org.springframework.batch.core.launch.support.SimpleJobLauncher] - <Job: [FlowJob: [name=job1]] completed with the following parameters: [{run.id=1}] and the following status: [COMPLETED]>
2011-08-18 00:40:30,002 INFO [com.beny23.test.JobLauncherDetails] - <Quartz trigger firing with Spring Batch jobName=job1>
2011-08-18 00:40:30,015 ERROR [com.beny23.test.JobLauncherDetails] - <Could not execute job.>
org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={run.id=1}.  If you want to run this job again, change the parameters.
    at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:122)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)

In order to ensure that the parameters for each job invokation are different, you could modify the UpdateDataFeedJob class to call your job like this:

JobParametersBuilder builder = new JobParametersBuilder();
builder.addLong("run.ts", System.currentTimeMillis());
JobParameters jobParameters = builder.toJobParameters();

Job job = jobLocator.getJob(jobName);
jobLauncher.run(job, jobParameters);
Related Topic