Using quartz in spring, this article describes Spring3.0 's self-developed scheduled Task tool, spring task, which can be compared to a lightweight quartz, and it's simple to use, except that spring-related packages do not require additional packages.
The following two ways to implement the spring timer function, One is based on the XML configuration , one is based on the annotation of the way , we choose according to their own projects appropriate.
One: Based on the way XML is configured
1: Writing the Ordinary Pojo class
Package Com.aflyun.web.task; Import org.springframework.stereotype.Component; Import Org.springframework.stereotype.Service; @Component // publicclass taskcool { /** * First timer test method */public void testjob () { System.out.println ("test First taskjob .... "); }}
2: Configuration XML file
<?XML version= "1.0" encoding= "UTF-8"?><BeansXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:tx= "Http://www.springframework.org/schema/tx"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:task= "Http://www.springframework.org/schema/task"xmlns= "Http://www.springframework.org/schema/beans"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd HTTP://WWW.SP RINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframewo Rk.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd Http://www.springframew Ork.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd "> <Context:component-scanBase-package= "Com.aflyun.web" /> <Aop:aspectj-autoproxyProxy-target-class= "true" /> <Context:annotation-config/> <!--Configure in Applicationcontext.xml, use the name of the Timer Ref:pojo class method: The name of the call cron:cronexpression expression cron= "0/5 * * * *?" Represents five seconds to execute once - <Task:scheduled-tasks> <task:scheduledref= "Taskcool"Method= "Testjob"Cron= "0/5 * * * *?"/> </Task:scheduled-tasks></Beans>
Note : The namespace and schema of the task must be added to the main configuration file above.
above ref= "Taskcool", the default is the Taskcool class of the first letter lowercase value , if need to modify can be modified in the @component,
For example, below
The @Component ("Taskcooljob") is ref= "Taskcooljon".
This is done based on the XML configuration, and you can see the effect on the run!
Two: annotation -based approach
It is much easier to use annotations without having to write a task class and configure it in an XML file. Using Spring's @Scheduled, let's take a look at the definition of @scheduled in the source file :
@Target ({Java.lang.annotation.ElementType.METHOD, Java.lang.annotation.ElementType.ANNOTATION_TYPE}) @Retention (retentionpolicy.runtime) @Documented public @interface Scheduled { public abstract String cron (); public abstract long Fixeddelay (); public abstract long Fixedrate (); }
Cron: indicates the specified cron expression.
Fixeddelay: represents the interval from the beginning of the previous task completion to the start of the next task, in milliseconds.
Fixedrate: represents the interval from the beginning of the previous task to the start of the next task, in milliseconds.
The following is a specific configuration process:
1: Writing the Pojo class
Package Com.tclshop.cms.center.web.task; Import org.springframework.scheduling.annotation.Scheduled; Import org.springframework.stereotype.Component; @Component Public class webtask { // every five seconds @Scheduled (cron = "0/5 * * * * *?") ) publicvoid taskjob () { System.out.println ("test second Annotation style ... "); }
2: Configuration XML file
The relevant configuration file content is posted below:
<!-- This configuration is enabled for spring to recognize @scheduled annotations - < Scheduler= "Qbscheduler" mode= "proxy"/> <id= "Qbscheduler" pool-size= "Ten"/>
Note: Theoretically only need to add this sentence configuration can be, other parameters are not necessary.
The configuration is complete, the operation can see the effect!
Summary: The use of this timer, do not need to integrate other parent class timers, easy to use! The function is also very powerful!
Attached: Configuration instructions for cronexpression
Field Allow value allowed special characters sec 0-59 ,-*/min 0-59 ,-*/hour 0-23 ,-*/Date 1-31 ,-*?/L W C month 1-12 or Jan-dec ,-*/week 1-7 or Sun-sat ,-*?/L C #年 (optional) Leave blank, 1970-2099 ,-*/
Example:
cron expression " 0 0 12 * *? " "0 * * *" "0 15 10 * *?" "0 * * * *" "0 * * * * * * * * * * * * * * * * * * * * * * * * 2005" 0 * 14 * * "0 0/5 14 * *?" "0 0/5 14,18 * *?" "0 0-5 14 * *?" "0 10,44 3 WED" "0" * mon-fri "
Timer implementation in spring