问题描述
我为要每30分钟执行一次的作业设置了一个CronExpression.但是,如果先前的工作没有完成,我需要跳过特定的工作.
I have a CronExpression set for the job to be executed at every 30 minutes. But I need to skip a particular job if the earlier job is not complete.
例如我有100名员工的姓名要在数据库中更新,我将其称为"Job1",从10AM开始.现在,情况就像"Job1"正在处理中,等到我有另一个作业"Job2"对齐时,我需要更新另外50个员工的姓名.我的问题是,我需要跳过作业2"和其他作业,直到当前运行的作业完成.
For eg. I have 100 Employee whose Names to be updated in the database and I terms it as "Job1" which starts at 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. My problem is this,I need to skip "Job2" and further jobs till my currently running Job is completed.
<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>
推荐答案
一种方法是实现TriggerListener
接口,该接口提供了vetoJobExecution(Trigger trigger, JobExecutionContext context)
方法来否决下一个作业的执行.从此方法返回true
将停止执行作业.
One way is to implement TriggerListener
interface, which provides a vetoJobExecution(Trigger trigger, JobExecutionContext context)
method to veto the execution of a next job. Returning true
from this method will stop the execution of job.
接口文档: http ://quartz-scheduler.org/api/2.0.0/org/quartz/TriggerListener.html#vetoJobExecution(org.quartz.Trigger ,org.quartz.JobExecutionContext)
Interface documentation: http://quartz-scheduler.org/api/2.0.0/org/quartz/TriggerListener.html#vetoJobExecution(org.quartz.Trigger, org.quartz.JobExecutionContext)
示例:
//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();
这篇关于如何在Spring Quartz Scheduler中跳过特定的作业执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!