基於spring+quartz開發定時器

基於spring+quartz開發定時器

一、 準備Jar包
       在Spring全部包齊全的前提下還要導入一個定時器工具包:quartz-1.6.2.jar < http://www.opensymphony.com/quartz/download.action>
二、 開發定時器類,實例代碼以下:
 1 public   class  TriggerUtil  {
 2
 3    private TriggerUtil(){
 4        
 5    }

 6    
 7    public void expDataBase(){
 8        System.out.println("trigger actived..");
 9    }

10    
11}
三、 配置定時任務
     爲了清晰代碼結構,單獨創建一個配置定時任務的配置文件context-trigger.xml,並在applicationContext.xml進行import:
< import  resource ="context-trigger.xml" />
    
     context-trigger.xml內容以下:
 1 <? xml version="1.0" encoding="UTF-8" ?>
 2 < beans
 3      xmlns ="http://www.springframework.org/schema/beans"
 4     xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
 6      <!--  定時器配置  -->
 7 <!--  配置定時器類  -->
 8    < bean  id ="triggerUtil"  class ="com.pro.base.util.TriggerUtil"   >
 9    </ bean >
10    <!--  指定任務(方法)  -->
11    < bean  id ="expDataBaseJob"
12       class ="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
13        < property  name ="targetObject" >
14            < ref  local ="triggerUtil"   />
15        </ property >
16        < property  name ="targetMethod" >
17            < value > expDataBase </ value >
18        </ property >
19    </ bean >
20    <!--  設定計劃執行時間  -->
21    < bean  id ="expDataBaseTrigger"
22       class ="org.springframework.scheduling.quartz.CronTriggerBean" >
23        < property  name ="jobDetail" >
24            < ref  local ="expDataBaseJob"   />
25        </ property >
26        < property  name ="cronExpression" >
27            < value > 00 33 21 * * ? </ value >
28        </ property >
29        </ bean >
30        <!--  任務執行器配置  -->
31        < bean  class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
32          < property  name ="triggers" >
33              < list >
34                  < ref  local ="expDataBaseTrigger"   />
35              </ list >
36          </ property >
37      </ bean >
38 </ beans >

附:定時時間配置說明
Expression    Meaning                                                                                                                                                                                      
0 0 12 * * ?     天天中午12點觸發【Fire at 12pm (noon) every day 】                                                                                                                                           
0 15 10 ? * *     天天上午10:15觸發【Fire at 10:15am every day 】                                                                                                                                              
0 15 10 * * ?     天天上午10:15觸發【Fire at 10:15am every day 】                                                                                                                                              
0 15 10 * * ? *     天天上午10:15觸發【Fire at 10:15am every day 】                                                                                                                                              
0 15 10 * * ? 2005     在2005這一年中天天上午10:15觸發【Fire at 10:15am every day during the year 2005 】                                                                                                           
0 * 14 * * ?     天天下午14:00到15:00之間,每1分鐘觸發一次【Fire every minute starting at 2pm and ending at 2:59pm, every day 】                                                                             
0 0/5 14 * * ?     天天下午14:00到14:55之間,每5分鐘觸發一次【Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day 】                                                                          
0 0/5 14,18 * * ?     天天的14:00~14:55和18:00~18:55之間,每5分鐘觸發一次【Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day 】 
0 0-5 14 * * ?     天天的14:00~14:05之間,每1分鐘觸發一次【Fire every minute starting at 2pm and ending at 2:05pm, every day 】                                                                                
0 10,44 14 ? 3 WED     3月的每週三的14:10和14:44觸發【Fire at 2:10pm and at 2:44pm every Wednesday in the month of March. 】                                                                                       
0 15 10 ? * MON-FRI     每週週一到週五的10:15觸發【Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday 】                                                                                         
0 15 10 15 * ?    每個月15日的10:15觸發【 Fire at 10:15am on the 15th day of every month 】                                                                                                                     
0 15 10 L * ?     每個月最後一天的10:15觸發【Fire at 10:15am on the last day of every month 】                                                                                                                  
0 15 10 ? * 6L     每個月的最後一個週五的10:15觸發【Fire at 10:15am on the last Friday of every month 】                                                                                                         
0 15 10 ? * 6L 2002-2005     在2002到2005之間,每個月的最後一個週五的10:15觸發【Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005 】                                          
0 15 10 ? * 6#3     每個月的第三個星期五的10:15觸發【Fire at 10:15am on the third Friday of every month 】                

