In the Java EE system, we often use scheduled tasks, such as daily morning to generate the day before the report, every hour to generate summary data and so on.
We can do this with Java.util.Timer combined with java.util.TimerTask, but scheduling control is very inconvenient, and we need a lot of code.
Using the quartz framework is undoubtedly a very good choice, and it can be very easy to integrate with spring, with a detailed description of their integration methods and cron expressions.
One, increase the dependency of the jar package
1. Increase Spring's maven dependency
<Dependency>
<GroupId>Org.springframework</groupid>< Span style= "color: #000000;" >
<artifactid >spring-webmvc</ artifactid>
Span style= "color: #0000ff;" ><version> 3.0.5.release</version >
</dependency>
2. Increase Quartz's maven dependency
<Dependency>
<GroupId>Org.quartz-scheduler</groupid>< Span style= "color: #000000;" >
<artifactid >quartz</ Artifactid>
version>1.8.4 </version>< Span style= "color: #000000;" >
</dependency
Second, increase the timing business logic class
Public ClassExpirejobtask {
/**Logger*/
Private static final Logger Logger = Loggerfactory.getlogger ( Expirejobtask. class);
/**
* Business logic processing
public void Dobiz () {
// Execute business logic
// .....
} /span>
The Expirejobtask business logic class does not have the task difference from the general ordinary class, it defines the Dobiz method as the dispatching business method.
Third, add spring configuration
1. Add a thread pool
<!--Thread executor configuration for task registration-
<BeanId= "Executor"Class= "Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<PropertyName= "Corepoolsize"Value= "10" />
<property name= "Maxpoolsize" Value= " />
< Property name= "queuecapacity" Value= " />
</bean< Span style= "color: #0000ff;" >>
2. Define business logic processing class
<!-- Business Objects--
<bean id= "bizobject" class= "Com.aboy.potak.common.toolkit.scheduling.ExpireJobTask" />
3, increase the scheduling business logic
<!--Dispatch Business-
<BeanId= "Jobdetail"Class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
<property name= "TargetObject" Ref=" Bizobject " />
< Property name= "Targetmethod" Value=" dobiz " />
</bean< Span style= "color: #0000ff;" >>
In the above configuration, we use the Bizobject.dobiz method to execute logic for the business that will be dispatched.
4. Increase the Dispatch trigger
<BeanId= "Crontrigger"Class= "Org.springframework.scheduling.quartz.CronTriggerBean">
<property name= "Jobdetail" Ref=" Jobdetail " />
< Property name= "Cronexpression" Value= "10 0/1 * * *?" />
</bean>
Cron expression "10 */1 * * *?" Starts at 10 seconds and executes every 1 minutes.
<BeanId= "Tasktrigger"Class= "Org.springframework.scheduling.quartz.SimpleTriggerBean">
<PropertyName= "Jobdetail"Ref= "Jobdetail" />
<property name= "Startdelay" Value=" 10000 " />
< Property name= "Repeatinterval" Value=" 60000 " />
</bean< Span style= "color: #0000ff;" >>
The schedule indicates that the delay starts 10 seconds and then executes every 1 minutes.
5, Increase the dispatch
<!--Set Schedule-
<BeanClass= "Org.springframework.scheduling.quartz.SchedulerFactoryBean">
<PropertyName= "Triggers">
<List>
<RefBean= "Crontrigger" />
</list>
Span style= "color: #0000ff;" ></property>
<property name = "Taskexecutor" Ref= " Executor " />
Span style= "color: #0000ff;" ></bean>
In the Triggers property, we can add multiple triggers.
In this way, spring has been perfectly integrated with quartz, and our next job is to start the system and start dispatching.
Iv. detailed usage of cron expressions
Special characters allowed for field allowed values
Seconds 0-59,-*/
Sub 0-59,-*/
Hours 0-23,-*/
Date 1-31,-*? /L W C
Month 1-12 or JAN-DEC,-*/
Week 1-7 or Sun-sat,-*? /L C #
Year (optional) leave blank, 1970-2099,-*/
Example:
0/5 * * * *? : Executes once every 5 seconds
The "*" character is used to specify all values. such as: "*" in the field of the minute to indicate "per minute."
“?” Characters are used only in date and weekday fields. It is used to specify a "non-explicit value". It is useful when you need to specify something by one of these two fields. Look at the example below and you'll understand.
The date in the month and the date in the day of the week these two elements should be mutually exclusive together by setting a question mark to indicate that you do not want to set that field.
The "-" character is used to specify a range. For example: "10-12" in the hour field means "10 points, 11 points, 12 points".
The "," character is used to specify a different value. such as: "Mon,wed,fri" in the field of the week to say "Monday, Wednesday, Friday."
The "/" character is used to specify increments. For example: "0/15" in the second field means 0,15,30 and 45 seconds per minute. "5/15" in the minute field represents the hourly 5,20,35 and 50. The symbol "*" in front of "/" (eg: */10) is equivalent to 0 in front of "/" (for example: 0/10). Remember the essence: each numeric field of an expression is a collection with the largest and smallest values, such as: The collection of second and minute fields is 0-59, the Date field is 1-31, and the Month field is 1-12. The character "/" helps you to take a corresponding value in each character field. such as: "7/6" in the month domain only when the July will be triggered, does not mean that every June.
L is the ellipsis of ' last ' to represent the Day-of-month and Day-of-week fields, but they differ in two fields, such as the last day of one months represented in the Day-of-month field. If the Day-of-week field represents ' 7 ' or ' SAT ', if preceded by a number in the Day-of-week field, it represents the last days of one months, e.g. ' 6L ' represents the last Friday of one months.
The character "W" only allows date fields to appear. This character is used for the last working day of the specified date. For example, if you write "15W" in the Date field, it means: the most recent working day of 15th this month. So, if number 15th is Saturday, then the task will be triggered at number 14th. If the 15 good is Sunday, then the task will be triggered in Monday, the number 16th. If you fill in the Date field "1W" even if the number 1th is Saturday, then the task will only be in the next Monday, that is, the number 3rd trigger, the "W" character specified in the last working day is not able to cross the month. The character "W" can only be used with a single numeric value and cannot be a number field, such as: 1-15w is wrong.
"L" and "W" can be combined in a date field, and LW represents the working day of the last week of the month.
The character "#" is only allowed to appear in the day of the week field. This character is used to specify a certain day of the month. For example: "6#3" means Friday of the third week of this month (6 for Friday, 3 for the third week). "2#1" represents the first week of this month in Monday. "4#5" means Wednesday of the fifth week.
The character "C" is allowed to appear in the Date field and the Weekday field. This character relies on a specified "calendar". This means that the value of this expression depends on the results of the relevant "calendar", and if there is no calendar association, it is equivalent to all included calendars. For example: the Date field is "5C" for the first day of the associated calendar, or 5 days after the first day of the month. The week field is "1C" for the first day of the associated calendar, or 1 days after the first day of the week, that is, the day after Sunday (Monday).
V. Examples of expressions
"0 0 12 * *?" trigger 12 o'clock noon every day.
"0 15 10?" * * "trigger 10:15 every day"
"0 15 10 * *?" Daily 10:15 Trigger
"0 15 10 * *?" * "10:15 per day" trigger
"0 15 10 * *?" 2005 "2005-year daily 10:15 Trigger
"0 * 14 * *?" triggers every 1 minutes from 2 o'clock in the afternoon to 2:59 daily
"0 0/5 14 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily
"0 0/5 14,18 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily and from 6 o'clock in the afternoon to 6:55
"0 0-5 14 * *?" triggers every 1 minutes from 2 o'clock in the afternoon to 2:05 daily
"0 10,44 14?" 3 WED "2:10 and 2:44 triggers in Wednesday of every March
"0 15 10?" * Mon-fri "Monday to Friday 10:15 trigger
"0 15 10 15 *?" 15th 10:15 per month
"0 L *?" 10:15 on the last day of the month
"0 15 10?" * 6L "Last month of Friday 10:15 Trigger
"0 15 10?" * 6L 2002-2005 "2002 to 2005 the last of the monthly Friday 10:15 trigger
"0 15 10?" * 6#3 "Monthly third Friday 10:15 trigger
(2) Spring Integrated Quartz timing Task framework and cron expression details