Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

1. Two Calendar classes

The java.util package has a Calendar Calendar class. When using the Quartz framework, using the Calendar class incorrectly references the Calendar in the org. Quartz package in the Quertz framework, resulting in some errors.

Let’s take a look at what Calendar is used for in the Quertz framework.

2. Organization in Quartz

Calendar, as a top-level interface, has the following implementation structure:

Although there are many implementation classes in the structure diagram, AnnualCalendar and other classes inherit BaseCalendar class and also implement the Calendar interface, which will be repeatedly displayed in the structure. The structure without the repetition can be expressed as:

  • Calender, interface
    • BaseCalendar, class that implements the Calender interface
      • AnnualCalendar class, inheriting from BaseCalendar and implementing Calender
      • MonthlyCalendar class, inherit BaseCalendar, at the same time to implement Calender
      • WeeklyCalendar class, inheriting BaseCalendar and implementing Calender
      • DailyCalendar class, inherited from BaseCalendar
      • HolidayCalendar class, inherited from BaseCalendar
      • CronCalendar class, inherited from BaseCalendar

3. Implement functions

Once you know Calender’s structure information, you can take a look at the implementation classes one by one.

  • AnnualCalendar: AnnualCalendar class. You can set a set of dates in a year. After taking effect, these date and time triggers do not take effect
// National Day holiday, no trigger
AnnualCalendar annualCalendar = new AnnualCalendar();
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.MONTH,10);
calendar.add(Calendar.DATE,1);
annualCalendar.addExcludedDate(calendar)
Copy the code
  • BaseCalendar: Calendar base class, with some additional methods that can be implemented if needed
  • CronCalendar: Use cron expressions to set when triggers don’t fire (segment)
//cron expression, such as stop firing at 22:00-23:00 PM
CronCalendar calendar = new CronCalendar("* * 22-23? * *");
Copy the code
  • DailyCalendar: DailyCalendar class. Sets whether the trigger is triggered during a certain period of time in a day. SetInvertTimeRange (false) : no trigger
// 11:45-13:30 noon nap, no trigger
DailyCalendar dailyCalendar = new DailyCalendar("11:45:00"."13:30:00");
dailyCalendar.setInvertTimeRange(false);
Copy the code
  • HolidayCalendar: HolidayCalendar class that can set multiple copies of the date trigger to expire, granularity in the day
HolidayCalendar = new HolidayCalendar(); HolidayCalendar = new HolidayCalendar(); Calendar calendar = new GregorianCalendar(2021, 24, 10); holidayCalendar.addExcludedDate(calendar);Copy the code
  • MonthlyCalendar: a MonthlyCalendar class that sets which dates of the month do not trigger
// Set the 1st and 15th of each month not to trigger
MonthlyCalendar monthlyCalendar = new MonthlyCalendar();
monthlyCalendar.setDayExcluded(1.true);
monthlyCalendar.setDayExcluded(15.true)
Copy the code
  • WeeklyCalendar: Week calendar class that sets the days of the week not to trigger
// Set Saturdays and Sundays as two days off
WeeklyCalendar weeklyCalendar = new WeeklyCalendar();
weeklyCalendar.setDayExcluded(Calendar.SATERDAY, true);
weeklyCalendar.setDayExcluded(Calendar.SUNDAY, true);
Copy the code
  • Multiple calendar objects are used in combination
// Set Friday night from 20:00 to 21:00 not to trigger
DailyCalendar dailyCalendar = new DailyCalendar("20:00:00"."21:00:00");
dailyCalendar.setInvertTimeRange(false);  
WeeklyCalendar weeklyCalendar = new WeeklyCalendar(dailyCalendar);
weeklyCalendar.setDayExcluded(Calendar.FRIDAY, true);
Copy the code

4. Configuration process

  1. Select Create calendar object
  2. Example Set a calendar time range policy
  3. Configure the calendar object into the trigger
  4. Registers the calendar object with the scheduler
  5. The scheduler performs triggers and tasks

Sample code:

//①-② Select an appropriate calendar object to create and set the policy not to trigger
DailyCalendar dailyCalendar = new DailyCalendar("11:45:00"."13:30:00");
dailyCalendar.setInvertTimeRange(false);

ModifiedByCalendar ("dailyCalendar")
SimpleTrigger trigger = newTrigger() 
.withIdentity("trigger1"."group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(3)
.repeatForever())
.modifiedByCalendar("dailyCalendar")
.build();

//④-⑤ Register the calendar with the scheduler and execute the scheduler timer trigger task
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
sched.addCalendar("dailyCalendar", dailyCalendar, false.false);
scheduler.scheduleJob(job, trigger);
Copy the code

5. To summarize

The above is the function and use method of Calendar in Quartz framework. Although Quartz provides us with a complete set of timing policies and can exclude dates and times according to holidays and other holidays, most of the time policies can be completed by using CRon expression in practice. When creating a trigger, you can use a trigger object whose type is CronTrigger to quickly complete a scheduled task with a customized time.