Spring中定時器實現

在一些工作需要使用到定時器,spring很好的集成了定時器的功能! 
在Spring 中使用Quartz,本文介紹Spring3.0以後自主開發的定時任務工具,spring task,可以將它比作一個輕量級的Quartz,而且使用起來很簡單,除spring相關的包外不需要額外的包, 
下面介紹兩種方式實現Spring定時器功能,一種是基於xml配置方式,一種是基於註解的方式,大家根據自己的項目選擇適合自己的。 
一:基於xml配置的方式 
1:編寫普通的pojo 類

package com.aflyun.web.task;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
//@Service 都可以 
public class TaskCool {
    /**
     * 第一個定時器測試方法
     */
    public void testJob(){
        System.out.println("test first taskJob .... ");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2:配置xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/task 
       http://www.springframework.org/schema/task/spring-task.xsd">

    <context:component-scan base-package="com.aflyun.web" />
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <context:annotation-config />
    <!-- 在applicationContext.xml中進行配置,使用定時器
        ref : pojo類的名稱
        method : 調用的方式名稱
        cron : cronExpression表達式
        cron="0/5 * * * * ?"  //表示五秒鐘執行一次
     -->
    <task:scheduled-tasks>
        <task:scheduled ref="taskCool" method="testJob" cron="0/5 * * * * ?"/>
    </task:scheduled-tasks>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

:上面主要的配置文件中一定要加入task的命名空間和schema。 
上面 ref=”taskCool”,默認爲這個TaskCool 類的首字母小寫的值,若需要修改可以在@Component裏面進行修改 ,例如下面 
@Component(“taskCoolJob”)則ref=”taskCoolJon”。 
到此基於xml配置完成,運行則可以看到效果! 
二:基於註解方式 
使用註解方式不需要再每寫一個任務類還要在xml文件中配置下,方便了很多。使用Spring的@Scheduled,下面先看一註解@Scheduled在源文件中的定義:

@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})  
@Retention(RetentionPolicy.RUNTIME)  
@Documented  
public @interface Scheduled  
{  
  public abstract String cron();  

  public abstract long fixedDelay();  

  public abstract long fixedRate();  
}  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

cron:表示指定cron表達式。 
fixedDelay:表示從上一個任務完成開始到下一個任務開始的間隔,單位是毫秒。 
fixedRate:表示從上一個任務開始到下一個任務開始的間隔,單位是毫秒。 
下面進行一下具體的配置過程: 
1:編寫pojo類

package com.tclshop.cms.center.web.task;

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

@Component
public class WebTask {

    // 每五秒執行一次
    @Scheduled(cron = "0/5 * * * * ?")
    public void TaskJob() {
        System.out.println("test second annotation style ...");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2:配置xml文件 
下面貼出相關的配置文件內容:

<!-- 開啓這個配置,spring才能識別@Scheduled註解   -->  
    <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
    <task:scheduler id="qbScheduler" pool-size="10"/>
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

注:理論上只需要加上這句配置就可以了,其他參數都不是必須的。 
配置完成,運行就能看到效果!

總結:這種定時器的使用,不需要集成其他父類定時器,使用簡單方便!功能也很強大!

附:cronExpression的配置說明

字段      允許值     允許的特殊字符
秒       0-59        , - * /
分       0-59        , - * /
小時      0-23        , - * /
日期      1-31        , - * ? / L W C
月份      1-12 或者 JAN-DEC     , - * /
星期      1-7 或者 SUN-SAT      , - * ? / L C #
年(可選)       留空, 1970-2099       , - * /
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

例子:

CRON表達式         含義 
"0 0 12 * * ?"    每天中午十二點觸發 
"0 15 10 ? * *"    每天早上1015觸發 
"0 15 10 * * ?"    每天早上1015觸發 
"0 15 10 * * ? *"    每天早上1015觸發 
"0 15 10 * * ? 2005"    2005年的每天早上1015觸發 
"0 * 14 * * ?"    每天從下午2點開始到259分每分鐘一次觸發 
"0 0/5 14 * * ?"    每天從下午2點開始到255分結束每5分鐘一次觸發 
"0 0/5 14,18 * * ?"    每天的下午2點至2556點至655分兩個時間段內每5分鐘一次觸發 
"0 0-5 14 * * ?"    每天14:0014:05每分鐘一次觸發 
"0 10,44 14 ? 3 WED"    三月的每週三的14101444觸發 
"0 15 10 ? * MON-FRI"    每個週一、週二、週三、週四、週五的1015觸發 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

參考資料: 
1:Spring定時任務的幾種實現 
2:Spring中任務調度cronExpression配置說明 
3:QuartZ Cron表達式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章