The Quartz API
Quartz API 中主要的接口与类:
- IScheduler - the main API for interacting with the scheduler.
- IJob - an interface to be implemented by components that you wish to have executed by the scheduler.
- IJobDetail - used to define instances of Jobs.
- ITrigger - a component that defines the schedule upon which a given Job will be executed.
- JobBuilder - used to define/build JobDetail instances, which define instances of Jobs.
- TriggerBuilder - used to define/build Trigger instances.
Scheduler的生命周期以SchedulerFactory的Create()和Shutdown()为界。一旦创建IScheduler接口,他就可以被用来add,remove,list jobs和Triggers,和执行其他与调度相关的操作(比如暂停一个trigger)。但是,调度器实际上不会再任何触发器上执行直到启动Start()方法。
1 // define the job and tie it to our HelloJob class
2 IJobDetail job = JobBuilder.Create<HelloJob>()
3 .WithIdentity("myJob", "group1") // name "myJob", group "group1"
4 .Build();
5
6 // Trigger the job to run now, and then every 40 seconds
7 ITrigger trigger = TriggerBuilder.Create()
8 .WithIdentity("myTrigger", "group1")
9 .StartNow()
10 .WithSimpleSchedule(x => x
11 .WithIntervalInSeconds(40)
12 .RepeatForever())
13 .Build();
14
15 // Tell quartz to schedule the job using our trigger
16 sched.scheduleJob(job, trigger);
调度器执行有以下方式:
- WithCalendarIntervalSchedule
- WithCronSchedule
- WithDailyTimeIntervalSchedule
- WithSimpleSchedule
Jobs and Triggers
Job是实现IJob接口的类。
IJob Interface
1 namespace Quartz
2 {
3 public interface IJob
4 {
5 void Execute(JobExecutionContext context);
6 }
7 }
JobExecutionContext:
JobDetail:
Trigger:触发器。用于触发jobs的执行。当调度一个job时,实例化一个trigger并且‘调整’它的属性供给调度。Trigger还有JobDataMap与它们关联--这对于将参数传递给Job非常有用。Quartz附带不同的触发器类型,最经常被用到的是SimpleTrigger (interface ISimpleTrigger) and CronTrigger (interface ICronTrigger).
SimpleTrigger: SimpleTrigger是非常方便的如果你需要 '一次性' 的执行(在给定的时刻单一的执行一个job),或者在给定的时间出发一个job。
CronTrigger :以日历为基础触发。