I have a requirement where user can set a end date or can set a specific number of occurrences before stopping any job.
As for example,
Consider I have to send sms to a specific number and that should start from now and the sms will be sent in each 5 minutes.
Now based on user's choice the above job will be stopped on a specific time or after n number of occurrences.
And I am using cron scheduler of quartz.
Now stopping it at a specific date time is easy and I have done it like following way;
trigger = TriggerBuilder.newTrigger()
.startAt(startDateObj)
.endAt(endDate)
.withIdentity(uniqueID, "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule(cronString)
)
.build();
But what to do if I have to stop it after n number of occurrences? I know it can be done with simple schedule like;
simpleSchedule().withRepeatCount(1).withIntervalInSeconds(15)
But how to do the same for cron scheduler? For some reasons I can not shift to any other type of schedule except cron.
Any help will be great for me.
Please let me know if any more data is required.
Thanks in advance.
You can count the endDate using computeEndTimeToAllowParticularNumberOfFirings() method. (That name though!).
See the following example:
CronTrigger trigger = new Trigger()
.withIentity("some_id")
.withSchedule(buildCronScheduler("some_cron_exp"))
.build();
Date endDate = TriggerUtils.computeEndTimeToAllowParticularNumberOfFirings(
(OperableTrigger) trigger,
new BaseCalendar(Calendar.getInstance().getTimeZone()),
repeatCount);
trigger = trigger.getTriggerBuilder().endAt(endDate).build();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With