Quartz 6 CronTrigger (official document translation), quartzcrontrigger

Source: Internet
Author: User

Quartz 6 CronTrigger (official document translation), quartzcrontrigger

CronTriggers is used more frequently than SimpleTrigger. If you want to trigger a Job in schedule in the form of a calendar, rather than a definite time interval, you need to use CronTrigger.

 

For CronTrigger, You can trigger Schedule. For example, Schedule is executed every Friday at or every workday at to; every five minutes on every Monday, Tuesday, or Friday.

 

If this is not the case, SampleTrigger and crontrigadh both have a start time and an optional end time to stop.

Cron Expressions

Cron-ExpressionsUsed for configurationCronTriggerInstance.Cron-ExpressionsIs a string. It consists of seven parts to describe Time Scheduling in detail. The content of each part is separated by spaces, and each part is represented as follows:

A simple example is used to explain: "0 0 12? * WED ". This string represents 12: 00 every Tuesday.

 

Individual sub-expressions can contain a range combination. For example, the day of week field can be written as "MON-FRI", "MON, WED, FRI" or "MON, WED, SAT" in the previous example ".

 

Each part uses wildcards to indicate all possible values of this field. Obviously, the Month field indicates all months, and the day of week indicates all days of each week.

 

Each field has its own value limit. The value range of these limits is quite obvious-in the minute and second fields, the value range is 0-59. The Hours field ranges from 0 to 23. The range of the day of month field is 0-31, but this is calculated based on the specific month. The valid value of Month is 0-11 .. Of course, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC can also be used. The valid range of day of week is 1-7 (Sunday). SUN, MON, TUE, WED, THU, FRI and SAT can also be used.

 

'/' Indicates a special growth interval. For example, if 0/15 is written to the Minutes field, it means that every 15 Minutes of execution is started at 0 Minutes. If you write 3/20, it indicates that the task is executed every 20 minutes and started after 3 minutes. The working time is 3, 23, 43.

 

'? 'Can be used in the day-of-month and day-of-week fields. Used to indicate that there is no specific significance. If you need to perform some special operations on these two fields, this symbol is very useful. For details, see the CronTrigger API documentation.

 

'L' can be used on the day-of-month and day-of-week fields. This character indicates different meanings in two fields. For example, day-of-month indicates the last day of the month. If the value is 7 (SAT) in day-of-week ). If there are other specific values before L, it indicates the last day of a month. For example, 6L or FRIL indicates the last week of the month. when using L, you need to know that it does not specify a special finger or a range, but an uncertain value.

 

'W' indicates the workday closest to the given day (from Monday to Friday ). For example, if you write 15 W on day-of-month, it indicates the workday closest to 15 of the month.

 

"#" Is used to specify the "nth" XXX business day of the month. For example, if "6 #3" or "FRI #3" is set on day-of-week, it indicates the third Friday of this month ".

Example Cron Expressions

The following is an example of a specific expression. for more information, see the API documentation for CronTrigger.

 

1. Execute every five minutes

"0 0/5 ***? "

 

2. Execute the task every five minutes and wait for 10 seconds.

"10 0/5 ***? "10:00:10 10:05:10

 

3. Run the command at every Monday and Friday.

"0 30 10-13? * WED, FRI"

 

4. Execute the task every 30 minutes between PM and PM on the 5 th and 20 th of each month.

"0 0/30 8-9 5, 20 *? "

 

Note: Some trigger mechanisms are too complex. For example, the trigger is executed every five minutes at 8-9 o'clock every month, and the trigger is executed every 20 minutes from to. Remember, you can install two independent triggers to work the same content.

 

Building CronTriggers

Build a specific CronTrigger instance usingTriggerBuilder(Used to configure the main content of a Trigger) andWithCronSchedule(Used to configureTrigger)Extension Method.

 

1. Create an execution every two minutes between and each day.

1 trigger = TriggerBuilder.Create()2     .WithIdentity("trigger3", "group1")3     .WithCronSchedule("0 0/2 8-17 * * ?")4     .ForJob("myJob", "group1")5     .Build();

2. Build a program and execute it once every day.

1 // we use CronScheduleBuilder's static helper methods here2 trigger = TriggerBuilder.Create()3     .WithIdentity("trigger3", "group1")4     .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(10, 42))5     .ForJob(myJobKey)6     .Build();

Or

1 trigger = TriggerBuilder.Create()2     .WithIdentity("trigger3", "group1")3     .WithCronSchedule("0 42 10 * * ?")4     .ForJob("myJob", "group1")5     .Build();

3. Build a every Tuesday and use a non-System Default Time Zone

1 trigger = TriggerBuilder.Create()2     .WithIdentity("trigger3", "group1")3     .WithSchedule(CronScheduleBuilder4         .WeeklyOnDayAndHourAndMinute(DayOfWeek.Wednesday, 10, 42)5         .InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time")))6     .ForJob(myJobKey)7     .Build();

Or

1 trigger = TriggerBuilder.Create()2     .WithIdentity("trigger3", "group1")3     .WithCronSchedule("0 42 10 ? * WED", x => x4         .InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time")))5     .ForJob(myJobKey)6     .Build();
CronTrigger Misfire Instructions

The following is CronTrigger MisFire. For more information, see API documentation.

  • MisfireInstruction. IgnoreMisfirePolicy
  • MisfireInstruction. CronTrigger. DoNothing
  • MisfireInstruction. CronTrigger. FireOnceNow

 

All triggers have available MisfireInstrution. SmartPolicy commands, which are also the default of all trigger types. The "wise policy" directive is interpreted by CronTrigger as MisfireInstruction. CronTrigger. FireOnceNow. The API documentation for the CronTrigger. UpdateAfterMisfire () method explains the details of this behavior.

When CronTriggers is created, the misfire you specify is part of the cron Plan (via the WithCronSchedule Extension Method ):

1 trigger = TriggerBuilder.Create()2     .WithIdentity("trigger3", "group1")3     .WithCronSchedule("0 0/2 8-17 * * ?", x => x4         .WithMisfireHandlingInstructionFireAndProceed())5     .ForJob("myJob", "group1")6     .Build();

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.