如何在 Spring Quartz Scheduler 中跳过特定作业执行
我有一个 CronExpression 设置为每 30 分钟执行一次作业。但如果之前的工作未完成,我需要跳过特定的工作。
例如。我有 100 名员工的姓名需要在数据库中更新,我将其称为“Job1”,从上午 10 点开始。现在的情况就像“Job1”正在处理中,当我有另一项工作“Job2”时,我需要更新另外 50 名员工的姓名。我的问题是这样的,我需要跳过“Job2”和其他作业,直到我当前运行的作业完成。
<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>
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.
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种方法是实现 TriggerListener 接口,该接口提供 vetoJobExecution(Trigger trigger, JobExecutionContext context) 方法来否决下一个作业的执行。从此方法返回
true
将停止作业的执行。接口文档: http:// quartz-scheduler.org/api/2.0.0/org/quartz/TriggerListener.html#vetoJobExecution(org.quartz.Trigger, org.quartz.JobExecutionContext)
样本:
One way is to implement
TriggerListener
interface, which provides avetoJobExecution(Trigger trigger, JobExecutionContext context)
method to veto the execution of a next job. Returningtrue
from this method will stop the execution of job.Interface documentation: http://quartz-scheduler.org/api/2.0.0/org/quartz/TriggerListener.html#vetoJobExecution(org.quartz.Trigger, org.quartz.JobExecutionContext)
Sample:
如果它们是相同的作业类:@DisallowConcurrentExecution
否则,听起来您需要使用单个线程池执行器。将相同的执行器注入到两个类中(或者实现一个协调器类来管理它)并以这种方式将工作单元添加到队列中。
If they are the same job class : @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.
如果您永远不需要并行运行多个作业,您可以将 Quartz 使用的工作线程池设置为 1。那么它一次只会运行一个作业。在您的quartz.properties 文件中,设置:
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. In your quartz.properties file, set: