Search code examples
javaquartz-scheduler

Does Quartz scheduler have inclusion calendars


Does Quartz scheduler have inclusion calendars? I see that there exclusion calendars but I do not see anything about inclusion calendars. I would like to be able to select days from calendar and run jobs on those days. There is not necessarily a pattern to the dates. How can I do this in Quartz?


Solution

  • Quartz Calendar only defines inclusions. Here is the interface:

    public interface Calendar {
    
      public boolean isTimeIncluded(long timeStamp);
    
      public long getNextIncludedTime(long timeStamp);
    
    }
    

    Some implementations are used for defining exclusions but other support inclusions. Take a look at DailyCalendar and its invertTimeRange property.

    Since you mention that you want to include only specific days on your calendar, I suspect you might have to implement your own calendar. Check out BaseCalendar which should be a great starting point.

    If you're lazy, you might be able to inherit HolidayCalendar and simply change isTimeIncluded to

    @Override
    public boolean isTimeIncluded(long timeStamp) {
        if (super.isTimeIncluded(timeStamp) == false) {
            return false;
        }
    
        Date lookFor = getStartOfDayJavaCalendar(timeStamp).getTime();
    
        return dates.contains(lookFor); // <-- HERE we remove the bang (!)
    }
    

    Defining your calendar should be pretty simple. Keep in mind that it needs to be Serializable!

    Check out the Quartz Tutorial for more info.