In a real-world project, a timed task (timed job) is often required, and the spring Framework provides a good implementation.
1. Download Spring-quartz plug-in package
The default is that the basic functionality of the spring framework is integrated in the current system. Go online to download the Spring Timer jar package, here is the Quartz-all-1.8.4.jar, download completed after the addition of the project Classpath.
2, write the code of the Timed Task execution module
Spring makes timed task coding very convenient, when writing execution modules, do not need to be coupled with any spring framework, do not need to inherit or implement a class or interface, only need to write general classes and methods. The business logic that needs to be executed is usually written as a method for the timer to invoke.
3. Configure Scheduled Tasks
The configuration file is placed in the same path as the spring normal configuration file. The logic of the configuration is this:
A first initializes the class of the dispatch task, which, like any other general configuration, uses the spring IOC mechanism to treat the timed task as a normal bean.
b then configure the task to execute a task, implemented by Methodinvokingjobdetailfactorybean. This configuration tells the container which class and which method performs the specific scheduled task. A timed task corresponds to a method and also to a task configuration. Here you need to configure two parameters, execute the class instance TargetObject and Execute method targetmethod. An instance of the execution class is the preceding configuration. The method that is executed is the specific method of performing the task in the class.
C Next, configure a timed schedule trigger, typically implemented by Crontriggerbean, which tells spring when to perform a task. This configuration requires two parameters: the executed task Jobdetail and the trigger timing expression cronexpression. The value of the first parameter is the preceding task, and the second is an expression that indicates when to perform the task, such as 0 0 0 1 *? indicates that the task is triggered by 1 good 0:0 per month. The specific configuration references are described later.
The D Configuration Center Scheduler, implemented by Schedulerfactorybean, tells Spring that it needs to start those triggers. There is only one parameter triggers, which accepts a list of triggers and how many of them have timed tasks.
Here, the configuration is complete.
4, start the project, test.
5. Example:
A task class
public class Monthlyjob {private A a;/** * Specific task */public void Initwjxx () {...} public void SetA (a a) {THIS.A = A;}}
B configuration File
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "><!--Perform job class--><bean id=" Monthlyjob "class=" Com.pmi.quartz.MonthlyJob "><! --Inject attribute value--><property name= "a" ref= "a"/></bean><!--initialize dispatch task-survey information--><bean id= " Simmonthlyjobtask "class=" Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean "> < !--called class-<property name= "TargetObject" > <ref bean= "monthlyjob"/> </propert Y> <!--calling methods in classes--<property name= "Targetmethod" > <value>initwjxx</value > </property> </bean><!--trigger-Questionnaire information--><bean id= "Crontriggertest" class= "Org.springframework.scheduling.quartz.CronTriggerBean" ><!--point to our task--><property name= " Jobdetail "ref=" Simmonthlyjobtask "/><!--00:00 trigger per month 1th,--><property name=" cronexpression "value=" 0 0 0 1 *? " /></bean><!--Total Scheduler--><bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" ><property name= "triggers" ><list><!--trigger list--><ref bean= "Crontriggertest"/></list ></property></bean></beans>