Z syntax in java

Source: Internet
Author: User

I hope this article will help you learn the basic knowledge of Quartz syntax in java.

The Quartz cron expression format is very similar to the UNIX cron format, but there is still a few obvious differences. One of the differences is that the Quartz format supports the second-level plan, while the UNIX cron plan only supports the minute-level plan. Many of our trigger plans are incremental in seconds (for example, every 45 seconds), so this is a very good difference.

In UNIX cron, the job (or command) to be executed is stored in the cron expression and located in the sixth domain. Quartz uses the cron expression to store the execution plan. The CronTrigger that references the cron expression will be associated with the job in the scheduled time.

Another difference from a UNIX cron expression is that it supports the number of domains. UNIX provides five domains (minute, hour, day, month, and week), and Quartz provides seven domains. Table 5.1 lists the seven fields supported by the Quartz cron expression.

Table 5.1. Quartz Cron expressions support seven fields
Name Required? Allowed value Special characters
Seconds Yes 0-59 ,-*/
Minute Yes 0-59 ,-*/
Hour Yes 0-23 ,-*/
Day Yes 1-31 ,-*? /L W C
Month Yes 1-12 or JAN-DEC ,-*/
Week Yes 1-7 or SUN-SAT ,-*? /L c #
Year No Null or 1970-2099 ,-*/


The names of months and weeks are case-insensitive. FRI and fri are the same.

Fields are separated by spaces, which is the same as UNIX cron. The simplest expression we can write seems like this:

***? **

