问题
I am trying to build a Trigger in Quartz Scheduler API which should get executed with following criteria.
- Start on particular date (Jan 25, 2012)
- Start at predefined time (08.00.00 AM)
- Every Week.
- Can be scheduled for alternate week or every 3 week (if not every week)
- On these particular days of week (Monday,Tuesday,Friday etc)
I have created the following expression
newTrigger().withIdentity(cronTriggerDTO.getTiggerId(), "simpleGroup")
.startAt(getTriggerExecutionDate(cronTriggerDTO))
.withSchedule(calendarIntervalSchedule().withIntervalInWeeks
(cronTriggerDTO.getWeeklyInterval())).build();
but i am confused how i shd add the condition to execute this trigger on particular days of Week
Any help in this regard will be great.
Thanks in advance
回答1:
Use DailyTimeIntervalScheduleBuilder
Set daysOfWeek = new HashSet();
daysOfWeek.add(1);
daysOfWeek.add(2);
daysOfWeek.add(5);
newTrigger().withIdentity(cronTriggerDTO.getTiggerId(), "simpleGroup")
.startAt(getTriggerExecutionDate(cronTriggerDTO))
.withSchedule(dailyTimeIntervalSchedule()
.onDaysOfTheWeek(daysOfWeek)
.startingDailyAt(new TimeOfDay(8,0)))
.build();
回答2:
I'd use CronScheduleBuilder.cronSchedule(String cronExpression)
, like this:
newTrigger().withIdentity(cronTriggerDTO.getTiggerId(), "simpleGroup")
.startAt(getTriggerExecutionDate(cronTriggerDTO))
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 * * 1,2,5"))
.build();
回答3:
Use cron trigger and below is the simple way to prepare cron expression
int second = 53;//prepare from the time selected from UI(fire time)
int minute=0;
int hour=8;
String dayOfWeek="1,3";//prepare it from the days you get from UI(give check box values as 1 for SUN,....)
String cronExpression = String.format("%d %d %d ? * %s",second,minute , hour, dayOfWeek);
newTrigger()
.withIdentity(cronTriggerDTO.getTiggerId(), "simpleGroup")//
.withSchedule(cronSchedule(cronExpression)//
.startAt(getTriggerExecutionDate(cronTriggerDTO))
.build();
Then schedule the job..,hope this helps you.
来源:https://stackoverflow.com/questions/9000054/creating-a-trigger-which-execute-on-particular-days-of-week-quartz-scheduler-ap