我正在尝试在Quartz Scheduler API中构建一个Trigger,它应该按照以下标准执行。
我创建了以下表达式
newTrigger().withIdentity(cronTriggerDTO.getTiggerId(), "simpleGroup")
.startAt(getTriggerExecutionDate(cronTriggerDTO))
.withSchedule(calendarIntervalSchedule().withIntervalInWeeks
(cronTriggerDTO.getWeeklyInterval())).build();
但我感到困惑的是我如何在周的特定日子添加条件来执行此触发器
这方面的任何帮助都会很棒。
提前致谢
答案 0 :(得分:1)
我会使用CronScheduleBuilder.cronSchedule(String cronExpression)
,如下所示:
newTrigger().withIdentity(cronTriggerDTO.getTiggerId(), "simpleGroup")
.startAt(getTriggerExecutionDate(cronTriggerDTO))
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 * * 1,2,5"))
.build();
答案 1 :(得分:1)
使用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 :(得分:0)
使用cron触发器,下面是准备cron表达式的简单方法
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();
然后安排工作..希望这可以帮助你。