Creating a Trigger which execute on particular days of week (Quartz Scheduler API)

蓝咒 提交于 2019-12-12 02:53:55

问题


I am trying to build a Trigger in Quartz Scheduler API which should get executed with following criteria.

  1. Start on particular date (Jan 25, 2012)
  2. Start at predefined time (08.00.00 AM)
  3. Every Week.
  4. Can be scheduled for alternate week or every 3 week (if not every week)
  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!