1. 程式人生 > >spring整合quartz定時任務(註解實現)

spring整合quartz定時任務(註解實現)

必備jar:quartz-1.6.5.jar、commons-collections-3.2.jar、commons-logging-1.1.jar


//applicationContext.xml增加


xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd


<!-- 平臺提供的預設元件 -->
<context:component-scan base-package="com.quartz.*" />


<!-- 定時任務註解開啟 -->
<task:annotation-driven/> 




//業務介面
package com.quartz.test;


public interface IMyTestService {
    public void myTest(); 
}


//業務介面實現類
package com.quartz.test;


import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;




@Component
public class MyTestServiceImpl implements IMyTestService{
    
    @Scheduled(cron="0/5 * *  * * ? ")   //每5秒執行一次
    @Override
    public void myTest() {
        System.out.println("進入測試");  
    }


}




其他內容:
"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觸發