Spring Comes with timer

Source: Internet
Author: User

Http://www.cnblogs.com/pengmengnan/p/6714203.html

Note Mode Spring Timer
1, first configure our Spring.xml
xmlns add the following content,
xmlns:task= "http://www.springframework.org/ SCHEMA/TASK&NBSP
 
and xsi:schemalocation Add the following,
1. Http://www.springframework.org/schema/task  
2. 
http://www.springframework.org/schema/task/spring- 
task-3.1.xsd 
2, Task Task Scan note
<task:annotation-driven/>
3, the configuration scan location is:
<context:annotation-config/> 
<bean 
class= "ORG.SPRINGFRAMEWORK.BEANS.FACTORY.ANNOTATION.AU&NBSP;
Towiredannotationbeanpostprocessor "/>&NBSP;
<context:component-scan base- 
package=" Com.jk.spring "/>&NBSP;
4, write an interface
public interface Springhorodateuri {
public void myTest ();  
}
5, interface implementation class
//import 
org.springframework.scheduling.annotation.Scheduled;
Import org.springframework.stereotype.Component;

@Component//import
Org.springframework.stereotype.Component;
public class Springhorodateur implements
springhorodateuri{

@Scheduled (cron= "0/5 * * * *?) ")//every 5 seconds to perform a
Times
@Override
public void MyTest () {
System.out.println (">>>>>>>>>>>>> entry test
Try it! ");
}

}

Complete!
A few things to note:
1, spring @scheduled annotations need to write on the implementation,
2, the Timer task method can not have a return value (if there is a return value, spring initial
Will tell you that there is a mistake, you need to set a Proxytargetclass
A value of true, specific to Baidu Google bar)
3, the implementation of the class to have component annotations @component


Timer for configuration file format
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/c
Ontext "

xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"


Xsi:schemalocation= "Http://www.springframework.org/sch
Ema/beans

http://www.springframework.org/schema/beans/spring-
Beans-3.0.xsd
Http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-
3.0.xsd
Http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-
3.0.xsd
Http://www.springframework.org/schema/context
http://www.springframework.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:annotation-driven/> <!--timer Switch--
<bean id= "Mytaskxml"
class= "COM.SPRING.TASK.MYTASKXML" ></bean>
<task:scheduled-tasks>
<!--
This represents the execution every five seconds.
-
<task:scheduled ref= "Mytaskxml" method= "show"
cron= "*/5 * * * * *?"/>
<task:scheduled ref= "Mytaskxml" method= "print"
cron= "*/10 * * * * *?" />
< differences between!--and annotations--
</task:scheduled-tasks>
<!--automatically scanned package name--
<context:component-scan base-
Package= "Com.spring.task"/>

</beans>
2. XML-based Timer task
/**
* XML-Based Timers
* @author HJ
*/
public class Mytaskxml {
public void Show () {
System.out.println ("Xml:is show Run");
}

public void print () {
System.out.println ("Xml:print run");
}
}

Cron expression meaning
"0 0 12 * *?" trigger 12 o'clock noon every day.
"0 15 10?" * * "trigger 10:15 every day"
"0 15 10 * *?" Daily 10:15 Trigger
"0 15 10 * *?" * "10:15 per day" trigger
"0 15 10 * *?" 2005 "2005-year daily 10:15 Trigger
"0 * 14 * *?" daily from 2 o'clock in the afternoon to 2:59 one touch per minute
Hair
"0 0/5 14 * *?" daily from 2 o'clock in the afternoon to 2:55 minutes end every 5 mins
One trigger
"0 0/5 14,18 * *?" daily from 2 o'clock in the afternoon to 2:55 and 6 to 6 55
Trigger every 5 minutes in two time periods
"0 0-5 14 * *?" daily from 14:00 to 14:05 per minute trigger
"0 10,44 14?" 3 WED "March of 14:10 and 14:44 triggers per Wednesday
"0 15 10?" * Mon-fri "Every Monday, Tuesday, Wednesday, Thursday, Friday
The 10:15 Trigger

Spring Comes with timer

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.