The newly introduced task namespace in spring. The quartz feature can be partially replaced, the configuration and API simpler, and annotations are supported.
The first step:
Configure and turn on spring in the relevant configuration file for spring (applicationcontext.xml or {project_name}_servelt.xml or stand-alone configuration file such as Xxx_quartz.xml) Schedule Task. Note that the highlighted part is required.
1 <?XML version= "1.0" encoding= "UTF-8"?> 2 <Beansxmlns= "Http://www.springframework.org/schema/beans" 3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 4 XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" 5 Xmlns:tx= "Http://www.springframework.org/schema/tx" 6 Xmlns:context= "Http://www.springframework.org/schema/context" 7 Xmlns:mvc= "Http://www.springframework.org/schema/mvc" 8 xmlns:p= "http://www.springframework.org/schema/p" 9 Xmlns:task= "Http://www.springframework.org/schema/task" Ten xsi:schemalocation=" One Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd A HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.1.xsd - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd - Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.1.xsd the Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd - Http://www.springframework.org/schema/task - http://www.springframework.org/schema/task/spring-task-3.0.xsd - "> + <Mvc:annotation-driven/> - <Context:component-scanBase-package= "Com.mytools.validator.engine" /> + A <!--Start Timer - at <Task:annotation-driven/> - </Beans>
Step Two:
You can specify the following annotation in a class that needs to be executed in a timed manner
1 // 2public voidthrows 34 }
Note: In fact, the following 3 kinds of time expressions can be specified in @scheduled:
(1) Fixedrate: This method is executed once every few milliseconds. Such as:
1 @Scheduled (fixedrate=2000) //2publicvoid Schedulemethod () { 3 System.out.println ("Hello world ..."); 4 }
(2) Fixeddelay: The method is deferred for a number of milliseconds after the execution of the method is completed.
(3) Cron: Detailed configuration of when the method is executed. The cron value is a cron expression. Such as:
1 @Scheduled (cron= "0 0 0 * * SAT") 2 public voidarchiveoldspittles () { 3 / /... 4 }
After a specified time, the task always executes 2 times the solution:
This is because it is easy for us to start 2 timed threads in a spring-based Web project:
First: When the Web container starts, it is loaded once when the Applicationcontext.xml (or other spring core configuration file) file is read.
Second time: Spring itself loads the applicationcontext.xml (or other spring core configuration files) at once.
Solution: Separate the relevant configuration of your task and load it through Context-param in Web. Xml. Instead of loading through spring.
1) independent out of Spring-task, such as the new name of a file named Cms_quartz.xml
2) in Web. XML to load the file:
1 <Context-param> 2 <Param-name>Contextconfiglocation</Param-name> 3 <Param-value>/web-inf/cms-servlet.xml, Classpath:cms-quartz.xml</Param-value> 4 </Context-param>
Note: Originating from HTTP://WWW.TUICOOL.COM/ARTICLES/JMU7BQ
Spring shedule Task Annotation implementation (two-time solution to start schedule Task)