1. 程式人生 > >Spring中定時器實現

Spring中定時器實現

很多 xmlns ice ... spring3 runtime pack tcl 首字母

在一些工作需要使用到定時器,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 .... ");
    }
    }
    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 />

</beans>

註:上面主要的配置文件中一定要加入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();
}

cron:表示指定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 ...");
}
}

2:配置xml文件

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

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

 註:理論上只需要加上這句配置就可以了,其他參數都不是必須的。 

配置完成,運行就能看到效果!

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

附:cronExpression的配置說明

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

例子:

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中定時器實現