I temporarily used to achieve a scheduled task of 2 kinds of ways
First, the annotation method realizes, is simple and convenient
1: Add the following configuration to the Applicationcontext.xml
This is a spring component scan that guarantees classes with timed tasks that can be scanned by spring
<context:component-scan base-package= "Com.test.task" >
This is the configuration of the timed task
<task:executor id= "Executor" pool-size= "1"/>
<task:scheduler id= "Scheduler" pool-size= "1"/>
<task:annotation-driven executor= "Executor" scheduler= "Scheduler"/>
How to write a timed task class
1. Labeling classes with @component
2 use @lazy (value=false) to label the class (this write also line, do not write also can.) I did not write when I was doing the project.)
3 Write a specific method
@Scheduled (cron= "0/5 * * * * *")//indicates the time of the specific operation
public void Testtask () {
Specific methods
}
4. Configuration Complete
Ii. How to implement the configuration file
1. To implement a timed task class, according to the ordinary Java class to write, the class of the class of the timer task of the method name written work () can be
For example
- public class Tmallpricestorechangefailedgetjob {
- public void work () {
- Concrete Method implementation
- }
- }
2. Copy a application.xml file, rename it to Application_tmallpricestorcechangefailed_task.xml, and add it to the spring profile
- <import resource= "application_tmallpricestorcechangefailed"/>
The 3.application_tmallpricestorcechangefailed_task.xml file is written in the following notation
- <!--Quartz Job config Begin--
- <bean id= "Tmallpricestorechangefailedgetjob" class= "Com.shopin.third.job.TmallPriceStoreChangeFailedGetJob"/ >
- <bean id= "gettmallpricestroechangefailed" class= " Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">
- <property name= "TargetObject" >
- <ref bean= "Tmallpricestorechangefailedgetjob"/>
- </property>
- <property name= "Targetmethod" >
- <value>work</value>
- </property>
- </bean>
- <bean id= "Gettmallpricestroechangefailedtask" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
- <property name= "Jobdetail" >
- <ref bean= "gettmallpricestroechangefailed"/>
- </property>
- <!--cron Expressions--
- <property name= "Cronexpression" >
- <value>0 * *?</value><!--done once a day at 9 points 30--
- </property>
- </bean>
- <!--quertz Boot configuration--
- <bean id= "Starttmallquertz" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
- <property name= "Triggers" >
- <list>
- <ref bean= "Gettmallpricestroechangefailedtask"/>
- </list>
- </property>
- </bean>
Implementation of Spring timed tasks < turn >