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