Quartz_2.2.X学习系列四: Tutorials - Lesson 4: More About Triggers

第四课小结:触发器的可定制的选项。

通用的触发器属性,所有触发器类型共有的属性列表:

  • TriggerKey属性:表明当触发器触的唯一标识。
  • jobKey属性:表明当触发器触发时应该执行的JOB的唯一标识。
  • startTime属性:表明触发器的schedule何时开始生效。
  • endTime属性:表明触发器的schedule何时不再生效。

 

  • 优先级(Priority)

当您有许多触发器(或者在您的Quartz线程池中只有很少的工作线程)时,Quartz可能没有足够的资源来立即触发所有预定同时触发的触发器。在这种情况下,您可能想要控制您的触发器在可用的Quartz工作线程上首先得到的执行。

 

 

  • 激活失败指令(Misfire Instructions)

如果由于scheduler被关闭,或者因为在Quartz的线程池中没有可用的线程来执行这项工作,而导致一个持久的触发器“错过”了它的触发时间,那么就会发生"misfire"。不同的触发类型有不同的"misfire指令"。默认情况下,他们使用“智能策略”指令——它基于触发器类型和配置的来动态的判定行为。

 

  • 日历(Calendars)

Quartz日历对象(不是java.util。日历对象)可以在触发器被定义并存储在调度程序的时候与触发器相关联。日历对于在触发器的scheduler设置排除时间段是很有用的。

 

日历必须通过addCalendar(..)方法实例化并在调度程序中注册。如果您使用假日日历,在实例化它之后,您应该使用它的addExcludedDate(Date date)方法来填充日期,以便实现你想在一个scheduling中排除这些日期。同一个日历实例可以与多个触发器一起使用。

 

 

Lesson 4: More About Triggers

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之前,您需要了解和理解这些选项。另外,正如前面提到的,您可以选择不同类型的触发器来满足不同的调度需求。

在第5课和第6课,您将了解两种最常见的触发器类型。

 

 

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).

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.

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

 

通用的触发器属性

除了所有触发器类型都有用于跟踪其标识的TriggerKey属性(name + group组合KEY)之外,在所有触发器类型中,还有许多其他属性都是通用的。当您构建触发器定义时,可以使用TriggerBuilder来设置这些公共属性(参考后面的示例)。

下面是所有触发器类型共有的属性列表:

  • “jobKey”属性:表明当触发器触发时应该执行的JOB的标识。
  • “startTime”属性:表明触发器的schedule何时开始生效。值是一个java.util.Date 对象,它在给定的日历日期上定义了一个时间点。对于某些触发器类型,触发器实际上会在开始时间触发,而对于其他触发器来说,它只是标记了 schedule应该开始的时间。比如:你可以在1月份存储一个触发器,这个触发器会“每个月的第5天”时触发,但是如果 startTime属性设置为4月1日,则触发的时候会是4月5号(第一次触发时间,是startTime的几个月后了)。
  • "endTime"属性:表明触发器的schedule何时不再生效。换句话说,一个设置为“每个月的第5天”执行schedule ,以及7月1日的结束时间的触发器,将会在6月5日作最后一次的触发。
  • 其他的属性:更多的解释,在下面的小节中讨论。

 

 

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触发的触发器提前触发(即此时优先级设定是无效的)。

注意:当检测到触发器的Job需要恢复时,它恢复后会与原始的触发器的优先级相同。

 

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 instruction”。如果由于scheduler被关闭,或者因为在Quartz的线程池中没有可用的线程来执行这项工作,而导致一个持久的触发器“错过”了它的触发时间,那么就会发生"misfire"。不同的触发类型有不同的"misfire指令"。默认情况下,他们使用“智能策略”指令——它基于触发器类型和配置的来动态的判定行为。当调度程序启动时,它会搜索任何被misfire的持久性触发器,然后根据各自配置的misfire指令对每个触发器进行更新。当您开始在自己的项目中使用Quartz时,您应该去熟悉在给定的触发器类型上定义的misfire指令,并在其JavaDoc中解释。关于更具体的misfire指令信息将在每个触发类型的教程中给出。

 

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.

 

日历

Quartz日历对象(不是java.util。日历对象)可以在触发器被定义并存储在调度程序的时候与触发器相关联。日历对于在触发器的scheduler设置排除时间段是很有用的。例如,您可以创建一个触发器,在每个工作日的上午9:30触发一个作业,然后添加一个日历,该日历不包括所有的假期。(即实现在每一个工作日【除假日】的09:30执行任务)

 

Calendar’s can be any serializable objects that implement the Calendar interface, which looks like this:

 

The Calendar Interface


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:

 

请注意,这些方法的参数是 long类型的。正如您可能猜到的,它们是毫秒级的时间戳。这意味着日历可以“屏蔽”毫秒级的时间段。最有可能的是,你会对“屏蔽”整天感兴趣。为了更方便,Quartz包含了实现类似功能的org.quartz.impl.HolidayCalendar类。

 

日历必须通过addCalendar(..)方法实例化并在调度程序中注册。如果您使用假日日历,在实例化它之后,您应该使用它的addExcludedDate(Date date)方法来填充日期,以便实现你想在一个scheduling中排除这些日期。同一个日历实例可以与多个触发器一起使用,例如:

 

 

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.

 

在接下来的几节课中,将会给出关于触发器构建的细节。现在,只要相信上面的代码创建了两个触发器,每个计划每天触发。然而,在日历排除的时间段内发生的任何触发都将被跳过。

查看org.quartz.impl.calendar 包,里面有许多Calendar 的实现类,这里可能合适你的需求。

 

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

 

猜你喜欢

转载自blog.csdn.net/arnolian/article/details/82528053