Spring Task Configuration

Source: Internet
Author: User

Spring has a package for quartz, and spring itself provides a task timer (Spring-task) that summarizes it.
For quartz, we used to focus on two aspects, one is the business of the scheduled task, and the other is the cron expression. The timing task is related to the specific business, which does not need to be said, here only the meaning of the expression and its wording.
The cron expression consists of the following 7 fields and the order of difference: seconds 0-59, minutes 0-59, hours 0-23, month date 1-31, month 1-12 or JAN-DEC, week date 1-7 or Sun-sat, year (optional field) Leave blank or 1970-2099 and indicate special meanings by special characters, for the following:
The slash (/) character represents the increment value. For example, in the seconds field, "5/15" represents the beginning of the 5th second, once every 15 seconds.
Question mark (?) Characters and Letters L characters are available only in the date and week date fields within the month. A question mark indicates that the field does not contain a specific value. Therefore, if you specify a date within a month, you can insert "?" in the Date field within the week, which means that the date value in the week does not matter. Here's a very painful setting, not quartz, but a constraint that spring integrates with quartz, which is: Date (1-31) and week (SUN-SAT) both, must have a question mark (?), and spring will check the expression when the system starts, If it does not conform to its rules, it throws an exception. So in the use of this place must pay attention to, and this on Linux execution cron does not have this limit.
The letter L character is the last abbreviation. Placed in the Month Date field, indicates that the schedule is scheduled to be executed on the last day of the month. In the week Date field, if "L" exists alone, it is equal to "7", otherwise it represents the last instance of the week date within the month. So "0L" means that it is scheduled to be executed on the last Sunday of the month.
The letter (W) character arranges execution at the working day closest to the specified value. Placing "1W" in the Month Date field indicates that the execution is scheduled for the first business day of the month.
The pound sign (#) character specifies a specific weekday instance for a given month. Put "mon#2" in the week Date field, indicating that the task is scheduled for the second Monday of the month.
The asterisk (*) character is a wildcard character, indicating that the field can accept any possible value, an example of an expression.
Example:
"0 0 08 * *?" Daily 8 o'clock in the morning trigger
"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 2009-2019 "2009 to 2019 the last of the monthly Friday 10:15 trigger
"0 15 10?" * 6#3 "Monthly third Friday 10:15 trigger

There are two ways to implement a job task using spring quartz, One is inherited Org.springframework.scheduling.quartz.QuartzJobBean and the other does not need to be inherited, just need to be defined in the configuration file Org.springframework.scheduling.quartz.Method Invokingjobdetailfactorybean, and specify its TargetObject property as the job task class, the Targetmethod property is the task method.
<bean id="Job"class="Xx.xx.xx.Job"/>
<bean id="Crontask"class="Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="TargetObject"ref="Job"/>
<property name="Targetmethod"Value="Runwork"/>
<!--false indicates that the job does not execute concurrently and defaults to true-->
<property name="Concurrent"Value="false"/>
</bean>
The TargetObject property specifies a task class that is implemented in several ways.
1, can be annotated on the class with @component annotations, so that you do not have to define <bean id="Job" .../> these things up.
2, can be configured according to the above wording.
3, directly use the following wording.
<property name="TargetObject">
<bean class="Xx.xx.xx.Job"/>
</property>
Next Configure the Trigger
<bean id="DoWork"class="Org.springframework.scheduling.quartz.CronTriggerBean">
<property name="Jobdetail"Te ="Crontask"/>
<!-Daily 0:1 A.M.
<property name="Cronexpression"Value="0 * * *?"/>
</bean>
Finally configure the Dispatch factory
<bean class="Org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="Triggers">
<list>
<ref local="DoWork"/>
</list>
</property>
</bean>
Here, the entire configuration is complete. Let's take a look at the steps for Spring-task to implement timed tasks.
Spring has added its own Task Scheduler starting from 3.0, which is implemented by extending the class below the Java.util.concurrent package, which also uses cron expressions.
Using spring task is very simple, first increasing the namespace schema
<beans xmlns="Http://www.springframework.org/schema/beans"
......
xmlns:task="Http://www.springframework.org/schema/task"
xsi:schemalocation="
......
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd " >
Then add @component annotations to the timed task class and add @scheduled to the task method (cron = "0/5 * * * *?") Annotations, and let spring scan to the class.
Then add <task:annotation-driven/> This configuration, let spring identify @scheduled annotations ( org.springframework.scheduling.annotation.Scheduled).
OK, Setup is complete. If you want to expand it, change it to the following:
<task:executor id="Executor"Pool-size="5"/>
<task:scheduler id="Scheduler"Pool-size="5"/>
<task:annotation-driven executor="Executor"Scheduler="Scheduler"/>
If a lot of scheduled tasks, you can configure the executor thread pool, here executor meaning and java.util.concurrent.Executor is the same, pool-size the size of the official recommendation for 5~10. The pool-size of Scheduler is the Scheduledexecutorservice thread pool, which defaults to 1. If I set up 8 tasks, each task is executed every 5 seconds, the following code to copy 7 copies and then change to see the print results.
@Scheduled (cron = "0/5 * * * * *?")
PublicvoidWork1 () {
System. out. println (Thread.CurrentThread(). GetName () + "" + "WORK1: Execute once every 5 seconds");
}

The timed task executes 3 times, and we can see that the thread name is prefixed with scheduler, because we are already in <task:scheduler id="Scheduler"Pool-size="5"/> This configuration defines the result of ID scheduler, which is used as the prefix for the task thread, and then handed to the executor thread pool.
3 task execution, because we set the Task Scheduler thread pool size of 5, so, only 5 instances to handle these 8 tasks, from the results can be seen, not every time will be used on all 5 instances. If you have too many scheduled tasks in your system, the size of this pool-size should be larger and easier to implement than the executor thread pool defined earlier.

Spring Task Configuration

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.