spring boot / cloud (十) 使用quartz搭建调度中心
前言
quartz介绍:
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或 EJBs。Quartz的最新版本为Quartz 2.3.0
思路
将quartz集成进spring boot,并且提供一下内容:
配置
样例作业和触发器
实现
1.配置
引入maven依赖(这里的quartz使用的是2.2.3)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>```
编写AutowiringSpringBeanJobFactory类
public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory
implements ApplicationContextAware {
/**
- 描述 : beanFactory
*/
private AutowireCapableBeanFactory beanFactory;
@Override
public void setApplicationContext(final ApplicationContext context) {
beanFactory = context.getAutowireCapableBeanFactory();
}
@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
final Object job = super.createJobInstance(bundle);
beanFactory.autowireBean(job);
return job;
}
}`
编写SchedulerConfig类
@Configuration
@ConfigurationProperties(prefix = "com.egridcloud.scheduler.config")
@Validated
public class SchedulerConfig {
..........
}```
编写SchedulerConfig类-属性
//是否记录日志
@NotNull
private Boolean logFlag = true;
//是否记录详细日志
@NotNull
private Boolean logDetailFlag = false;
/是否自动启动
@NotNull
private Boolean autoStartup = true;
//是否覆盖已经存在的jobs
@NotNull
private Boolean overwriteExistingJobs = true;
//延迟启动秒数
@NotNull
private Integer startupDelay = 0;
/Job接受applicationContext的成员变量名
@NotNull
private String applicationContextSchedulerContextKey = "applicationContext";
//quartz配置文件地址
@NotNull
private String quartzPropertiesPath = "def_quartz.properties";`
编写SchedulerConfig类-编写bean
@Bean
public JobFactory jobFactory(ApplicationContext applicationContext) {
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource(getQuartzPropertiesPath()));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
@Bean("clusterQuartzScheduler")
public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource, JobFactory jobFactory,
Properties quartzProperties)
throws IOException {
//实例化
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setDataSource(dataSource);
factory.setJobFactory(jobFactory);
//属性设置
factory.setQuartzProperties(quartzProperties);
factory.setOverwriteExistingJobs(getOverwriteExistingJobs());
factory.setAutoStartup(getAutoStartup());
factory.setStartupDelay(getStartupDelay());
factory.setApplicationContextSchedulerContextKey(getApplicationContextSchedulerContextKey());
//返回
return factory;
}```
编写def_quartz.properties配置文件
Configure scheduler
org.quartz.scheduler.instanceName=clusterQuartzScheduler
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.skipUpdateCheck=true
Configure ThreadPool
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=5
org.quartz.threadPool.threadPriority=5
Configure JobStore and Cluster
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties=true
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.isClustered=true
org.quartz.jobStore.clusterCheckinInterval=20000
org.quartz.jobStore.misfireThreshold=60000`
执行数据库脚本,以及数据库配置(按需,我这里选择的是h2)
tables_h2.sql
########################################################
###DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
########################################################
spring.datasource.url=jdbc:h2:file:D:/develop/d2_db/testdb```
结束