Reputation: 728
I am wondering if there is a function/method to create jobs which are triggered on random time. By that I mean that if I set a cron schedule to be triggered every monday at 10.00 am and given a time interval, lets say 30 minutes, the trigger will always go off from 9.30 ~ 10.30. For example this is the cron schedule.
schedule.setCronSchedule("0 0 10 ? * MON");
trigger = newTrigger()
.withIdentity(triggerId)
.startNow() // <~~~~~~~~~~~~~~~ ???
.withDescription(schedule.getCronSchedule())
.withSchedule(cronSchedule(schedule.getCronSchedule())).build();
If I have a variable with a specific range in minutes can I set it to trigger randomly? And by that I mean not just take the cron schedule string and remodify it, but using a method to trigger the event every time, based on the random range so first monday may be triggered at 10.01 second monday may be triggered at 9.46 and etc.
Thanks in advance.
Upvotes: 2
Views: 2071
Reputation: 8668
To fire a schedule at some time between 1.00am and 1.30am every day, you could try this:
schedule.setCronSchedule(String.format("0 %d 1am * * ?", random.nextInt(30)));
Unfortunately there isn't anything built into Quartz, or even unix cron for that matter. And the randomness is going to be the same every day from here on, unless you regularly reset the schedule. But perhaps that isn't a problem for your situation.
Upvotes: 5