Java Timer Scheduler (Quartz) Use instance _java

Source: Internet
Author: User
Tags numeric value time interval

1, quartz timer execution process
Quartz needs to define methods for executing tasks, triggers, specifying the execution of class objects and objects in the task, and defining the time of execution and the tasks to perform in the trigger.

2, the configuration in the Web environment

Copy Code code as follows:

<!--define Task class-->
<bean id = "Tmptask" class= "Com.tmp.task.BcdTask"/>
<!--define methods for calling objects and calling objects, Tmpwork as a method in Bcdtask-->
<bean id= "Tmpjob"
class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
<property name= "TargetObject" >
<ref bean = "Tmptask"/>
</property>
<property name= "Targetmethod" value= "Tmpwork"/>
</bean>
<!--define triggers-->
<bean id= "Tmptrigger"
class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
<property name= "Jobdetail" >
<ref bean = "Tmpjob"/>
</property>
<property name= "Cronexpression" >
<!--10th Second 5 seconds to perform a-->
&LT;VALUE&GT;10/5 * * * *?</value>
</property>
</bean>
<!--configuration execution tasks, managing triggers-->
<bean id= "Triggermanager" autowire= "no"
class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name= "Startupdelay" value= "/>"
<property name= "Triggers" >
<list>
<ref bean = "Tmptrigger"/>
</list>
</property>
</bean>

If Startupdelay is specified, Quartzscheduler will not start until the application is started. You can specify properties

Lazy-init = "false" cancels the delay start, that is, the scheduler is executed as soon as the application is started.

3. Trigger Cron Expression
Cron Expression Time field
The time field of a cron expression allows you to set a numeric value, and you can use special characters to provide features such as lists, scopes, wildcards, and so on.

Asterisk (*): Used in all fields to represent every moment of the corresponding time field, for example, * "Per minute" in the minute field;

Question mark (?): The character is used only in the date and week fields, which is usually specified as "meaningless value", equivalent to a bit character;

Minus sign (-): expresses a range, such as using "10-12" in the hour field, representing 10 to 12 points, i.e. 10,11,12;

Comma (,): Expresses a list value, such as "Mon,wed,fri" in the Week field, for Monday, Wednesday, and Friday;

Slash (/): X/y expresses a sequence of equal steps, X is the starting value, and Y is the increment step value. If you use 0/15 in the minute field, it is 0,15,30 and 45 seconds, and 5/15 represents 5,20,35,50 in the minute field, you can also use */y, which is equivalent to 0/y;

L: This character is used only in the date and week fields, meaning "last", but it means different in two fields. L In the Date field, represents the last day of the month, such as number 31st in January, not leap year February 28th; if L is used in the week, it means Saturday, equivalent to 7. However, if L appears in the Week field and has a value x in front of it, it means "last x days of the Month", for example, 6L represents the last Friday of the month;

W: This character can only appear in the Date field and is a cosmetic of the leading date that represents the most recent weekday from that date. For example, 15W represents the most recent working day from 15th, if the month 15th is Saturday, match 14th Friday; if 15th is Sunday, match 16th Monday; if 15th is Tuesday, the result is 15th Tuesday. But it must be noted that the associated matching date is not able to span the month, as you specify 1W, if number 1th is Saturday, the result matches the number 3rd Monday, not last month. The W string can only specify a single date and cannot specify a date range;

LW combination: In the Date field can be combined to use LW, which means the last working day of the month;

Pound sign (#): This character can only be used in the week field, indicating a weekday of the month. If 6#3 said that the third Friday of the month (6 for Friday, #3表示当前的第三个), and 4#5 that the month of the fifth Wednesday, assuming that the month does not have the fifth Wednesday, ignoring the trigger;

C: This character is used only in the date and week fields and represents the meaning of calendar. It means the date that the plan is associated with, and if the date is not associated, it is equivalent to all the dates in the calendar. For example 5C in the Date field corresponds to the first day of the calendar after 5th. 1C is equivalent to the first day after Sunday in the Week field.

Cron expressions are not sensitive to the case of special characters, nor are they sensitive to the abbreviated English case of the representative week.

Each element can be a value (such as 6), a continuous interval (9-12), a time interval (8-18/4) (per 4 hours), a list (1,3,5), wildcard characters. Because the two elements, "date in the month" and "date in the Week", are mutually exclusive, one of the settings must be set?

0 0 10,14,16 * *? Every day 10 o'clock in the morning, 2 o'clock in the afternoon, 4.
0 0/30 9-17 * *? Nine to five working hours every half hour
0 0 12? * WED says every Wednesday 12 o'clock noon
"0 0 12 * *" Every day 12 o'clock noon.
"0 15 10?" * * "Every 10:15 trigger
"0 15 10 * *" Every day 10:15 trigger
"0 15 10 * *? * "Every 10:15 trigger
"0 15 10 * *? 2005 "2005 Every day 10:15 triggers
"0 * 14 * *" is triggered every 1 minutes from 2 o'clock in the afternoon to 2:59 every day
"0 0/5 14 * *" is triggered every 5 minutes from 2 o'clock in the afternoon to 2:55 every day
"0 0/5 14,18 * * *" triggered every 5 minutes from 2 o'clock in the afternoon to 2:55 and 6 o'clock in the afternoon to 6:55 every day
"0 0-5 14 * *" is triggered every 1 minutes from 2 o'clock in the afternoon to 2:05 every day
"0 10,44 14?" 3 WED "Every March of Wednesday, 2:10 and 2:44 triggers
"0 15 10?" * Mon-fri "from Monday to Friday 10:15 trigger
"0 15 10 15 *" Every month 15th 10:15 trigger
"0 L *?" triggered 10:15 the last day of every month
"0 15 10?" * 6L "last Friday 10:15 trigger per month
"0 15 10?" * 6L 2002-2005 "2002 to 2005 monthly last Friday 10:15 trigger
"0 15 10?" * 6#3 "The third Friday 10:15 trigger per month

"0 5 2 1 8?" * "August 1 of the year 2:5 trigger


Some of the subexpression can contain ranges or lists

For example: a subexpression (days (weeks)) can be "Mon-fri", "Mon,wed,fri", "Mon-wed,sat"

The "*" character represents all possible values

Therefore, "*" represents the meaning of each month in the subexpression (month), and "*" in the subexpression (Day (week)) represents every day of the week

The '/' character is used to specify the increment of the value

For example, the expression "0/15" in a subexpression (minutes) starts from the No. 0 minute, every 15 minutes

The expression "3/20" in the subexpression (minutes) is the same as the meaning of every 20 minutes (it's "3,23,43") starting at the 3rd minute.


“? "Character is used only for days (months) and days (weeks), two subexpression, indicating unspecified value

When one of the 2 subexpression is assigned a value, in order to avoid conflict, you need to set the value of another subexpression to "? ”

The "L" character is used only for days (months) and days (weeks) of two subexpression, which is the abbreviation for the word "last"

But it has different meanings in the two-subexpression.

In the day (month) subexpression, "L" means the last day of the one month

In the day (week) Self expression, "L" means the last day of the one week, which is the SAT

If there is a specific content before "L", it has other meanings.

For example: "6L" means the 6th day of the month, and "Fril" means the last Friday of the month.

Note: Do not specify a list or range when using the "L" parameter, because this can cause problems

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.