It is common to use timed tasks in a project, such as synchronizing or backing up data on a daily basis.
Prior to the development of C language, timed tasks were dispatched by writing a shell script and then adding it to a Linux timed task.
Now that you've used SPRINGMVC, it's easier to get together O (∩_∩) o
There are two ways to configure, I will tell each other, but after watching you must only choose the back of the kind, yes! I was in the back way.
The first configuration method: This is more complex, the configuration of the place a bit more, a little attention is not successful, the specific look at the code
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.1.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd "> <!--re-fetch tokens every one hours - <!--1. The work class to invoke - <BeanID= "Refreshaccesstokenjob"class= "Com.xiao.weixin.quartz.RefreshAccessToken"/> <!--2. Define methods for calling objects and calling objects - <BeanID= "Refreshaccesstokentask"class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!--the class that is called - < Propertyname= "TargetObject"> <refBean= "Refreshaccesstokenjob"/> </ Property> <!--calling methods in a class - < Propertyname= "Targetmethod"> <value>Work</value> </ Property> </Bean> <!--3. Define the trigger time - <BeanID= "Refreshaccesstoken"class= "Org.springframework.scheduling.quartz.CronTriggerBean"> < Propertyname= "Jobdetail"> <refBean= "Refreshaccesstokentask"/> </ Property> <!--cron-expression - < Propertyname= "Cronexpression"> <value>0 0 */2 * *?</value> </ Property> </Bean> <!--4. Total Management class If you lazy-init= ' false ' then the container starts executing the scheduler - <BeanID= "Startquertz"Lazy-init= "false"Autowire= "No"class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean"> < Propertyname= "Triggers"> <List> <refBean= "Refreshaccesstoken"/> </List> </ Property> </Bean></Beans>
The second type of configuration: highly recommended
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:task= "Http://www.springframework.org/schema/task"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <Task:scheduled-tasks> <task:scheduledref= "Quartztestbean"Method= "Quartzjobtestmethod"Cron= "*/5 * * * *?" /> </Task:scheduled-tasks> <BeanID= "Quartztestbean"class= "Xiaochangwei.zicp.net.service.QuartzTestServiceImpl"/></Beans>
In this case, it is very simple to call the service interface implementation of the method in the class can be,
For example, my implementation class is this:
package Xiaochangwei.zicp.net.service; import java.util.Date; import Org.springframework.stereotype.Service; import Xiaochangwei.zicp.net.common.tools.DateUtil; @Service public class Quartztestserviceimpl implements Quartztestservice { public void Span style= "color: #000000;" > Quartzjobtestmethod () {System.out.println ( "timed Task execution:" + dateutil.getformatdate (new Date ())); }}
Print a current time every five seconds
The results of the implementation are as follows:
How often the out-of-configuration task is executed is simple:
<ref= "Quartztestbean" method= "Quartzjobtestmethod" cron = "*/5 * * * *?" />
The above represents a five-second execution.
There are a total of five *, from the point of travel after the seconds, minutes, when ....
How many seconds do once say, say how many points to execute once
cron= "* */2 * * * *?" Are you right?
This is not right, you want to change the previous star to 0 cron= "0 */2 * * *?" for two-point execution
Similarly
Specific rules please Baidu cron so easy !
Scheduled task configuration in Springmvc