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

Source: Internet
Author: User

1. Spring configuration file

[HTML]View PlainCopy
  1. <beans xmlns="Http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:task="Http://www.springframework.org/schema/task"
  5. xmlns:context="Http://www.springframework.org/schema/context"
  6. xmlns:aop="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
  7. xsi:schemalocation= "Http://www.springframework.org/schema/beans
  8. Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  10. Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
  11. Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  12. HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  13. Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd " >
  14. <task:annotation-driven /> <!--timer switch --
  15. <Bean id="Mytaskxml" class="com.spring.task.MyTaskXml"></Bean >
  16. <task:scheduled-tasks>
  17. <!--
  18. This represents the execution every five seconds.
  19. -
  20. <task:scheduled ref="Mytaskxml" method="show" cron="*/5 * * * * *?" />
  21. <task:scheduled ref="Mytaskxml" method="print" cron="*/10 * * * * *?" />
  22. </task:scheduled-tasks>
  23. <!--automatically scanned package name--
  24. <context:component-scan base-package="Com.spring.task" />
  25. </Beans>


2. XML-based Timer task

[Java]View PlainCopy
  1. Package com.spring.task;
  2. /**
  3. * XML-Based Timers
  4. * @author HJ
  5. */
  6. Public class Mytaskxml {
  7. public Void Show () {
  8. System.out.println ("Xml:is show Run");
  9. }
  10. public void print () {
  11. System.out.println ("xml:print run");
  12. }
  13. }


3, note-based timer task

[Java]View PlainCopy
  1. Package com.spring.task;
  2. Import org.springframework.scheduling.annotation.Scheduled;
  3. Import org.springframework.stereotype.Component;
  4. /**
  5. * Note-Based Timers
  6. * @author HJ
  7. */
  8. @Component
  9. Public class Mytaskannotation {
  10. /**  
  11. * Timed calculation. Once daily 01:00
  12. */
  13. @Scheduled (cron = "0 0 1 * * *")
  14. public Void Show () {
  15. System.out.println ("Annotation:is show Run");
  16. }
  17. /**  
  18. * Heartbeat update. Executes once at startup and then executes every 2 seconds
  19. */
  20. @Scheduled (fixedrate = 1000*2)
  21. public void print () {
  22. System.out.println ("annotation:print run");
  23. }
  24. }


4. Testing

[Java]View PlainCopy
  1. Package com.spring.test;
  2. Import Org.springframework.context.ApplicationContext;
  3. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  4. Public class Main {
  5. public static void Main (string[] args) {
  6. ApplicationContext CTX = new Classpathxmlapplicationcontext ("Spring-mvc.xml");
  7. }
  8. }

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

Project: http://download.csdn.NET/detail/wxwzy738/7305741

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

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.