基於spring+quartz開發定時器

一、 準備Jar包
       在Spring全部包齊全的前提下還要導入一個定時器工具包:quartz-1.6.2.jar < http://www.opensymphony.com/quartz/download.action>
二、 開發定時器類,實例代碼以下:
 1 public   class  TriggerUtil  {
 2
 3    private TriggerUtil(){
 4        
 5    }

 6    
 7    public void expDataBase(){
 8        System.out.println("trigger actived..");
 9    }

10    
11}
三、 配置定時任務
     爲了清晰代碼結構,單獨創建一個配置定時任務的配置文件context-trigger.xml,並在applicationContext.xml進行import:
< import  resource ="context-trigger.xml" />
    
     context-trigger.xml內容以下:
 1 <? xml version="1.0" encoding="UTF-8" ?>
 2 < beans
 3      xmlns ="http://www.springframework.org/schema/beans"
 4     xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
 6      <!--  定時器配置  -->
 7 <!--  配置定時器類  -->
 8    < bean  id ="triggerUtil"  class ="com.pro.base.util.TriggerUtil"   >
 9    </ bean >
10    <!--  指定任務(方法)  -->
11    < bean  id ="expDataBaseJob"
12       class ="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
13        < property  name ="targetObject" >
14            < ref  local ="triggerUtil"   />
15        </ property >
16        < property  name ="targetMethod" >
17            < value > expDataBase </ value >
18        </ property >
19    </ bean >
20    <!--  設定計劃執行時間  -->
21    < bean  id ="expDataBaseTrigger"
22       class ="org.springframework.scheduling.quartz.CronTriggerBean" >
23        < property  name ="jobDetail" >
24            < ref  local ="expDataBaseJob"   />
25        </ property >
26        < property  name ="cronExpression" >
27            < value > 00 33 21 * * ? </ value >
28        </ property >
29        </ bean >
30        <!--  任務執行器配置  -->
31        < bean  class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
32          < property  name ="triggers" >
33              < list >
34                  < ref  local ="expDataBaseTrigger"   />
35              </ list >
36          </ property >
37      </ bean >
38 </ beans >

附:定時時間配置說明
Expression    Meaning                                                                                                                                                                                      
0 0 12 * * ?     天天中午12點觸發【Fire at 12pm (noon) every day 】                                                                                                                                           
0 15 10 ? * *     天天上午10:15觸發【Fire at 10:15am every day 】                                                                                                                                              
0 15 10 * * ?     天天上午10:15觸發【Fire at 10:15am every day 】                                                                                                                                              
0 15 10 * * ? *     天天上午10:15觸發【Fire at 10:15am every day 】                                                                                                                                              
0 15 10 * * ? 2005     在2005這一年中天天上午10:15觸發【Fire at 10:15am every day during the year 2005 】                                                                                                           
0 * 14 * * ?     天天下午14:00到15:00之間,每1分鐘觸發一次【Fire every minute starting at 2pm and ending at 2:59pm, every day 】                                                                             
0 0/5 14 * * ?     天天下午14:00到14:55之間,每5分鐘觸發一次【Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day 】                                                                          
0 0/5 14,18 * * ?     天天的14:00~14:55和18:00~18:55之間,每5分鐘觸發一次【Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day 】 
0 0-5 14 * * ?     天天的14:00~14:05之間,每1分鐘觸發一次【Fire every minute starting at 2pm and ending at 2:05pm, every day 】                                                                                
0 10,44 14 ? 3 WED     3月的每週三的14:10和14:44觸發【Fire at 2:10pm and at 2:44pm every Wednesday in the month of March. 】                                                                                       
0 15 10 ? * MON-FRI     每週週一到週五的10:15觸發【Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday 】                                                                                         
0 15 10 15 * ?    每個月15日的10:15觸發【 Fire at 10:15am on the 15th day of every month 】                                                                                                                     
0 15 10 L * ?     每個月最後一天的10:15觸發【Fire at 10:15am on the last day of every month 】                                                                                                                  
0 15 10 ? * 6L     每個月的最後一個週五的10:15觸發【Fire at 10:15am on the last Friday of every month 】                                                                                                         
0 15 10 ? * 6L 2002-2005     在2002到2005之間,每個月的最後一個週五的10:15觸發【Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005 】                                          
0 15 10 ? * 6#3     每個月的第三個星期五的10:15觸發【Fire at 10:15am on the third Friday of every month 】                
相關文章
相關標籤/搜索