背景
在項(xiàng)目開發(fā)過程中,我們經(jīng)常需要執(zhí)行具有周期性的任務(wù)。通過定時(shí)任務(wù)可以很好的幫助我們實(shí)現(xiàn)。
我們拿常用的幾種定時(shí)任務(wù)框架做一個(gè)比較:
從以上表格可以看出,spring schedule框架功能完善,簡單易用。對于中小型項(xiàng)目需求,spring schedule是完全可以勝任的。
1、springboot集成schedule
1.1 添加maven依賴包
由于spring schedule包含在spring-boot-starter基礎(chǔ)模塊中了,所有不需要增加額外的依賴。
1
2
3
4
5
6
7
8
9
10
11
12
|
<dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> |
1.2 啟動(dòng)類,添加啟動(dòng)注解
在springboot入口或者配置類中增加@enablescheduling注解即可啟用定時(shí)任務(wù)。
1
2
3
4
5
6
7
|
@enablescheduling @springbootapplication public class scheduleapplication { public static void main(string[] args) { springapplication.run(scheduleapplication. class , args); } } |
1.3.添加定時(shí)任務(wù)
我們將對spring schedule三種任務(wù)調(diào)度器分別舉例說明。
1.3.1 cron表達(dá)式
類似于linux下的cron表達(dá)式時(shí)間定義規(guī)則。cron表達(dá)式由6或7個(gè)空格分隔的時(shí)間字段組成,如下圖:
常用表達(dá)式:
舉個(gè)栗子:
添加一個(gè)work()方法,每10秒執(zhí)行一次。
注意 :當(dāng)方法的執(zhí)行時(shí)間超過任務(wù)調(diào)度頻率時(shí),調(diào)度器會(huì)在下個(gè)周期執(zhí)行。
如:假設(shè)work()方法在第0秒開始執(zhí)行,方法執(zhí)行了12秒,那么下一次執(zhí)行work()方法的時(shí)間是第20秒。
1
2
3
4
5
6
7
|
@component public class mytask { @scheduled (cron = "0/10 * * * * *" ) public void work() { // task execution logic } } |
1.3.2 固定間隔任務(wù)
下一次的任務(wù)執(zhí)行時(shí)間,是從方法最后一次任務(wù)執(zhí)行結(jié)束時(shí)間開始計(jì)算。并以此規(guī)則開始周期性的執(zhí)行任務(wù)。
舉個(gè)栗子:
添加一個(gè)work()方法,每隔10秒執(zhí)行一次。
例如:假設(shè)work()方法在第0秒開始執(zhí)行,方法執(zhí)行了12秒,那么下一次執(zhí)行work()方法的時(shí)間是第22秒。
1
2
3
4
|
@scheduled (fixeddelay = 1000 * 10 ) public void work() { // task execution logic } |
1.3.3 固定頻率任務(wù)
按照指定頻率執(zhí)行任務(wù),并以此規(guī)則開始周期性的執(zhí)行調(diào)度。
舉個(gè)栗子:
添加一個(gè)work()方法,每10秒執(zhí)行一次。
注意 :當(dāng)方法的執(zhí)行時(shí)間超過任務(wù)調(diào)度頻率時(shí),調(diào)度器會(huì)在當(dāng)前方法執(zhí)行完成后立即執(zhí)行下次任務(wù)。
例如:假設(shè)work()方法在第0秒開始執(zhí)行,方法執(zhí)行了12秒,那么下一次執(zhí)行work()方法的時(shí)間是第12秒。
1
2
3
4
|
@scheduled (fixedrate = 1000 * 10 ) public void work() { // task execution logic } |
2、配置taskscheduler線程池
在實(shí)際項(xiàng)目中,我們一個(gè)系統(tǒng)可能會(huì)定義多個(gè)定時(shí)任務(wù)。那么多個(gè)定時(shí)任務(wù)之間是可以相互獨(dú)立且可以并行執(zhí)行的。
通過查看org.springframework.scheduling.config.scheduledtaskregistrar源代碼,發(fā)現(xiàn)spring默認(rèn)會(huì)創(chuàng)建一個(gè)單線程池。這樣對于我們的多任務(wù)調(diào)度可能會(huì)是致命的,當(dāng)多個(gè)任務(wù)并發(fā)(或需要在同一時(shí)間)執(zhí)行時(shí),任務(wù)調(diào)度器就會(huì)出現(xiàn)時(shí)間漂移,任務(wù)執(zhí)行時(shí)間將不確定。
1
2
3
4
5
6
7
|
protected void scheduletasks() { if ( this .taskscheduler == null ) { this .localexecutor = executors.newsinglethreadscheduledexecutor(); this .taskscheduler = new concurrenttaskscheduler( this .localexecutor); } //省略... } |
2.1 自定義線程池
新增一個(gè)配置類,實(shí)現(xiàn)schedulingconfigurer接口。重寫configuretasks方法,通過taskregistrar設(shè)置自定義線程池。
1
2
3
4
5
6
7
8
9
10
11
12
|
@configuration public class scheduleconfig implements schedulingconfigurer { @override public void configuretasks(scheduledtaskregistrar taskregistrar) { taskregistrar.setscheduler(taskexecutor()); } @bean (destroymethod= "shutdown" ) public executor taskexecutor() { return executors.newscheduledthreadpool( 20 ); } } |
3、實(shí)際應(yīng)用中的問題
3.1 web應(yīng)用中的啟動(dòng)和關(guān)閉問題
我們知道通過spring加載或初始化的bean,在服務(wù)停止的時(shí)候,spring會(huì)自動(dòng)卸載(銷毀)。但是由于線程是jvm級別的,如果用戶在web應(yīng)用中啟動(dòng)了一個(gè)線程,那么這個(gè)線程的生命周期并不會(huì)和web應(yīng)用保持一致。也就是說,即使web應(yīng)用停止了,這個(gè)線程依然沒有結(jié)束(死亡)。
解決方法:
1)當(dāng)前對象是通過spring初始化
spring在卸載(銷毀)實(shí)例時(shí),會(huì)調(diào)用實(shí)例的destroy方法。通過實(shí)現(xiàn)disposablebean接口覆蓋destroy方法實(shí)現(xiàn)。在destroy方法中主動(dòng)關(guān)閉線程。
1
2
3
4
5
6
7
8
9
10
|
@component public class mytask implements disposablebean{ @override public void destroy() throws exception { //關(guān)閉線程或線程池 threadpooltaskscheduler scheduler = (threadpooltaskscheduler)applicationcontext.getbean( "scheduler" ); scheduler.shutdown(); } //省略... } |
2)當(dāng)前對象不是通過spring初始化(管理)
那么我們可以增加一個(gè)servlet上下文監(jiān)聽器,在servlet服務(wù)停止的時(shí)候主動(dòng)關(guān)閉線程。
1
2
3
4
5
6
7
|
public class mytasklistenter implements servletcontextlistener{ @override public void contextdestroyed(servletcontextevent arg0) { //關(guān)閉線程或線程池 } //省略... } |
3.2 分布式部署問題
在實(shí)際項(xiàng)目中,我們的系統(tǒng)通常會(huì)做集群、分布式或?yàn)?zāi)備部署。那么定時(shí)任務(wù)就可能出現(xiàn)并發(fā)問題,即同一個(gè)任務(wù)在多個(gè)服務(wù)器上同時(shí)在運(yùn)行。
解決方法(分布式鎖):
1)通過數(shù)據(jù)庫表鎖
2)通過緩存中間件
3)通過zookeeper實(shí)現(xiàn)
總結(jié):
spring schedule給我們提供了一套簡單、快速、高效、穩(wěn)定的定時(shí)任務(wù)框架。但需要考慮線程的生命周期及分布式部署問題。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/skychenjiajun/p/9057379.html