How to simulate 'No. of occurrences' or 'Repeat Count' with CronTriggers in Java?

主宰稳场 提交于 2019-11-30 22:25:23

It seems that Quartz have implemented something that can help: TriggerUtils.computeEndTimeToAllowParticularNumberOfFirings.

I haven't tested it yet, but this is the code I have wrote for now:

CronTrigger trigger = newTrigger()
    .withSchedule(cronSchedule(cronExpression))
    .build();
Date endDate = TriggerUtils.computeEndTimeToAllowParticularNumberOfFirings((OperableTrigger) trigger,
              new BaseCalendar(Calendar.getInstance().getTimeZone()), 10);
trigger = trigger.getTriggerBuilder().endAt(endDate).build();

If this won't work, then as said here and here, you can't set a repeat count, and you should use TriggerListener.

In any case, version 2.2 doesn't have this feature.

Update

I've tested it, and it works.

Wy dont't you instead use the Simple Trigger? You would have the additional taks of calculating the time interval at the time of scheduling the job, but that will be a one time activity.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!