Spring Spring中定時器實現

2021-07-24 17:48:28 字數 3495 閱讀 1430

在一些工作需要使用到定時器,

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

}

2:配置xml檔案

<?xml version="1.0" encoding="utf-8"?>

xmlns:xsi=""

xmlns:tx=""

xmlns:aop=""

xmlns:context=""

xmlns:task=""

xmlns=""

xsi:schemalocation="

/spring-beans.xsd

/spring-tx.xsd

/spring-aop.xsd

/spring-context.xsd

/spring-task.xsd">

base-package="com.aflyun.web" />

proxy-target-class="true" />

ref : pojo類的名稱

method : 呼叫的方式名稱

cron : cronexpression表示式

cron="0/5 * * * * ?" //表示五秒鐘執行一次

-->

ref="taskcool"

method="testjob"

cron="0/5 * * * * ?"/>

task:scheduled-tasks>

beans>

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

上面 ref=」taskcool」,預設為這個taskcool 類的首字母小寫的值

,若需要修改可以在@component裡面進行修改 ,例如下面 

@component(「taskcooljob」)則ref=」taskcooljon」。 

到此基於xml配置完成,執行則可以看到效果! 

二:基於註解方式

使用註解方式不需要再每寫乙個任務類還要在xml檔案中配置下,方便了很多。使用spring的@scheduled,下面先看一註解@scheduled在原始檔中的定義:

@target()  

@retention(retentionpolicy.runtime)

@documented

public @inte***ce

scheduled

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

}

2:配置xml檔案 

下面貼出相關的配置檔案內容:

scheduler="qbscheduler"

mode="proxy"/>

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"

2023年的每天早上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 Spring中的AOP簡介 原理

aop aspect oriented programming 面向切面程式設計。aop是oop object oriented programming 物件導向程式設計的延續。aop是可以通過預編譯方式和執行期動態 的方式在不修改源 的情況下給程式統一新增功能的一種技術。aop實際上是gof ga...

Spring Spring開發準備

原始碼,jar檔案 spring framework 3.2.5.release commons logging 1.1.3.jar 日誌 spring beans 3.2.5.release.jar bean節點 spring context 3.2.5.release.jar spring上下文...

Spring《Spring與Mybatis結合一》

將mybatis與spring結合可以把service與servlet分離開來 首先導包 第一部分 在之前使用mybatis框架時,需要寫乙個sqlsessionfactory工廠來獲取sqlsession,在工廠裡需要readsource取讀取mybatis.xml配置檔案,其中配置檔案的envi...