Quartz CRON触发器仅运行一次任务

3
我有一个需要在一天中特定时间段内安排的Spring Batch工作。我设置了Quartz CRON调度程序来完成此任务。然而,我发现该工作只被触发了一次。
可能出了什么问题?
以下是XML文件片段 -
<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  -->

4
你尝试过使用每分钟触发的cron表达式进行测试吗?这样你就不必等待24小时才能看到它是否有效。 - Alexander Pogrebnyak
是的。实际上为了测试,我已将其配置为0/5分钟。但是问题依旧。它只运行一次。 - Ashish K Agarwal
2个回答

0

我怀疑cron表达式是正确的,但是Spring Batch作业只会执行一次,除非它的参数发生变化。

如果您获得类似于以下堆栈跟踪,则可以验证此操作:

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)

为确保每个作业调用的参数不同,您可以修改 UpdateDataFeedJob 类以这种方式调用作业:
JobParametersBuilder builder = new JobParametersBuilder();
builder.addLong("run.ts", System.currentTimeMillis());
JobParameters jobParameters = builder.toJobParameters();

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

0

我不熟悉Spring,但我使用Quartz。我认为问题在于,您提供了两个cron表达式。您必须提供一个单独的cron表达式,例如0 0 3-20 * * ?(类似于此),因为crontrigger只能有一个cron表达式,重写cron表达式以在3点和20点触发。


另一个 cron 表达式已被注释。 - Ashish K Agarwal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接