Spring中定時(shí)器實(shí)現(xiàn)

在一些工作需要使用到定時(shí)器,Spring很好的集成了定時(shí)器的功能!

創(chuàng)新互聯(lián)公司長(zhǎng)期為超過(guò)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為江都企業(yè)提供專業(yè)的成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì),江都網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。

在Spring 中使用Quartz,本文介紹Spring3.0以后自主開(kāi)發(fā)的定時(shí)任務(wù)工具,spring task,可以將它比作一個(gè)輕量級(jí)的Quartz,而且使用起來(lái)很簡(jiǎn)單,除spring相關(guān)的包外不需要額外的包,下面介紹兩種方式實(shí)現(xiàn)Spring定時(shí)器功能,一種是基于xml配置方式,另外一種是基于注解的方式,大家根據(jù)自己的項(xiàng)目選擇適合自己的。

一:基于xml配置的方式

1:編寫(xiě)普通的pojo 類

package com.aflyun.web.task;

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

@Component
//@Service 都可以
public class TaskCool {
/**

  • 第一個(gè)定時(shí)器測(cè)試方法
    */
    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 />
    <!-- 在applicationContext.xml中進(jìn)行配置,使用定時(shí)器
    ref : pojo類的名稱
    method : 調(diào)用的方式名稱
    cron : cronExpression表達(dá)式
    cron="0/5 ?" //表示五秒鐘執(zhí)行一次
    -->
    <task:scheduled-tasks>
    <task:scheduled ref="taskCool" method="testJob" cron="0/5 ?"/>
    </task:scheduled-tasks>

</beans>

注:上面主要的配置文件中一定要加入task的命名空間和schema。

上面 ref=”taskCool”,默認(rèn)為這個(gè)TaskCool 類的首字母小寫(xiě)的值,若需要修改可以在@Component里面進(jìn)行修改 ,例如下面
@Component(“taskCoolJob”) 則此時(shí) ref=”taskCoolJon”。
到此基于xml配置完成,運(yùn)行則可以看到效果!

二:基于注解方式

使用注解方式不需要再每寫(xiě)一個(gè)任務(wù)類還要在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表達(dá)式。(cron類型表示 是指定時(shí)間觸發(fā)器觸發(fā)任務(wù)執(zhí)行!)

fixedDelay:表示從上一個(gè)任務(wù)完成開(kāi)始到下一個(gè)任務(wù)開(kāi)始的間隔,單位是毫秒。
fixedRate:表示從上一個(gè)任務(wù)開(kāi)始到下一個(gè)任務(wù)開(kāi)始的間隔,單位是毫秒。

下面進(jìn)行一下具體的配置過(guò)程:

1:編寫(xiě)pojo類

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

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

@Component
public class WebTask {

// 每五秒執(zhí)行一次
@Scheduled(cron = "0/5 ?")
public void TaskJob() {
System.out.println("test second annotation style ...");
}
}

2:配置xml文件

下面貼出相關(guān)的配置文件內(nèi)容:

<!-- 開(kāi)啟這個(gè)配置,spring才能識(shí)別@Scheduled注解 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>

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

配置完成,運(yùn)行就能看到效果!

總結(jié):這種定時(shí)器的使用,不需要集成其他父類定時(shí)器,使用簡(jiǎn)單方便!功能也很強(qiáng)大!

附:cronExpression的配置說(shuō)明

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

例子:

CRON表達(dá)式 含義
"0 0 12 ?" 每天中午十二點(diǎn)觸發(fā)
"0 15 10 ? " 每天早上10:15觸發(fā)
"0 15 10 ?" 每天早上10:15觸發(fā)
"0 15 10 ? " 每天早上10:15觸發(fā)
"0 15 10
? 2005" 2005年的每天早上10:15觸發(fā)
"0
14 ?" 每天從下午2點(diǎn)開(kāi)始到2點(diǎn)59分每分鐘一次觸發(fā)
"0 0/5 14 ?" 每天從下午2點(diǎn)開(kāi)始到2:55分結(jié)束每5分鐘一次觸發(fā)
"0 0/5 14,18 ?" 每天的下午2點(diǎn)至2:55和6點(diǎn)至6點(diǎn)55分兩個(gè)時(shí)間段內(nèi)每5分鐘一次觸發(fā)
"0 0-5 14 ?" 每天14:00至14:05每分鐘一次觸發(fā)
"0 10,44 14 ? 3 WED" 三月的每周三的14:10和14:44觸發(fā)
"0 15 10 ? * MON-FRI" 每個(gè)周一、周二、周三、周四、周五的10:15觸發(fā)

分享名稱:Spring中定時(shí)器實(shí)現(xiàn)
文章分享:http://www.muchs.cn/article26/geeicg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站企業(yè)建站、靜態(tài)網(wǎng)站、全網(wǎng)營(yíng)銷推廣、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司