Ashis Jena
Ashis Jena

Reputation: 450

Quartz scheduler with excluding specific time range

I want to schedule through Quartz scheduler excluding specific time ranges.

Ex : 2am - 3am and 5:30pm - 5:45pm

Is there any other way to achieve this without the use cron expression and cron scheduler?

Upvotes: 0

Views: 1899

Answers (1)

Jan Moravec
Jan Moravec

Reputation: 1880

This is what Quartz Calendars are used for. Since in your case you want to exclude specific daytime ranges, you will want to use the DailyCalendar.

A single DailyCalendar can exclude a single day time range. Since you want to exclude multiple (two) ranges, you will need to combine two DailyCalendars like so:

// calendar that excludes the 2am-3am day time range
DailyCalendar dc1 = new DailyCalendar(2,0,0,0,3,0,0,0);

// calendar that excludes the 5:30pm-5:45pm day time range
DailyCalendar dc2 = new DailyCalendar(17,30,0,0,17,45,0,0);

// combine the two calendars so that both ranges are excluded by dc2
dc2.setBaseCalendar(dc1);

// register the calendar with the scheduler
scheduler.addCalendar("MyExcludedDayTimeRangesCalendar", dc2, true, true);

MutableTrigger trigger = ... create SimpleTrigger / CronTrigger  / DailyTimeInterval / CalendarIntervalTrigger instance

// associate the created trigger with the registered calendar - the trigger will exclude calendar's time ranges  
trigger.setCalendarName("MyExcludedDayTimeRangesCalendar");

...

Upvotes: 1

Related Questions