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

前端 未结 2 1953
孤街浪徒
孤街浪徒 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:10

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题