Ensure that Spring Quartz job execution doesn't overlap

后端 未结 7 1368
迷失自我
迷失自我 2020-11-28 23:12

I have a Java program that executes from Spring Qquartz every 20 seconds. Sometimes it takes just few seconds to execute, but as data gets bigger I\'m sure it run for 20 sec

相关标签:
7条回答
  • 2020-11-28 23:37

    Quartz 1

    If you change your class to implement StatefulJob instead of Job, Quartz will take care of this for you. From the StatefulJob javadoc:

    stateful jobs are not allowed to execute concurrently, which means new triggers that occur before the completion of the execute(xx) method will be delayed.

    StatefulJob extends Job and does not add any new methods, so all you need to do to get the behaviour you want is change this:

    public class YourJob implements org.quartz.Job {
        void execute(JobExecutionContext context) {/*implementation omitted*/}
    }
    

    To this:

    public class YourJob implements org.quartz.StatefulJob {
        void execute(JobExecutionContext context) {/*implementation omitted*/}
    }
    

    Quartz 2

    In version 2.0 of Quartz, StatefulJob is deprecated. It is now recommended to use annotations instead, e.g.

    @DisallowConcurrentExecution
    public class YourJob implements org.quartz.Job {
        void execute(JobExecutionContext context) {/*implementation omitted*/}
    }
    
    0 讨论(0)
提交回复
热议问题