Quartz.NET - Lesson 4: More on trigger

Translator's Note: The directory in which [translation] Quartz.NET 3.x tutorial
Translator's Note: In this original Lesson 4: More About Triggers

Like jobs, triggers are also fairly easy to use, but it does contain a variety of customizable options, before the full use of Quartz.NET, you need to know about these options. In addition, as mentioned earlier, there are different types of triggers, you We can choose to meet different scheduling needs.

General Trigger Properties

In addition to all types of flip-flops have their properties for tracking TriggerKey identified, there are a number of other attributes are common to all types of triggers may be used TriggerBuilder set these common attributes defined in the construction of the trigger (examples below).

The following is a list of all flip-flops types of common attributes:

  • JobKey property represents the identity of the job should trigger execution.
  • StartTimeUtc property represents the first time the trigger schedule in effect. This value is the point of time on a given calendar date to define a DateTimeOffset object is used for certain types of flip-flop, flip-flop actually triggered at the start, while for others type, it just should mark the beginning follow the scheduled time. this means that you can store a trigger that contains a schedule, such as January's "day 5 per month," and if StartTimeUtc property is set to April 1 , then a few months before the first trigger.
  • EndTimeUtc attribute indicates when the trigger scheduling is no longer valid. In other words, a time schedule for the "5 per month" and end time for the flip-flop July 1 will activate the trigger in last June 5.

Other properties will be discussed further explained in the following sections.

priority

Sometimes, when you have a lot of flip-flops (or your Quartz.NET little thread pool worker thread), Quartz.NET may not have sufficient resources to all programs at the same time trigger trigger trigger immediately. In this under case, you may want to control which triggers the trigger first available Quartz.NET worker thread. To do this, you can set the priority attribute on the trigger. If the N flip-flops are triggered simultaneously, but only the current Z worker threads are available, the first execution Z flip-flops with the highest priority. If you do not set priorities on the trigger, it will use the default priority 5. priority can be set to any integer value, can be positive or negative.

Note: only when the trigger has the same trigger time are prioritized plan always trigger the trigger before 10:59 11:00 executed in the execution of the trigger.

Note: When the trigger is detected the need to restore job, resume its scheduling priority trigger the same as the original.

Description misfiring

Another important attribute is its trigger "Misfire Instructions". If for Scheduler close, causing continuous trigger "missed" its trigger time, or Quartz.NET threads in the thread pool is not available to perform the operation, misfiring occurs different types of triggers can use different misfiring instruction by default, they use "smart strategy" instruction - it has a dynamic behavior based on the trigger type and configuration when the scheduler starts, she will search for any failure of persistence flip-flop, flip-flop and then update each of them according to their instructions misfiring configuration when you start using Quartz.NET in your own projects, you should familiarize yourself with the type definition to the trigger of misfiring instruction set, and explain in their API documentation. for more specific information about the misfiring instruction will be given in the tutorial course for each trigger type.

calendar

Quartz ICalendar implements the interface. Calendar useful for troubleshooting blocks from the scheduled time schedule flip-flop NET Calendar object can be associated with the trigger is stored in scheduler and trigger. For example, you can create a trigger, and trigger a job at 9:30 am every weekday, then add the calendar to exclude all business holiday.

Calendars may be any object that implements ICalendar serializable interface, the interface ICalendar defined as follows:

namespace Quartz
{
    public interface ICalendar
    {
    string Description { get; set; }

    ICalendar CalendarBase { set; get; }

    bool IsTimeIncluded(DateTimeOffset timeUtc);

    DateTime GetNextIncludedTimeUtc(DateTimeOffset timeUtc);

    ICalendar Clone();
    }
}

Even though the calendar may be "shielded" a millisecond such a short period of time, but most likely is that you will "mask" more interested in a day. For convenience, Quartz.NET built like HolidayCalendar, she is doing of.

Calendar must be instantiated and registered with the dispatcher through AddCalendar (..) method. If you use HolidayCalendar, then after instantiation, you should use her AddExcludedDate (DateTime date) method to add you want to be excluded from the agenda . examples of the same calendar day may be used for multiple triggers, such as:

For chestnuts

HolidayCalendar cal = new HolidayCalendar();
cal.AddExcludedDate(someDate);
    
await sched.AddCalendar("myHolidays", cal, false);
    
Trigger t = TriggerBuilder.Create()
    .WithIdentity("myTrigger")
    .ForJob("myJob")
    .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(9, 30)) // 每天 9:30 执行作业
    .ModifiedByCalendar("myHolidays") // 指定节假日不执行
    .Build();

// .. schedule job with trigger

ITrigger t2 = TriggerBuilder.Create()
    .WithIdentity("myTrigger2")
    .ForJob("myJob2")
    .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(11, 30)) // 每天 11:30 执行作业
    .ModifiedByCalendar("myHolidays") // 指定节假日不执行
    .Build();
    
// .. schedule job with trigger2 

Trigger construction / building details will be given in the next few sections curriculum. Now, just need to believe the above code creates two triggers, trigger a schedule every day. In addition, occurred during the period of exclusion of any calendar trigger will be skipped.

See Quartz.Impl.Calendar namespace can be more suitable for your needs ICalendar implementation.

Guess you like

Origin www.cnblogs.com/taadis/p/quartz-3-x-tutorial-more-about-triggers.html
Recommended