Springboot2.x 集成 quartz 实现动态定时任务

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

猜你喜欢

转载自blog.csdn.net/weixin_38045214/article/details/113735445