1. 程式人生 > >spring自帶定時器

spring自帶定時器

author google scheduled xmlns tasks ogl 位置 .org 執行

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

註解模式的spring定時器
1 , 首先要配置我們的spring.xml
xmlns 多加下面的內容、
xmlns:task="http://www.springframework.org/schema/task
"
然後xsi:schemaLocation多加下面的內容、
1. http://www.springframework.org/schema/task
2.
http://www.springframework.org/schema/task/spring-
task-3.1.xsd
2,task任務掃描註解
<task:annotation-driven/>
3,配置掃描位置是:
<context:annotation-config/>
<bean
class="org.springframework.beans.factory.annotation.Au
towiredAnnotationBeanPostProcessor"/>
<context:component-scan base-
package="com.jk.spring"/>
4 ,寫一個接口
public interface SpringHorodateurI {
public void myTest();
}
5,接口實現類
//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 * * * * ? ") //每5秒執行一

@Override
public void myTest() {
System.out.println(">>>>>>>>>>>>>進入測
試!!");
}

}

完成!
需要註意的幾點:
[email protected]

/* */ 需要寫在實現上、
2、 定時器的任務方法不能有返回值(如果有返回值,spring初始
化的時候會告訴你有個錯誤、需要設定一個proxytargetclass的
某個值為true、具體就去百度google吧)
[email protected]


配置文件格式的定時器
1、spring的配置文件
<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 /> <!-- 定時器開關-->
<bean id="myTaskXml"
class="com.spring.task.MyTaskXml"></bean>
<task:scheduled-tasks>
<!--
這裏表示的是每隔五秒執行一次
-->
<task:scheduled ref="myTaskXml" method="show"
cron="*/5 * * * * ?" />
<task:scheduled ref="myTaskXml" method="print"
cron="*/10 * * * * ?"/>
<!-- 和註解的區別-->
</task:scheduled-tasks>
<!-- 自動掃描的包名 -->
<context:component-scan base-
package="com.spring.task" />

</beans>
2、基於xml的定時器任務
/**
* 基於xml的定時器
* @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表達式 含義
"0 0 12 * * ?" 每天中午十二點觸發
"0 15 10 ? * *" 每天早上10:15觸發
"0 15 10 * * ?" 每天早上10:15觸發
"0 15 10 * * ? *" 每天早上10:15觸發
"0 15 10 * * ? 2005" 2005年的每天早上10:15觸發
"0 * 14 * * ?" 每天從下午2點開始到2點59分每分鐘一次觸

"0 0/5 14 * * ?" 每天從下午2點開始到2:55分結束每5分鐘
一次觸發
"0 0/5 14,18 * * ?" 每天的下午2點至2:55和6點至6點55
分兩個時間段內每5分鐘一次觸發
"0 0-5 14 * * ?" 每天14:00至14:05每分鐘一次觸發
"0 10,44 14 ? 3 WED" 三月的每周三的14:10和14:44觸發
"0 15 10 ? * MON-FRI" 每個周一、周二、周三、周四、周五
的10:15觸發

spring自帶定時器