This expression inspires a deployed job every second (every minute, every hour, and every day.

· Understanding special characters

Like UNIX cron, Quartz cron expressions support special characters to create more complex execution plans. However, Quartz is richer in support of special characters than standard UNIX cron expressions.

* Asterisks

The asterisk (*) indicates that you want to include all valid values in this field. For example, using an asterisk in a month domain means that the trigger is triggered every month.

Expression examples:

0*17 **?

Meaning: trigger is triggered every minute from five o'clock P.M. to PM. It stops at six o'clock P.M. pm because the value 17 is in the small time domain. At five o'clock P.M., the hour changes to 18, and the trigger is ignored until of the next day.

Use the * character when you want the trigger to be fired on all valid values of this field.

? Question mark

? It can only be used in the day and week domains, but cannot be used in both domains. You can think? The character is "I don't care about the value in this field. "Unlike asterisks, asterisks indicate each value in this field .? It means that no value is specified for this field.

The reason why these two fields cannot be specified at the same time is hard to explain or even hard to understand. Basically, if a value is specified at the same time, the meaning will become ambiguous: consider, if an expression has a value of 11 on the daily domain, and WED is specified on the weekly domain. So is trigger only on the 11th day of every month, and is triggered on Wednesday? Or is it triggered on the 11th of every Wednesday? To remove such ambiguity, you cannot specify values for both domains.

Remember, if you specify a value for one of these two fields, you must put one? on the other ?.

Expression examples:

0 10, 44 14? 3 WEB

Meaning: It is triggered at PM and PM on every Wednesday of March 13, March.

, Comma

Comma (,) is used to specify a value list for a domain. For example, if the value 0, 15, 30, and 45 are used, a trigger is triggered every 15 seconds.

Expression examples:

0 0, 15, 30, 45 ***?

Meaning: trigger a trigger within a clock.

/Slash

A slash (/) is used to increase the number of timelines. We just used a comma to indicate the increase every 15 minutes, but we can also write it as 0/15.

Expression examples:

0/15 0/30 ***?

Meaning: trigger every 15 seconds at the hour and half.

-Dashes

Hyphens (-) are used to specify a range. For example, 3-8 in a small time domain means "3, 4, 5, 6, 7, and 8. "Domain values cannot be rolled back, so values like 50-10 are not allowed.

Expression examples:

0 45 3-8? **

Meaning: trigger at AM to AM.

L letters

L indicates the last value allowed on a domain. It is only supported by day and week domains. When used in the day domain, it indicates the last day of the month specified in the month domain. For example, if JAN is specified in the current month, the L in the Japanese domain will trigger the trigger on January 1, January 31. If the monthly domain is SEP, L indicates that the domain will be triggered on March 13, September 30. In other words, no matter which month is specified, trigger is triggered on the last day of the corresponding month.

Expression 0 0 8 L *? Trigger is triggered at AM on the last day of each month. In the monthly domain, * indicates "every month ".

When L is used in the week domain, it indicates the last day of the week, that is, Saturday (or number 7 ). So if you need to trigger a trigger at on the last Saturday of each month, you can use this expression 0 59 23? * L.

When used in the week domain, you can use a number to connect to L to represent the last week X of the month. For example, the expression 0 0 12? * 2L calls trigger on the last Monday of each month.

Do not use range and List Value with L

Although you can use the number of weeks (1-7) with L, you are not allowed to use a range value and List Value with L. This produces unpredictable results.


W letters

The W character represents the day (Mon-Fri) and can only be used in the day domain. It is used to specify a calendar day closest to a specified day. Most of the business processing is based on the work week, So W characters may be very important. For example, 15 W in the Japanese domain means "the last day of the month on the 15th. "If the 15th is Saturday, trigger will be triggered on the 14th (Friday) Because Thursday is closer to the 15th (17 in this example. (Translator Unmi Note: it will not be triggered on the 17th. If it is 15 W, it may be triggered on the 14th (Saturday 15) or 15th (Sunday 15, that is, it can only appear in the adjacent day. If the day 15 is normal, it will be executed on the same day ). W can only be used for a single day in the specified daily domain, and cannot be a range or list value.

# Well ID

# Characters can only be used in the weekly domain. It is used to specify the day of the week in the month. For example, if you specify the value of the weekly domain as 6 #3, it means the third Friday of a month (6 = Friday, #3 means the third week of the month ). Another example 2 #1 indicates the first Monday of a month (2 = Monday, #1 indicates the first week of the month ). Note: If you specify #5, but there are no 5th weeks in the month, this month will not be triggered.

 

The Cron expression cookbook here is designed to provide a solution for common execution requirements. Although it is impossible to list all expressions, the following examples should be provided to meet your business needs.

· Minute Cron expression

Table 5.1. Cron expressions that contain minute-level task plans
Usage Expression
Triggered every minute from PM to PM every day 0*17 **?
Triggered every five minutes from PM to PM every day 0 0/5 23 **?
Triggered every five minutes from PM to PM and PM to PM every day 0 0/5 **?
Triggered every minute from AM to AM every day 0 0-5 5 **?


· Cron expression of explain


Table 5.2. Cron expressions for task plans based on the daily frequency
Usage Expression
AM every day 0 0 3 **?
AM every day (another way) 0 0 3? **
Every day (noon) 0 0 12 **?
At AM every day in 2005 0 15 10 **? 2005


· Cron expressions for weeks and months

Table 5.3. Cron expressions for task plans based on the week/month frequency
Usage Expression
AM on every Monday, Thursday, and Thursday 0 15 10? * MON-FRI
AM on the 15th of every month 0 15 10 15 *?
AM on the last day of each month 0 15 10 L *?
AM on the last Friday of every month 0 15 10? * 6L
At AM on the last Friday of 2002,200, 2004, and 2005 0 15 10? * 6L 2002-2005
AM on the third Friday of every month 0 15 10? * 6 #3
Every five days from the first day of every month (noon) 0 0 12 1/5 *?
Every November 11 AM 0 11 11 11 11?
And every Wednesday in March 0 10, 44 14? 3 WED


8. Create an immediate Trigger

Sometimes, you need to execute a job immediately. For example, imagine that you are building a GUI program and allowing users to execute it immediately. In another example, you may have detected that a Job is not successfully executed, so you want to re-run it immediately. In Quartz 1.5, there are several methods added to the TriggerUtils class, making it easy to implement those things. Code 5.4 shows how to deploy a job and only execute it once.

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.