Quartz Scheduler(2.2.1) - Usage of Calendars

Quartz Calendar objects (not java.util.Calendar objects) can be associated with triggers at the time the trigger is defined and stored in the scheduler.

Calendars are useful for excluding blocks of time from the trigger‘s firing schedule. For instance, you could create a trigger that fires a job every weekday at 9:30 am, but then add a Calendar that excludes all of the business‘s holidays.

Calendar‘s can be any serializable object that implements the Calendar interface {shown below).

package org.quartz;
public interface Calendar {
    public boolean isTimeIncluded(long timeStamp);
    public long getNextIncludedTime(long timeStamp);
}

Notice that the parameters to these methods are of the long type. These are timestamps in millisecond format. This means that calendars can ‘block out‘ sections of time as narrow as a millisecond. Most likely, you‘ll be interested in ‘blocking-out‘ entire days. As a convenience, Quartz includes the class org.quartz.impl.HolidayCalendar, which does just that.

Calendars must be instantiated and registered with the scheduler via the addCalendar(..) method. If you use HolidayCalendar, after instantiating it, you should use its addExcludedDate(Date date) method in order to populate it with the days you wish to have excluded from scheduling. The same calendar instance can be used with multiple triggers as shown in the example below:

HolidayCalendar cal = new HolidayCalendar();
cal.addExcludedDate( someDate );
cal.addExcludedDate( someOtherDate );
sched.addCalendar("myHolidays", cal, false);
Trigger t = newTrigger()
    .withIdentity("myTrigger")
    .forJob("myJob")
    .withSchedule(dailyAtHourAndMinute(9, 30)) // execute job daily at 9:30
    .modifiedByCalendar("myHolidays") // but not on holidays
    .build();
// .. schedule job with trigger
Trigger t2 = newTrigger()
    .withIdentity("myTrigger2")
    .forJob("myJob2")
    .withSchedule(dailyAtHourAndMinute(11, 30)) // execute job daily at 11:30
    .modifiedByCalendar("myHolidays") // but not on holidays
    .build();
// .. schedule job with trigger2

The code above creates two triggers, each scheduled to fire daily. However, any of the firings that would have occurred during the period excluded by the calendar will be skipped.

The org.quartz.impl.calendar package provides a number of Calendar implementations that may suit your needs.

时间: 2024-12-23 04:30:16

Quartz Scheduler(2.2.1) - Usage of Calendars的相关文章

Quartz Scheduler(2.2.1) - Usage of SimpleTrigger

SimpleTrigger should meet your scheduling needs if you need to have a job execute exactly once at a specific moment in time, or at a specific moment in time followed by repeats at a specific interval. For example, if you want the trigger to fire at e

Quartz Scheduler(2.2.1) - Usage of CronTriggers

Cron is a UNIX tool that has been around for a long time, so its scheduling capabilities are powerful and proven. The CronTrigger class is based on the scheduling capabilities of cron. CronTrigger uses “cron expressions”, which are able to create fir

Table of Contents - Quartz Scheduler

Getting Started Hello World Integration with Spring Quartz Scheduler Developer Guide Usage of JobDataMap Usage of Calendars Usage of SimpleTrigger Usage of CronTriggers Working with TriggerListeners and JobListeners Working with SchedulerListeners Wo

Quartz Scheduler(2.2.1) - Working with JobStores

About Job Stores JobStores are responsible for keeping track of all the work data you give to the scheduler: jobs, triggers, calendars, and so forth. Selecting the appropriate JobStore for your Quartz scheduler instance is an important step. The choi

Quartz Scheduler(2.2.1) - hello world

简单示例 1. maven 依赖 <dependencies> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.quartz-

Spring Boot集成Spring Scheduler和Quartz Scheduler

本文介绍了Spring Boot集成Spring Scheduler和Quartz Scheduler的基础知识,利用ShedLock解决Spring Scheduler多实例运行冲突,介绍了Quartz ScheduleBuilder.Calendar,介绍了动态创建Quartz Job的方法. GitHub源码 Spring Scheduler Spring Framework提供了简单.易用的Job调度框架Spring Scheduler. 示例 在Spring Boot中,只需两步即可启

Quartz Enterprise Job Scheduler 1.x Tutorial---转载

Lesson 10: Configuration, Resource Usage and SchedulerFactory http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/TutorialLesson10 The architecture of Quartz is modular, and therefore to get it running several components need to be "sn

Quartz使用(2) - Quartz核心接口Scheduler、Job

quartz的核心接口如下: 接口 含义 Scheduler scheduler的主要API接口 Job 任务实现接口,期望调度器能够执行 JobDetail 用于定义Job实例 Trigger 调度器基于特定时间来执行指定任务的组件 JobBuilder 用于定义.创建JobDetail实例 TriggerBuilder 用于定义.创建Trigger实例 1. Scheduler 一个调度器的生命周期为通过SchedulerFactory创建,直到执行其shutdown()方法.当Schedu

Spring 3 + Quartz 1.8.6 Scheduler Example--reference

In this tutorial, we will show you how to integrate Spring with Quartz scheduler framework. Spring comes with many handy classes to support Quartz, and decouple your class to Quartz APIs. Tools Used : Spring 3.1.2.RELEASE Quartz 1.8.6 Eclipse 4.2 Mav