Lesson 4: More About Triggers

http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-04.html

Like jobs, triggers are quite easy to work with, but do contain a variety of customizable options that you need to be aware of and understand before you can make full use of Quartz. Also, as noted earlier, there are different types of triggers that you can select from to meet different scheduling needs.

You will learn about the two most common types of triggers in Lesson 5: Simple Triggers and Lesson 6: Cron Triggers.

如同作业一样,使用触发器也是很容易的一件事。但是触发器也包含一些需要客户端定制的选项,在充分使用Quartz之前这些细节你必须充分注意并理解他们。同样的,在之前,我们曾提到过,触发器有很多不同的类型,在设计不同类型的调度器的时候,你可以选择最合适的。

Common Trigger Attributes

触发器共同的特性

Aside from the fact that all trigger types have TriggerKey properties for tracking their identities, there are a number of other properties that are common to all trigger types. These common properties are set using the TriggerBuilder when you are building the trigger definition (examples of that will follow).

所有类型的触发器都有触发器的键属性,用来追踪他们的身份标识特征。除此之外,他们还有一些其他的相同的属性。这些共同的属性被设置成使用TriggerBuilder(触发器创建者)根据你设定的触发器定义进行创建。

Here is a listing of properties common to all trigger types:

下面是一个触发器共有属性的列表

  • The “jobKey” property indicates the identity of the job that should be executed when the trigger fires.
  • The “startTime” property indicates when the trigger’s schedule first comes into affect. The value is a java.util.Date object that defines a moment in time on a given calendar date. For some trigger types, the trigger will actually fire at the start time, for others it simply marks the time that the schedule should start being followed. This means you can store a trigger with a schedule such as “every 5th day of the month” during January, and if the startTime property is set to April 1st, it will be a few months before the first firing.
  • The “endTime” property indicates when the trigger’s schedule should no longer be in effect. In other words, a trigger with a schedule of “every 5th day of the month” and with an end time of July 1st will fire for it’s last time on June 5th.
  • JobKey(作业键)属性用来说明当触发器触发执行的时候,作业包含的身份标识信息。
  • StartTime属性用来设定触发器的周期什么时候开始生效。其值是一个Date类型的对象,用来定义一个给定日期中的具体的时刻。对于某一些触发器而言,他们会在一开始就触发执行,而对于另一些而言,只不过是标注了调度任务开始的时间而已。这意味着你可以用调度任务来存储一个触发器,比如在一月份的“每月的第五天”,而且当StartTime属性被设置在了四月一号,这个触发器实际上在几个月之后才会开始第一次执行。
  • EndTime属性用来设置触发器调度任务的什么时候结束。换句话说,一个触发器有一个调度任务“每月的第五天”,然后其结束时间设置在七月一号,则该触发器最后一次执行会在六月五号。

Other properties, which take a bit more explanation are discussed in the following sub-sections.

另一些属性,我们会话一些笔墨在下面的部分中进行更为详细的阐述。

Priority

Sometimes, when you have many Triggers (or few worker threads in your Quartz thread pool), Quartz may not have enough resources to immediately fire all of the Triggers that are scheduled to fire at the same time. In this case, you may want to control which of your Triggers get first crack at the available Quartz worker threads. For this purpose, you can set the priority property on a Trigger. If N Triggers are to fire at the same time, but there are only Z worker threads currently available, then the first Z Triggers with the highest priority will be executed first. If you do not set a priority on a Trigger, then it will use the default priority of 5. Any integer value is allowed for priority, positive or negative.

Note: Priorities are only compared when triggers have the same fire time. A trigger scheduled to fire at 10:59 will always fire before one scheduled to fire at 11:00.

Note: When a trigger’s job is detected to require recovery, its recovery is scheduled with the same priority as the original trigger.

优先级属性

