Two popular spring timer configurations: Java's Timer class and Opensymphony's quartz

Source: Internet
Author: User

1.Java Timer first inherits the Java.util.TimerTask class to implement the Run method import Java.util.TimerTask;     public class Emailreporttask extends timertask{@Override public void Run () {...} } in Spring definition ...   Configure Spring Timer <bean id= "Schedulereporttask" class= "Org.springframework.scheduling.timer.ScheduledTimerTask" > <property name= "TimerTask" ref= "Reporttimertask"/> <property name= "period" > <value>86400000value&   Gt Property> bean> TimerTask Property tells Scheduledtimertask which to run. 86400000 for 24 hours start Spring Timer Spring's Timerfactorybean is responsible for starting the scheduled task <bean class= " Org.springframework.scheduling.timer.TimerFactoryBean "> <property name=" scheduledtimertasks "> <list&   Gt;<ref bean= "Schedulereporttask"/>list> property> bean> scheduledtimertasks displays a list of timer tasks that need to be started. You can delay startup by setting the Delay property <bean id= "Schedulereporttask" class= "Org.springframework.scheduling.timer.ScheduledTimerTask "> <property name=" timertask "ref=" Reporttimertask "/>   <property name= "period" > <value>86400000value> property> <property name= "delay" > <va lue>3600000value> property> bean> This task we can only specify to run every 24 hours, can not be accurate to a certain time to start 2.   The quartz timer first inherits the Quartzjobbean class to implement the Executeinternal method import Org.quartz.JobExecutionContext;   Import org.quartz.JobExecutionException;    Import Org.springframework.scheduling.quartz.QuartzJobBean; public class Emailreportjob extends quartzjobbean{protected void executeinternal (Jobexecutioncontext arg0) throws Job   executionexception {...} } in spring, define <bean id= "Reportjob" class= "Org.springframework.scheduling.quartz.JobDetailBean" > <property Name= "Jobclass" > <value>EmailReportJobvalue> property> <property name= "Jobdataasmap" > &lt ;map> <entry key= "Courseservice" > <ref bean= "courseservice"/> ENTRY&G       T Map> property> bean> Here we do not directly declare a emailreportjob bean, but declare aA jobdetailbean. This is the characteristic of quartz. Jobdetailbean is a subclass of Quartz's org.quartz.JobDetail, which requires a job object to be set through the Jobclass property. Another special point in using Quartz's jobdetail is that the Courseservice property of Emailreportjob is indirectly set. The Jobdataasmap property of Jobdetail accepts a map, including various properties set to Jobclass, when. When Jobdetailbean is instantiated, it injects the courseservice bean into the Courseservice attribute of the emailreportjob. The Org.quartz.Trigger class that starts the timer quartz describes when and how often to run a quartz job. Spring provides two triggers Simpletriggerbean and Crontriggerbean. Simpletriggerbean is similar to Scheduledtimertasks. Specifies the frequency of execution of the work, mimicking the scheduledtimertasks configuration. <bean id= "Simplereporttrigger" class= "   Org.springframework.scheduling.quartz.SimpleTriggerBean "> <property name=" jobdetail "ref=" Reprotjob "/>        <property name= "Startdelay" > <value>360000value> property> <property name= "Repeatinterval" > <value>86400000value> property> bean> Startdelay is also delayed 1 hours startCrontriggerbean Specify the exact run time of the work <bean id= "Cronreporttrigger" class= " Org.springframework.scheduling.quartz.CronTriggerBean "> <property name=" jobdetail "ref=" Reprotjob "/> < Property name= "Cronexpression" > <value>0 0 6 * *?value> property> bean> attribute cronexpression tells when to trigger. The most mysterious is the cron expression: The Linux system's scheduled tasks usually have cron to bear. A cron expression has at least 6 (or possibly 7) time elements that have a space separation. From left to right: 1. Seconds 2.3. Hours 4. Date in the month (1-31) 5. Month (1-12 or JAN-DEC) 6. The date of the week (1-7 or Sun-sat) 7. Year (1970-2099) Each element displays a specified value (such as 6), A range (9-12), a list (9,11,13), or a wildcard character (*). Because 4 and 6 of these two elements are mutually exclusive, you should set a question mark (? ) to indicate the field that you do not want to set, "/" if the combination of values represents the number of repetitions (10/6 means repeat 6 times every 10 seconds). Start timer <bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" > <property name= "triggers" > <list><ref bean= "Cronreporttrigger"/>list> property> bean> triggers property accepts a set of triggers. 

A concrete example of the Crontriggerbean

<!--automatically confirm maintenance (remove stock)--

<bean id= "Autoconfirmmaintain" class= "Com.geosoft.webtp.service.maintainaccount.AutoConfirmMaintain" >

<property name= "Maintaindao" ref= "Maintaindaoimpl" ></property>

<property name= "Maintaincommdao" ref= "Maintaincommdaoimpl" ></property>

</bean>

<bean id= "Autoconfirmjobdetail" class= " Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">

<property name= "TargetObject" ><ref bean= "Autoconfirmmaintain" ></ref></property>

<property name= "Targetmethod" ><value>autoConfirmNC</value></property>

</bean>

<bean id= "Autoconfirmmtrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >

<property name= "Jobdetail" ><ref bean= "Autoconfirmjobdetail"/></property>

<property name= "Cronexpression" ><value>00 xx 1 *?</value></property>

</bean>

Two popular spring timer configurations: Java's Timer class and Opensymphony's quartz

Related Article

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.