Two ways to implement spring timer in Java _java

Source: Internet
Author: User

There are currently two popular spring timer configurations: TheJava Timer class and the Opensymphony quartz.

1.Java Timer Timing

First, inherit the Java.util.TimerTask class to implement the Run method

Import Java.util.TimerTask;  
public class Emailreporttask extends timertask{  
  @Override public  
  void Run () {  
    ...  
  }   
}

In the spring definition

...

Configure Spring Timer

<bean id= "Schedulereporttask" class= "Org.springframework.scheduling.timer.ScheduledTimerTask" >  
< Property Name= "TimerTask" ref= "Reporttimertask"/> <property name=  
"period" >  
<value> 86400000value>  
property>  

The TimerTask property tells Scheduledtimertask which to run. 86400000 represents 24 hours

Start Spring Timer

Spring's Timerfactorybean is responsible for starting timed tasks

<bean class= "Org.springframework.scheduling.timer.TimerFactoryBean" >  
<property name= " Scheduledtimertasks ">  
  <list><ref bean=" Schedulereporttask "/>list>  
property>  

A list of timer tasks that need to be started is displayed in the Scheduledtimertasks.

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" >  
<value>3600000value>  
Property>  
bean>  

We can only set the task to run every 24 hours and not be able to start at a certain time.

2.Quartz Timer

First, inherit Quartzjobbean class to implement 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 Jobexecutionexception {  
...}}  

Defined in spring

<bean id= "Reportjob" class= "Org.springframework.scheduling.quartz.JobDetailBean" >  
<property name= " Jobclass ">  
<value>EmailReportJobvalue>  
property>  
<property name=" Jobdataasmap " >  
  <map>  
    <entry key= "Courseservice" >  
      <ref bean= "Courseservice"/>  
      entry >  
  map>  
property>  

Here we do not declare a emailreportjob Bean directly, but declare a jobdetailbean. This is the characteristic of quartz. Jobdetailbean is a subclass of the quartz org.quartz.JobDetail that requires a job object to be set through the Jobclass property.

Another special in the jobdetail that uses quartz is that the Courseservice property of Emailreportjob is set indirectly. The Jobdetail jobdataasmap attribute accepts a map, including the various properties set to the Jobclass, when. When Jobdetailbean is instantiated, it injects the courseservice bean into the Courseservice property of Emailreportjob.

Start timer

The Quartz Org.quartz.Trigger class describes when and how frequently a quartz job is run. Spring provides two triggers Simpletriggerbean and Crontriggerbean.

The Simpletriggerbean is similar to Scheduledtimertasks. Specifies how frequently the work is performed, 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 to start

Crontriggerbean specify the exact running 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>  

Property Cronexpression tells when to trigger. The most mysterious of all is cron expression:

The planning tasks for Linux systems are usually cron-borne. A cron expression has a space-delimited time element of at least 6 (or possibly 7). From left to right:

1. Seconds 2 minutes 3 hours 4. Month (1-31) 5. Month (1-12 or JAN-DEC) 6. Days of the week (1-7 or Sun-sat) 7. Year (1970-2099)
Each element displays a specified value (such as 6), an interval (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 indicates 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>  

The Triggers property accepts a set of triggers.

Well, this article is finished, write a good bar, there are deficiencies, you are welcome to make valuable suggestions.

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.