有时候,当你有很多的触发器的时候(或者在你的Quartz线程池中有多个线程的时候),Quartz框架也许没有足够的计算资源在同一时间立刻执行事先设定好的所有触发器。在这种情况下,你也许想控制一下哪个触发器在Quartz框架腾出工作线程资源的时候,首先分得第一杯羹。为了达到这个目的,你可以给触发器设置优先级属性。如果有N个触发器在同一时间启动,但是只有Z个工作线程当前可用的话,则首先得Z个拥有最高优先级的触发器会被首先执行。如果你没有给触发器设置这个属性的话,则其会将默认优先级属性设置为5.任何整数值都可以设置为优先级属性的值,不论是正整数还是负整数。

注意:优先级属性只有在触发器拥有相同的启动时间的时候才会被比较。一个触发器如果被设置成10:59执行将会总是比设置成11:00点的触发器先执行。

注意:当一个触发器被框架发现需要重新启动的时候,其重新启动之后,其优先级和原先的触发器保持一致。

Misfire Instructions

Another important property of a Trigger is its “misfire instruction”. A misfire occurs if a persistent trigger “misses” its firing time because of the scheduler being shutdown, or because there are no available threads in Quartz’s thread pool for executing the job. The different trigger types have different misfire instructions available to them. By default they use a ‘smart policy’ instruction - which has dynamic behavior based on trigger type and configuration. When the scheduler starts, it searches for any persistent triggers that have misfired, and it then updates each of them based on their individually configured misfire instructions. When you start using Quartz in your own projects, you should make yourself familiar with the misfire instructions that are defined on the given trigger types, and explained in their JavaDoc. More specific information about misfire instructions will be given within the tutorial lessons specific to each trigger type.

Misfire Instructions属性(失火说明属性??或者叫异常处理方案?)

触发器另一个很重要的属性是其“misfire instruction”属性。如果一个触发器总是因为调度器的关闭,或者 Quartz框架线程池中没有可用线程而错过启动时间的时候,就会发生misfire(失火?异常?)行为。不同类型的触发器拥有不同类型的misfire instructions属性来应对这些情况的发生。默认状态下他们会使用“smart policy”instruction(“明智策略”方案?),这种情况下会根据触发器的类型和设置产生动态的应对行为。当你开始在你的项目中使用Quartz时,你应该花时间熟悉给定的不同触发器相对应的misfire instructions (异常处理方式?)并且在相关的接口文档中进行注释。更多的关于misfire instructions的详细信息会在接下来的教程中,在对每种触发器进行解释的时候进行说明。

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 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 objects that implement the Calendar interface, which looks like this:

The Calendar Interface

日历属性

Quartz框架的日历属性对象是对Calendar接口的实现,并且该对象会在触发器存储在调度器中的时候与触发器进行关联。在触发器的调度任务执行过程中要剔除大块的时间段的时候,日历对象会十分有用。比如你能创建一个触发器每周的工作日9:30进行一次作业,但是与此同时又希望能够剔除所有的法定节假日。

日历属性对象可以是任意实现了 Calendar接口的可序列化对象,比如下面这样:

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. As you may guess, they 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 such as this:

尽管日历对象可以剔除微秒级别的时间块,但是通常情况下,你会对剔除若干天这样的情况更感兴趣。为了方便,Quartz框架包含了HolidayCalendar(假期日历)类,就可以完成这样的工作。

日历对象必须通过 调度器的AddCalendar(..) 方法对其进行实例化和进行注册。如果你希望使用HolidayCalendar(假期日历)类,在实例化之后,你应该使用其AddExcludedDate(Date date)方法以便能够再任务调度的时间中剔除你不希望其执行的日子。同样的日历对象能在多个触发器中被使用,具体使用如下:

Calendar Example

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 details of the construction/building of triggers will be given in the next couple lessons. For now, just believe that 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.

See the org.quartz.impl.calendar package for a number of Calendar implementations that may suit your needs.

触发器创建/构建的详细细节,将会在接下来的几课中被给出。现在,只要相信上面的代码块创建了两个触发器,每个都被设置成每日执行。然而,在这两个触发器设置的剔除日期时刻,将会被其直接略过。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值