Import two packages first
1 <Dependency>2 <groupId>Org.quartz-scheduler</groupId>3 <Artifactid>Quartz</Artifactid>4 <version>2.2.2</version>5 </Dependency>6 <Dependency>7 <groupId>Org.quartz-scheduler</groupId>8 <Artifactid>Quartz-jobs</Artifactid>9 <version>2.2.2</version>Ten </Dependency>
Then write a class used as the timer class, and write a method for the timer to execute the method:
1 PackageCom.practice.prac.service.Impl;2 3 Importorg.springframework.stereotype.Component;4 5 @Component6 Public classTesttime {7 8 Public voidprint () {9System.out.println ("----------");Ten } One}
Here is the Testtime class and the Print method that defines the following beans in the XML file:
1 <!--The following is the Quartz implementation timer -2 3 <!--indicates the class as a timer -4 <BeanID= "Testtime"class= "Com.practice.prac.service.Impl.TestTime"></Bean>5 <!--Configure the corresponding information -6 <BeanID= "Testtimedetail"7 class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">8 <!--Specifying task Classes -9 < Propertyname= "TargetObject"ref= "Testtime" />Ten <!--specify how the task executes - One < Propertyname= "Targetmethod"value= "Print" /> A </Bean> - <!--Configuring triggers - - <BeanID= "Testtimetrigger" the class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean"> - < Propertyname= "Jobdetail"ref= "Testtimedetail" /> - <!--run once every 5 seconds - - < Propertyname= "Cronexpression"value= "0/5 * * * *?" /> + </Bean> - + <Beanclass= "Org.springframework.scheduling.quartz.SchedulerFactoryBean"> A < Propertyname= "Triggers"> at <List> - <!--<ref bean= "Examplejobtrigger"/> - - <refBean= "Testtimetrigger" /> - </List> - </ Property> - </Bean>
Where the name corresponds to ref, specify the task class and the method that specifies the task execution. The timing trigger is defined at the cronexpression of the trigger, and the specific expression meaning is in another article.
Using quartz to implement timer functions