Spring comes with timed task features, based on annotations and XML configuration

Source: Internet
Author: User
Tags aop xmlns
1. Spring 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> <!--This is done every five seconds--<task:scheduled ref= "Mytaskxml" method= "show" cron = "*/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, note-based timer task

    Package com.spring.task;  
    Import org.springframework.scheduling.annotation.Scheduled;  
    Import org.springframework.stereotype.Component;  
     * Note-based timer 
     * @author HJ */  
    @Component Public  
    class Mytaskannotation {  
         * timed calculation. Once daily 01:00 *  
        @Scheduled (cron = "0 0 1 * * *") public   
        Void Show () {  
            System.out.println ("Annotatio N:is show Run ");  
         * Heartbeat update. Executes once at startup, and executes once 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");  

Operation 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 Execution two times bug reference link: 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.