简体   繁体   English

如何在Spring Quartz Scheduler中跳过特定的作业执行

[英]How to skip a particular job execution in Spring Quartz Scheduler

I have a CronExpression set for the job to be executed at every 30 minutes. 我为要每30分钟执行一次的作业设置了CronExpression。 But I need to skip a particular job if the earlier job is not complete. 但是,如果先前的工作没有完成,我需要跳过特定的工作。

For eg. 例如。 I have 100 Employee whose Names to be updated in the database and I terms it as "Job1" which starts at 10AM. 我有100名员工的姓名要在数据库中更新,我将其称为“ Job1”,从10AM开始。 Now the case is like "Job1" is in process and by the time I have another job- "Job2" aligned where I need to update another 50 Employee's names. 现在,情况就像“ Job1”正在处理中,到我有另一个作业“ Job2”对齐时,我需要更新另外50个员工的姓名。 My problem is this,I need to skip "Job2" and further jobs till my currently running Job is completed. 我的问题是,我需要跳过“作业2”和其他作业,直到当前运行的作业完成。

    <bean name="employeeNameUpdateJob" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="name" value="Employee Update Job"/>
        <property name="group" value="Employee Update Group Job"/>
        <property name="jobClass"
            value="com.emp.scheduler.EmployeeUpdateScheduler" />
        <property name="volatility" value="false" />
    </bean>

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="name" value="Employee Update Trigger"/>
        <property name="group" value="Employee Update Group Trigger"/>
        <property name="volatility" value="false" />
        <property name="jobDetail" ref="employeeNameUpdateJob"/>
        <property name="cronExpression" value="0 0/30 * * * ?"/>
    </bean>

One way is to implement TriggerListener interface, which provides a vetoJobExecution(Trigger trigger, JobExecutionContext context) method to veto the execution of a next job. 一种方法是实现TriggerListener接口,该接口提供vetoJobExecution(Trigger trigger, JobExecutionContext context)方法来否决下一个作业的执行。 Returning true from this method will stop the execution of job. 从此方法返回true将停止执行作业。

Interface documentation: http://quartz-scheduler.org/api/2.0.0/org/quartz/TriggerListener.html#vetoJobExecution(org.quartz.Trigger , org.quartz.JobExecutionContext) 界面文档: http : //quartz-scheduler.org/api/2.0.0/org/quartz/TriggerListener.html#vetoJobExecution (org.quartz.Trigger,org.quartz.JobExecutionContext)

Sample: 样品:

//SampleTriggerListener.java
public class SampleTriggerListener implements TriggerListener {

    @Override
    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext ctx) {
        if(!previousJobCompleted)
            return true;

        return false;
    }
}


//Main.java
//init jobs, trigger & scheduler  
this.scheduler.addTriggerListener(new SampleTriggerListener());
this.scheduler.start();

If they are the same job class : @DisallowConcurrentExecution 如果它们是相同的工作类别:@DisallowConcurrentExecution

Else it sounds like you need to use a single threadpool executor. 否则,听起来您需要使用单个线程池执行程序。 Inject the same executor to both classes ( or alternatively implement an orchestrator class to manage this ) and add the work units to the queue this way. 将相同的执行程序注入到两个类中(或者替代地,实现一个Orchestrator类来管理它),并以此方式将工作单元添加到队列中。

If you will never need to run multiple jobs in parallel you could set the worker thread pool that Quartz uses to 1. Then it will only ever run one job at a time. 如果您永远不需要并行运行多个作业,则可以将Quartz使用的工作线程池设置为1。这样它一次只能运行一个作业。 In your quartz.properties file, set: 在您的quartz.properties文件中,设置:

org.quartz.threadPool.threadCount: 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM