精進Spring—Spring中定時器實現

做一個積極的人

編碼、改bug、提升自己

我有一個樂園,面向編程,春暖花開!

分享一位老師的人工智能教程。零基礎!通俗易懂!風趣幽默!
大家可以看看是否對自己有幫助,點擊這裏查看【人工智能教程】。接下來進入正文。


在一些工作需要使用到定時器,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 />
	<!-- 在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>

:上面主要的配置文件中一定要加入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"/>

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

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


附: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觸發 

三:參考資料

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


謝謝你的閱讀,如果您覺得這篇博文對你有幫助,請點贊或者喜歡,讓更多的人看到!祝你每天開心愉快!



不管做什麼,只要堅持下去就會看到不一樣!在路上,不卑不亢!

博客首頁 : http://blog.csdn.net/u010648555

願你我在人生的路上能都變成最好的自己,能夠成爲一個獨擋一面的人

© 每天都在變得更好的阿飛雲

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