Springboot2.x 集成 quartz 實作動態定時任務
1、引入依賴
springboot2只需要引入starter-quartz即可
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
``
2、實作ApplicationContextAware介面
實作ApplicationContextAware介面,具體作用后面會說
3、實作Job介面,通過ApplicationContextRegister獲取Service實體
事先準備好需要處理業務的Service,通過ApplicationContextAware的實作獲取Service的實體
(為啥不用依賴注入,因為Job的Bean是Quartz自己管理的,注入一個Spring管理的Bean會NullPointer)
4、實作動態定時任務介面
實作打開、關閉定時任務介面:
JobName和組名可以根據需要自定
實作更新JOB執行的頻率(cron)
至于JOB里具體想做啥,就可以隨意發揮了,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/257827.html
標籤:其他






