Reputation: 1272
I want to make my cron execute job every 2nd day. So if i execute job at 25th May, it will run at 25th, 27th, 29th, 31th, 2nd June, 4th June ..
but the issue is, after the end of 31th May, the cron will reset and start running at 1st June, 3rd June, 5th June ... instead of 2nd June, 4th June ..
below is my code..
CronTrigger cronTrigger = newTrigger()
.withIdentity("trigger1", "testJob")
.startAt(startDate)
.withSchedule(CronScheduleBuilder.cronSchedule( * * * 1/2 * ?")
.withMisfireHandlingInstructionDoNothing())
.build();
Upvotes: 3
Views: 3767
Reputation: 1108
CronTrigger cronTrigger = newTrigger()
.withIdentity("trigger1", "testJob")
.startAt(startDate)
.withSchedule(
CronScheduleBuilder.cronSchedule(" * * * */2 * ?")
.withMisfireHandlingInstructionDoNothing()
).build();
So you need to replace 1/2
with */2
. Here *
means each day and */2
mean every second day.
CronTrigger
can not be use to run every 2 days interval as it will always start at 1st day of every month even though last jab execution was on 31, it will run at 1st day of month instead if 2nd day of month. You can set it in to run after 48 hours in place of every 2 days.
Please look if this will help you:
trigger = newTrigger()
.withIdentity("trigger3", "group1")
.startAt(tomorrowAt(15, 0, 0) // first fire time 15:00:00 tomorrow
.withSchedule(
simpleSchedule()
.withIntervalInHours(2 * 24) // interval is actually set at 48 hours' worth of milliseconds
.repeatForever()
).build();
for more information please refer This link
Upvotes: 7
Reputation: 1272
From quartz cron site, i just found out i cannot use cronTrigger to setup every 2nd day job execution.
"At first glance, you may be tempted to use a CronTrigger. However, if this is truly to be every two days, CronTrigger won't work. To illustrate this, simply think of how many days are in a typical month (28-31). A cron expression like "0 0 5 2/2 * ?" would give us a trigger that would restart its count at the beginning of every month. This means that we would would get subsequent firings on July 30 and August 2, which is an interval of three days, not two."
Upvotes: 2