Springmvc Scheduled Task configuration in the

Source: Internet
Author: User

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

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.