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

前端 未结 2 1952
孤街浪徒
孤街浪徒 2021-01-06 07:50

I am using the Quartz Scheduler (version 1.8.3 due to project constraints) and I as assigned the task of creating an \"MS Outlook-like\" scheduler for Jobs specific to my pr

2条回答
  •  不知归路
    2021-01-06 08:11

    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.

提交回复
热议问题