From faabdf9b836f9abe73f6e515c866be58bcdbc573 Mon Sep 17 00:00:00 2001 From: wangkang Date: Mon, 19 Jun 2017 10:41:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 10.md | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 10.md diff --git a/10.md b/10.md new file mode 100644 index 0000000..2ad587e --- /dev/null +++ b/10.md @@ -0,0 +1,192 @@ +# 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) + +``` xml + + org.springframework + spring-context-support + + + + org.quartz-scheduler + quartz + ${quartz.version} + +``` + +编写AutowiringSpringBeanJobFactory类 + +``` java +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类 + +``` java +@Configuration +@ConfigurationProperties(prefix = "com.egridcloud.scheduler.config") +@Validated +public class SchedulerConfig { + +.......... + +} +``` + +编写SchedulerConfig类-属性 + +``` java + //是否记录日志 + @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 + +``` java + @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配置文件 + +``` 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) + + +``` properties + +tables_h2.sql + +######################################################## +###DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) +######################################################## +spring.datasource.url=jdbc:h2:file:D:/develop/d2_db/testdb + +``` + + +##结束 + +以上演示了quartz在spring boot中的配置,在下一章里会聊一下通过RMS来实现分布式调度的调度器的设计. + +--------- + +想获得最快更新,请关注公众号 + +![想获得最快更新,请关注公众号](https://mmbiz.qlogo.cn/mmbiz_jpg/gjOvoY7GOt5a4dicfGbqze591YAEiaRONE0nOsiaur4nlsmKtUpRuONue28wJ9JfOXfBl99OoVmYncohMnEY4LMdg/0?wx_fmt=jpeg "想获得最快更新,请关注公众号") +