Quartz CRON 只触发作业一次

发布于 2024-11-29 23:39:37 字数 1674 浏览 0 评论 0原文

我有一项春季批量工作,需要安排在一天中的特定时间。我设置了一个 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  -->

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  -->

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欲拥i 2024-12-06 23:39:37

我怀疑 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);

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);
活雷疯 2024-12-06 23:39:37

我现在不使用 spring,但我使用 Quartz。我认为问题是,你给出了两个 cron 表达式。你必须给出一个 cron 表达式,如 0 0 3-20 * * ? (类似的事情),crontrigger 只能有一个 cron 表达式重写在 3 和 20 触发的 cron 表达式。

I dont now spring but i use Quartz .I think problem is ,you are giving two cron expression.You must give a single cron expression like 0 0 3-20 * * ? (some thing like that ) ,crontrigger can have only one cron expression rewrite cron expression that fire at 3 and 20 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文