Quartz Cron expression (time format)

Source: Internet
Author: User
Tags valid

The format of the Quartz cron expression is very similar to that of the UNIX cron format, but there is a slight difference. One of the differences is that the Quartz format supports the plan down to the second level, while the UNIX cron program only supports up to the minute level. Many of our trigger plans are incremented based on the second level (for example, every 45 seconds), so this is a very good difference.

In UNIX Cron, the job (or command) to execute is stored in a cron expression, at the sixth domain location. The Quartz uses a cron expression to store the execution plan. A crontrigger that references a cron expression is associated with the job during the scheduled time.

Another point that differs from a UNIX cron expression is the number of fields that are supported in an expression. UNIX gives five domains (minutes, hours, days, months, and weeks), and Quartz provides seven domains. Table 5.1 lists the seven domains supported by the Quartz cron expression.

Table 5.1. Quartz Cron expression supports to seven domains
Name Whether you must Allowed values Special characters
Seconds Is 0-59 , - * /
Part Is 0-59 , - * /
When Is 0-23 , - * /
Day Is 1-31 , - * ? /L W C
Month Is 1-12 or Jan-dec , - * /
Week Is 1-7 or Sun-sat , - * ? /L C #
Years Whether Empty or 1970-2099 , - * /

The names of the month and week are case-insensitive. FRI and FRI are the same.

Fields are separated by spaces, as is the case with UNIX cron. It's indisputable that the simplest expression we can write seems to be this:

* * * ? * *

This expression will inspire a deployed job every second (every minute, every hour, every day).

• Understanding special characters

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

* Asterisk

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

Expression Model Example:

0 * 17 * *?

Significance: Trigger every minute from 5 o'clock in the afternoon to 5:59 every day. It stops at 5:59 because the value is 17 in the hour field, and at 6 o'clock in the afternoon, the hour becomes 18, and no longer cares about the trigger until the next day of 5 o'clock in the afternoon.

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

? Question mark

? The number can only be used on the day and week fields, but not in both fields. Can you think? The character is "I don't care what the value is on that field." "This is different from the asterisk, which indicates each value on that field." is to say that you do not specify a value for this field.

The reason why you cannot specify a value on both fields is difficult to explain and even incomprehensible. Basically, assuming that you specify a value at the same time, the meaning becomes ambiguous: consider if an expression has a value of 11 on the day field, and WED is specified on the domain of the week. Then it was to be trigger only 11th a month, and it was the day of Wednesday that was triggered. Or in every Wednesday number 11th is excited. The way to get rid of this uncertainty is not to specify values on both domains at the same time.

Just remember, if you specify a value for one of these two fields, then you have to put one on the other word value.

Expression Model Example:

0 10,44 14? 3 WEB

Meaning: Every Wednesday 2:10 and 2:44 in March is triggered.

, comma

A comma (,) is used to specify a list of values on a domain. For example, using a value 0,15,30,45 on a second field means that a trigger is triggered every 15 seconds.

Expression Model Example:

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

Meaning: Every moment of the clock triggers a trigger.

/Oblique Bar

The slash (/) is incremented for the timesheet. We've just used commas to denote increments every 15 minutes, but we can also write 0/15.

Expression Model Example:

0/15 0/30 * * *?

Meaning: Triggers the trigger every 15 seconds at the hour and the slightest.

-Medium Underline

The underscore (-) is used to specify a range. For example, the 3-8 on the hour field means "3,4,5,6,7 and 8 points." "The value of the field is not allowed to be rolled back, so a value like 50-10 is not allowed."

Expression Model Example:

0 45 3-8? * *

Meaning: In the morning of 3 points to the morning 8 points of 45 time trigger trigger.

L Letters

L describes the last value allowed on a domain. It is supported only by day and week fields. When used on the Day field, represents the last day of the month specified on the month field. For example, when a trigger is specified on the current month, L on the day field causes the trigger to be triggered on January 31. If there is SEP on the moon, then L will herald a trigger on September 30. In other words, no matter which month is specified, the trigger is triggered on the last day of the corresponding month.

Expression 0 0 8 L *? The meaning is to trigger trigger at 8:00 on the last day of the month. The * description on the month field is "every month".

When the L letter is used on the field, it indicates the last day of the week, which is Saturday (or the number 7). So if you need to trigger trigger in the last Saturday afternoon of each month, you can use the expression 0 59 23? L

When used on the week field, you can use a number to connect with L to represent the last one week X of the month. For example, an expression 0 0 12? * 2L says the trigger is triggered on the last one weeks of each month.

Do not allow range and list values to be used with L

Although you can use the week number (1-7) with L, you are not allowed to use a range value and a list value with L. This can produce unpredictable results.

W Letter

The W character represents the weekday (MON-FRI) and can only be used in the day domain. It is used to specify the most recent weekday from the specified day. Most business processes are based on work week, so W characters can be very important. For example, the 15W in the day field means "the most recent weekday from the month 15th." "If number 15th is Saturday, then trigger will be triggered at number 14th (Thursday), since 15th is the nearest Monday, this example will also be number 17th (translator Unmi Note: Will not be triggered in 17th, if it is 15W, it may be 14th (15th is Saturday) or 15th ( Number 15th is Sunday) triggered, that is, can only appear in the vicinity of the day, if 15th is on weekdays directly on the day of execution. W can only be used in the specified day field as a single day, not as a range or list value.

# Well No.

# characters can only be used in the week field. It is used to specify the day of the week ordinal of the month. For example, if you specify the value of the week field as 6#3, it means the third Friday of the month (6 = Friday, #3意味着月份中的第三周). Another example 2#1 meaning is the first Monday of a month (2 = Monday, #1意味着月份中的第一周). Note that if you specify a #5, but there is no 5th week in the month, then the month will not be triggered.

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.