Ensure that Spring Quartz job execution doesn't overlap

后端 未结 7 1367
迷失自我
迷失自我 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:12

    if you use spring quartz, i think you have to configure like this

        <bean id="batchConsumerJob"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="myScheduler" />
            <property name="targetMethod" value="execute" />
            <property name="concurrent" value="false" />
        </bean>
    
    0 讨论(0)
  • 2020-11-28 23:15

    put them in a queue

    Even if the time exceeds 20 second current job should be finished & then the next should be fetched from the queue.

    Or you can also increase time to some reasonable amount.

    0 讨论(0)
  • 2020-11-28 23:22

    You can use a semaphore. When the semaphore is taken, abandon the 2nd job and wait until the next fire time.

    0 讨论(0)
  • 2020-11-28 23:32

    Just in case anyone references this question, StatefulJob has been deprecated. They now suggest you use annotations instead...

    @PersistJobDataAfterExecution
    @DisallowConcurrentExecution
    public class TestJob implements Job {
    

    This will explain what those annotations mean...

    The annotations cause behavior just as their names describe - multiple instances of the job will not be allowed to run concurrently (consider a case where a job has code in its execute() method that takes 34 seconds to run, but it is scheduled with a trigger that repeats every 30 seconds), and will have its JobDataMap contents re-persisted in the scheduler's JobStore after each execution. For the purposes of this example, only @PersistJobDataAfterExecution annotation is truly relevant, but it's always wise to use the @DisallowConcurrentExecution annotation with it, to prevent race-conditions on saved data.

    0 讨论(0)
  • 2020-11-28 23:34

    I'm not sure you want synchronisation, since the second task will block until the first finishes, and you'll end up with a backlog. You could put the jobs in a queue, but from your description it sounds like the queue may grow indefinitely.

    I would investigate ReadWriteLocks, and let your task set a lock whilst it is running. Future tasks can inspect this lock, and exit immediately if an old task is still running. I've found from experience that that's the most reliable way to approach this.

    Perhaps generate a warning as well so you know you're encountering problems and increase the time interval accordingly ?

    0 讨论(0)
  • 2020-11-28 23:35

    If all you need to do is fire every 20 seconds, Quartz is serious overkill. The java.util.concurrent.ScheduledExecutorService should be perfectly sufficient for that job.

    The ScheduledExecutorService also provides two semantics for scheduling. "fixed rate" will attempt to run your job every 20 seconds regardless of overlap, whereas "fixed delay" will attempt to leave 20 seconds between the end of the first job and the start of the next. If you want to avoid overlap, then fixed-delay is safest.

    0 讨论(0)
提交回复
热议问题