英文:
Quartz Scheduler next execution time equal current time plus interval of scheduler
问题
我正在使用Quartz调度器,根据要求一切都运行得很完美。但有一件事我想要实现,那就是我希望我的作业的下一次执行将在**(currentFinishTime + intervalOfScheduler)**后触发。
具有30秒间隔的作业执行示例:
> Job-1-First-Executed 在 10-10-2020 18:30:05
> Job-1-Second-Executed 在 10-10-2020 18:30:35
> Job-1-Third-Executed 在 10-10-2020 18:31:05
所以,如果作业需要20秒来执行,那么下一次触发将在05+20+30 = 55时发生。不是在10-10-2020 18:30:35,而是在10-10-2020 18:30:55触发,其他执行也是如此...
注意:@DisallowConcurrentExecution
和MyJobExecutor implements Job {public void execute(JobExecutionContext context){...}}
已经实现。
请帮助我解决我的问题。
英文:
I am working with Quartz scheduler and everything work perfect according to the requirement. But there one thing that I want to implement and i.e. I want my next execution of job will trigger on (currentFinishTime + intervalOfScheduler)
Example of job execution with 30 seconds of interval:
> Job-1-First-Executed at 10-10-2020 18:30:05
> Job-1-Second-Executed at 10-10-2020 18:30:35
> Job-1-Third-Executed at 10-10-2020 18:31:05
So, here if the job takes 20 seconds to execute then next trigger will happen on 05+20+30 = 55. Instead of 10-10-2020 18:30:35, it will trigger at 10-10-2020 18:30:55 and same for other execution and so on...
Note: @DisallowConcurrentExecution
and MyJobExecutor implements Job {public void execute(JobExecutionContext context){...}}
are already implemented.
Please help me to solve my problem.
答案1
得分: 0
经过大量的研究,我已经实现了满足我的需求的解决方案。下面是代码部分:
if (是否包含执行时间) {
final TriggerBuilder triggerBuilder = context.getTrigger().getTriggerBuilder();
final Trigger newTrigger = triggerBuilder
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(cpoPullJobData.getInterval())
.repeatForever())
.startAt(futureDate(interval/*例如30*/, DateBuilder.IntervalUnit.SECOND);)
.build();
context.getScheduler().rescheduleJob(context.getTrigger().getKey(), newTrigger);
}
英文:
After a lots of research, I have implemented my own solution which suffice my requirement. The code is mentioned below:
if(isExecutionTimeIncluded) {
final TriggerBuilder triggerBuilder = context.getTrigger().getTriggerBuilder();
final Trigger newTrigger = triggerBuilder
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(cpoPullJobData.getInterval())
.repeatForever())
.startAt(futureDate(interval/*eg.30*/, DateBuilder.IntervalUnit.SECOND);)
.build();
context.getScheduler().rescheduleJob(context.getTrigger().getKey(), newTrigger);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论