Java Timer 2-spring Implementation

Source: Internet
Author: User

    • Spring Timers (XML-based)
    • Spring timers (based on annotations)
    • Quartz Timer
1. Using the spring timer based on XML configuration, first write the timed task class MyTask
 Public class MyTask {    publicvoid  execute () {    System.out.println (" Spring timed tasks based on XML configuration! ");   }  }  
Then add the xmlns:task= "Http://www.springframework.org/schema/task" namespace to the spring configuration file
<Task:annotation-driven/>  <BeanID= "MyTask"class= "Com.pptv.vipbackend.controller.Mytask"/>  <Task:scheduled-tasks>  <task:scheduledref= "MyTask"Cron= "*/5 * * * *?"Method= "Print"/><!--executes every 5 seconds -  </Task:scheduled-tasks> 

Of course, timed task classes are added to spring's management

<base-package= "Com.simonsfan.study.controller.Mytask">  
Execution can see the effect 2, the use of annotations based on the spring timer based on annotations will be relatively simple, directly write task class MyTask:
@EnableScheduling  @Component  publicclass  mytask {    = "*/5 * * * * *?" Publicvoid  execute () {      System.out.println ("Spring timed task based on annotation configuration! )     ");    }  }  

Of course, task classes are also included in spring management

<base-package= "Com.simonsfan.study.controller.Mytask">  <task:annotation-driven/>
Launch can see the same effect 3, quartz timer, its performance and flexibility are better than the JDK TimerTask class Pom file added
<Dependency>  <groupId>Org.quartz-scheduler</groupId>  <Artifactid>Quartz</Artifactid>  <version>2.2.2</version>  </Dependency> 

Timed Task Class MyTask:

@Component    Public class MyTask {    publicvoid  execute () {      System.out.println (" Implement timed tasks based on Spring+quartz! ");;    }  }  

In the spring configuration file, add:

<BeanID= "Jobbean"class= "Com.pptv.vipbackend.controller.Mytask"/>    <BeanID= "Myjobdetail"class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  < Propertyname= "TargetObject">  <refBean= "Jobbean"/>  </ Property>  < Propertyname= "Targetmethod">  <value>Execute</value><!--method names in a task class -  </ Property>  <!--set concurrency to False -  < Propertyname= "Concurrent"value= "false" />  </Bean>  <BeanID= "Simpletrigger"class= "Org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">  < Propertyname= "Jobdetail"ref= "Myjobdetail" />  < Propertyname= "Startdelay"value= "+" />  < Propertyname= "Repeatinterval"value= "$" />  </Bean>    <!--General Management Class if you lazy-init= ' false ' then the container starts executing the scheduler -  <BeanID= "Startquertz"class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean"Lazy-init= "false"Autowire= "No">  < Propertyname= "Triggers">  <List>  <!--Job Scheduler, List can join other scheduler -  <refBean= "Simpletrigger" />  </List>  </ Property>  </Bean>  
In this way, the time expression in the scheduled task of scheduled tasks can be implemented based on Spring + Quartz:
Order: Seconds Minutes Hours Date Month Week Year (optional)
Value: 0-59 0-59 0-23 1-30 (31) 1-12 1-7
Allow special characters: , - * / , - * / , - * / , - * / ? L W C , - * / ,-*/L # C 1970-2099,-*/

Field meaning

*: Represents all possible values  -: Specified range  ,: List enumeration  For example in minutes, "5,15" means 5 minutes and 20 minutes trigger  /: Specify increments  for example in minutes, "3/15" means starting from 3 minutes, not every 15 minutes  ?: Indicates no specific value, use? Be aware of conflicts  L: Last, for example, 7 or sat in the month, 31 or 30,6l for the end of the month, Fril represents the last Friday of the month.  W: Can only be used in the month, representing the day of the week that is closest to the specified day  #: only in the weeks  The first few weeks of the month, for example, 6#3 represents the 3rd Friday of this month, 0 * * * *? trigger once every    1 minutes  0 0 * * * Every 1 hours per day 0  0 10 * * * 10 triggers once a day 0  * 14 * * ? Every 1 minutes from 2 o'clock in the afternoon to 2:59 every day   0 30 9 1 *? 1th per month 9:30 A.M.  0 15 10 15 * Monthly 15th 10:15 Trigger */5 * * * * *  every 5 seconds to execute  0 */1 * * *? Execute once every 1 minutes  0 0 5-15 * * 5-15 dots per day trigger  0 0/3 * * *? trigger every three minutes  

Java Timer 2-spring Implementation

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.