Spring's self-contained timing task, based on annotations and XML configuration

Source: Internet
Author: User
1. Spring's configuration file

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:p=" http://www.springframework.org/schema/p "xmlns:task=" http://www.springframework.org /schema/task "xmlns:context=" Http://www.springframework.org/schema/context "xmlns:aop=" http:// Www.springframework.org/schema/aop "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://ww W.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/tx Http://www.spri Ngframework.org/schema/tx/spring-tx-3.0.xsd Http://www.springframework.org/schema/jee http://www.spring Framework.org/schema/jee/spring-jee-3.0.xsd Http://www.springframework.org/schema/context HTTP://WWW.SP Ringframework.org/schema/context/spring-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http: Www.springframework.org/schema/aop/spring-aop-3.0.xsd Http://www.sprinGframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd "> <task:ann

     Otation-driven/> <!--timer switch--> <bean id= "Mytaskxml" class= "com.spring.task.MyTaskXml" ></bean> <task:scheduled-tasks> <!--Here is the execution of--> <task:scheduled ref= "Mytaskxml" method= "show" cron every five seconds. = "*/5 * * * * *"/> <task:scheduled ref= "mytaskxml" method= "print" cron= "*/10 * * * *?" /> </task:scheduled-tasks> <!--automatically scanned package name--> <context:component-scan base-package= "com . Spring.task "/> </beans>
2, XML-based timer task

Package com.spring.task;  
  
/** 
 * xml-based Timer 
 * @author HJ/Public  
class Mytaskxml {public  
      
      
    void Show () {  
        System.out.println ("Xml:is show Run");  
    }  
      
    public void print () {  
        System.out.println ("Xml:print Run");  
    }  
3, the timer task based on annotations

    Package com.spring.task;  
      
    Import org.springframework.scheduling.annotation.Scheduled;  
    Import org.springframework.stereotype.Component;  
      
    /** 
     * Based on the annotation timer 
     * @author HJ/ 
     @Component public  
    class Mytaskannotation {  
          
        /**  
         * timed calculation. Perform A/@Scheduled every 01:00    
        (cron = "0 0 1 * * *") public   
        Void Show () {  
            System.out.println ("Annotatio N:is show Run ");  
        }  
          
        /**  
         * Heartbeat update. Executes once at startup, and then executes every 2 seconds/    
        @Scheduled (fixedrate = 1000*2) public   
        void print () {System.out.println ()  
            Annotation:print run ");  
        }  
      
4, testing

Package com.spring.test;  
  
Import Org.springframework.context.ApplicationContext;  
Import Org.springframework.context.support.ClassPathXmlApplicationContext;  
  
  
public class Main {public  
    static void Main (string[] args) {  
        ApplicationContext ctx = new Classpathxmlapplicationc Ontext ("Spring-mvc.xml");  
    }  

Run Result:
Annotation:print Run
Annotation:print Run
Annotation:print Run
Xml:print Run
Xml:is Show Run
Annotation:print Run
Annotation:print Run


Spring timed task performs two bug reference links: http://nkliuliu.iteye.com/blog/816335

Reprint Link: http://blog.csdn.net/wxwzy738/article/details/25158787

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.