SpringBoot定時任務實現

2021-10-07 06:20:10 字數 1449 閱讀 1535

靜態定時任務實現:基於註解來建立定時任務

@configuration

@enablescheduling

public

class

******scheduletask

}

基於資料庫的動態定時任務實現

將定時任務配置在資料庫,啟動專案的時候,用mybatis讀取資料庫,例項化物件,並設定定時任務。如果需要新增,減少,修改定時任務,僅需要修改資料庫資料,並重啟專案即可,無需改**。這樣的操作可以通過直接修改資料庫,執行週期就會改變,並且不需要我們重啟應用

@lazy

(value =

false

)@component

public

class

scheduletask implements schedulingconfigurer

catch

(exception e)}}

logger.

info

("started task number="

+count+

"; time="

+sdf.

format

(new

date()

));}

;/**

* 獲取要執行的所有任務

* @return

*/private list

getallscheduletasks()

/** * 獲取runnable

*/private runnable getrunnable

(scheduletask task)

catch

(invocationtargetexception e)

catch

(exception e)}}

;}/** * 獲取trigger

*/private trigger gettrigger

(scheduletask task)};

}/**

* 校驗資料

*/private list

checkdatalist

(list list);}

if(!stringutils.

isempty

(msg)

)return list;

}/**

* 按每一條校驗資料

*/private string checkonedata

(scheduletask task)

}catch

(classnotfoundexception e)

catch

(nosuchmethodexception e)

catch

(exception e)

return result;

}}

spring boot 定時任務實現

scheduled 使用 scheduled 非常容易,直接建立乙個 spring boot 專案,並且新增 web 依賴 spring boot starter web,專案建立成功後,新增 enablescheduling 註解,開啟定時任務 enablescheduling 開啟定時任務 pu...

SpringBoot 定時任務

第一步 在啟動類中加入如下註解 enablescheduling public class public static void main string args 注意 enablescheduling必須加,否則無法開啟定時任務 第二步 建立定時任務 component public class ...

Spring boot定時任務

最近做專案,需要使用定時任務,半個小時去更新redis資料,於是便學習了一下經驗。希望可以幫到你們。定時任務可使用三種 created by fx on 2017 9 4.component allargsconstructor public class featuredatatask 實現乙個任務...