解决Spring boot集成quartz时service注入失败为null的问题
调度任务理框架选择
提及调度任务,一般使用过Spring boot的开发者首先会想起自带的调度框架,直接用Spring boot中的@Scheduled注解,但是在一般的商业实际应用当中,往往有复杂的业务逻辑需要处理,有些还需要调度任务的持久化,这种场景下Spring boot自带的调度任务则有点力不从心,此时需要考虑去集成别的调度框架,而Quartz就是一个应用场景广泛的且能解决Spring boot自带调度任务实现不了的功能的不二之选。
Quartz是OpenSymphony开源的一个项目,是一个由Java编写的开源作业调度框架。
Spring task
优点:无需整合spring,作业类中就可以调用业务service
缺点:单线程;不能做数据存储型的定时任务
Quartz
优点:多线程;可以做数据存储型的定时任务,维护性高;多任务情况下,quartz更容易管理,可以实现动态配置;
缺点:需要整合spring,不能直接调用业务层service;
Quartz特点
1、支持分布式高可用
我们需要某个定时任务在多个节点中只有某个节点可以执行时,就需要Quartz来实现,否则使用@Scheduled等方式会造成所有节点都执行一遍。
2、支持持久化
Quartz有专门的数据表来实现定时任务的持久化。
3、支持多任务调度和管理
Quartz可以在数据库中存储多个定时任务进行作业调度,可以实现定时任务的增删改查等管理。
选用原则
优先推荐使用Quartz调度以应对复杂的调度场景,除非要使用的场景简单且单一,甚至是一次性的,或者不需要持久化的可以考虑使用spring boot自带的调度框架以减少集成时的时间成本。
Spring boot 与quartz代码集成
pom依赖
<!--quartz定时调度依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--quartz定时调度依赖-->
<!--jdbc依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
quartz配置
新建quartz.properties
配置如下:
#[quartz批处理配置文件]
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false
#============================================================================
# Configure ThreadPool 线程池配置
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
#持久化配置
#============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.misfireThreshold = 50000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#支持集群
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.useProperties = true
org.quartz.jobStore.clusterCheckinInterval = 15000
#使用weblogic连接Oracle驱动
#org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.oracle.weblogic.WebLogicOracleDelegate
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.dataSource = qzDS
#============================================================================
# Configure Datasources
#============================================================================
#数据源连接信息,quartz默认使用c3p0数据源可以被自定义数据源覆盖
org.quartz.dataSource.qzDS.connectionProvider.class=com.summer.msp.druid.conn.DruidConnectionProvider
org.quartz.dataSource.qzDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.qzDS.URL = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
org.quartz.dataSource.qzDS.user = root
org.quartz.dataSource.qzDS.password = 123456
#配置数据库源(org.quartz.dataSource.qzDS.maxConnections: c3p0配置的是有s的,druid数据源没有s)
org.quartz.dataSource.qzDS.maxConnection = 10
java代码
1、新建MyJobFactory类
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;
/**
* @ClassName: MyJobFactory
* @Description: TODO
* @author Sebastian
* @date 2020年11月08日 上午10:42:28
* @version V1.0
*/
@Component
public class MyJobFactory extends AdaptableJobFactory {
//这个对象Spring会帮我们自动注入进来
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
//重写创建Job任务的实例方法
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
//通过以下方式,解决Job任务无法使用Spring中的Bean问题
autowireCapableBeanFactory.autowireBean(jobInstance);
//return super.createJobInstance(bundle);
return jobInstance;
}
}