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里具体想做啥,就可以随意发挥了。