Profiles and annotations for spring timed tasks

Source: Internet
Author: User

To add content to a scheduled task profile:

<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://Www.springframework.org/schema/beans/spring-beans-3.0.xsd http://Www.springframework.org/schema/txHttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://Www.springframework.org/schema/jeeHttp://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://Www.springframework.org/schema/contextHttp://www.springframework.org/schema/context/spring-context-3.0.xsd http://Www.springframework.org/schema/aophttp://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:annotation-driven/> <!--timer Switch-- <bean id= "Mytaskxml" class= "com.spring.task.MyTaskXml" ></bean> <task :scheduled-tasks> <!-- This is what it means to do once 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>       

If the scheduled task is a profile:

 Package Com.spring.task; /**  @author*/Publicclass  mytaskxml            {public  void  Show () {        System.out.println ("Xml:is show        Run");  Public void print () {        System.out.println ("xml:print run");}    }

If it is an annotated scheduled task:

 PackageCom.spring.task;Importorg.springframework.scheduling.annotation.Scheduled;Importorg.springframework.stereotype.Component;/*** Note-Based timers *@authorHJ*/@Component Public classmytaskannotation {/*** timed calculation. Once daily 01:00*/@Scheduled (Cron= "0 0 1 * * *")      Public voidShow () {System.out.println ("Annotation:is Show Run"); }        /*** Heartbeat update. Executes once at startup and then executes every 2 seconds*/@Scheduled (fixedrate= 1000*2)      Public voidprint () {System.out.println ("Annotation:print Run"); }}

Test:

 Package com.spring.test; Import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.ClassPathXmlApplicationContext;  Public class Main {    publicstaticvoid  main (string[] args) {          New Classpathxmlapplicationcontext ("Spring-mvc.xml");}    }

Profiles and annotations for spring timed tasks

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.