The Java timing scheduler of Quartz USES an instance

  • 2020-04-01 02:35:34
  • OfStack

1. Quartz timer execution process
Quartz needs to define the execution task, the trigger, the class object and the method of the object to be executed in the task, the execution time and the task to be executed in the trigger.

2. Configuration in the WEB environment


<!-- Define task classes  -->
<bean id = "tmpTask" class="com.tmp.task.BcdTask"/>
<!--  Defines the method to invoke the object and the method to invoke the object , tmpWork for BcdTask One of the methods in -->
<bean id="tmpJob" 
    class=" org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject">
        <ref bean = "tmpTask"/>
    </property>
    <property name="targetMethod" value="tmpWork"/>
</bean>
<!-- Define trigger  -->
<bean id="tmpTrigger" 
    class=" org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail">
        <ref bean = "tmpJob"/>
    </property>
    <property name=" cronExpression">
        <!--  The first  10 seconds   every  5 Execute once per second -->
           <value>10/5 * * * * ?</value>
    </property>
</bean>
<!-- Configure the tasks to be performed , Management trigger -->
<bean id="triggerManager" autowire="no"
    class=" org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name=" startupDelay" value="60"/>
    <property name="triggers">
            <list>
                <ref bean ="tmpTrigger"/>
            </list>
    </property>
</bean>

If you specify startupDelay, the QuartzScheduler is not started until the application is started. You can specify properties

Lazy-init = "false" cancels late startup, meaning that the scheduler executes immediately when the application starts.

3. Trigger Cron expression
Cron expression time field
The time field of Cron expressions allows you to set values, but you can also use special characters to provide lists, ranges, wildcards, and so on.

low asterisk (*) : can be used in all fields to represent each moment in the corresponding time domain. For example, * in the minute field means "every minute";

low question mark (?) : this character is only used in the date and day fields. It is usually specified as a "meaningless value", equivalent to a point character.

low minus sign (-) : represents a range, such as the use of "10-12" in the hour field, from 10 to 12 points, that is, 10,11,12;

low comma (,) : represents a list value, if "MON,WED,FRI" is used in the week field, it represents Monday, Wednesday, and Friday;

low slash (/) : x/y represents a sequence of equal steps, with x as the starting value and y as the incremental step value. If you use 0/15 in the minute field, it means 0,15,30, and 45 seconds, and 5/15 in the minute field means 5,20,35,50. You can also use */y, which is the same thing as 0/y;

low L: this character is used only in the date and day fields to represent "Last", but it has a different meaning in the two fields. L in the date field, represents the last day of the month, such as the 31st of January, and the 28th of February in a non-leap year; If L is used during the week, it means Saturday and is equal to 7. However, if L appears in the week field and is preceded by a value of X, it means "the last X days of the month." for example, 6L means the last Friday of the month;

low W: this character can only appear in the date field and is a modification of the leading date to indicate the nearest working day to that date. For example, 15W represents the nearest working day to the 15th of the month. If the 15th of the month is Saturday, it matches Friday the 14th. If the 15th is a Sunday, it matches Monday the 16th. If the 15th is Tuesday, the result is Tuesday the 15th. However, it must be noted that the associated matching date cannot span months, if you specify 1W, if the 1st is Saturday, the result matches Monday the 3rd, not the last day of the previous month. W string can only specify a single date, not a date range;

low LW combination: LW can be combined in the date field, which means the last working day of the month;

low hashtag (#) : this character can only be used in the day field to indicate a working day of the month. For example, 6#3 represents the third Friday of the month (6 represents Friday, and #3 represents the current third), while 4#5 represents the fifth Wednesday of the month.

low C: this character is used only in the date and day fields and represents "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 after the 5th day of the calendar. 1C in the day field corresponds to the day after Sunday.

Cron expressions are insensitive to the case of special characters and to the case of abbreviations representing the week.

Each of these elements can be a value (such as 6), a continuous interval (9-12), an interval (8-18/4)(/ for every 4 hours), a list (1,3,5), a wildcard. Since the two elements "date in month" and "date in week" are mutually exclusive, you have to set one of them, right? .

0, 0, 10,14,16 * *? B: ten o 'clock in the morning, two o 'clock in the afternoon, four o 'clock every day
0, 0 over 30, 9 minus 17 times?     Every half hour during 9 to 5 working hours
0 0 12? * WED means every Wednesday at 12 noon
"0, 0, 12 * *?" Triggered every day at 12 noon
"0 15 10? * *" triggered at 10:15 am every day
"0, 15, 10 * *?" Triggered at 10:15 am each day
"0, 15, 10 * *? *" triggered at 10:15 am every day
"0, 15, 10 * *? Trigger every day at 10:15 am in 2005
"0 * 14 * *?" Triggered every minute between 2 p.m. and 2:59 p.m
"0, 0, 5, 14 * *?" Trigger every 5 minutes between 2 p.m. and 2:55 p.m
"0, 0, 5, 14,18 * *?" Trigger every 5 minutes between 2 p.m. and 2:55 p.m. and 6 p.m. and 6:55 p.m
"0, 0, minus 5, 14 * *?" Trigger every minute between 2 p.m. and 2:05 p.m
"0 10 44 14? 3 WED" on wednesdays in March every year at 2:10 PM and 2:44 PM
"0 15 10? * mon-fri "triggers at 10:15 am Monday through Friday
"0, 15, 10, 15 times?" Triggered at 10:15 am on the 15th of each month
"0, 15, 10 L *?" Triggered at 10:15 am on the last day of each month
"0 15 10? * 6L" triggers at 10:15 am on the last Friday of each month
"0 15 10? * 6L 2002-2005" triggered at 10:15 am on the last Friday of each month from 2002 to 2005
"0 15 10? * 6#3" triggers at 10:15 am on the third Friday of the month

"0, 5, 2, 1, 8? *" triggered at 2:5 on the 1st of August each year


Some subexpressions can contain ranges or lists

For example: subexpression (day (week)) can be "mon-fri", "MON, WED, FRI", "mon-wed,SAT"

The "*" character represents all possible values

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

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

For example, "0/15" in the subexpression (minutes) means every 15 minutes starting at 0 minutes

                "3/20" in the subexpression (minutes) means starting at 3 minutes, every 20 minutes (it's the same as "3, 23, 43")


"?" Character is used only for day (month) and day (week) subexpression, indicating that no value is specified

When one of the 2 subexpression is assigned a value, the value of the other subexpression needs to be set to "? "to avoid collisions.

The "L" character is used only for the day (month) and day (week) subexpression, which is an abbreviation of the word "last"

But it has a different meaning in the two subexpression.

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

In the day (week) self expression, "L" represents the last day of the week, the SAT

If there is something specific before "L", it has other meanings

For example, "6L" means the last six days of the month, and "FRIL" means the most Friday of the month

Note: when using the "L" parameter, do not specify a list or scope, as this can cause problems


Related articles: