时间:2021-05-20
Spring Boot与Quartz集成实现分布式定时任务集群
直接贴代码
POM
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://mitted = trueorg.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTXorg.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate#============================================================================# Configure Main Scheduler Properties# Needed to manage cluster instances#============================================================================org.quartz.scheduler.instanceName = ClusterQuartzorg.quartz.scheduler.instanceId= AUTOorg.quartz.scheduler.rmi.export = falseorg.quartz.scheduler.rmi.proxy = falseorg.quartz.scheduler.wrapJobExecutionInUserTransaction = false#============================================================================# Configure ThreadPool# Can also be configured in spring configuration#============================================================================#org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool#org.quartz.threadPool.threadCount = 5#org.quartz.threadPool.threadPriority = 5#org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = trueSpring配置类
package test.daemon.clusterquartz.config;import java.io.IOException;import java.util.Properties;import java.util.concurrent.Executor;import javax.sql.DataSource;import org.quartz.Scheduler;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.config.PropertiesFactoryBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ClassPathResource;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import org.springframework.scheduling.quartz.CronTriggerFactoryBean;import org.springframework.scheduling.quartz.JobDetailFactoryBean;import org.springframework.scheduling.quartz.SchedulerFactoryBean;import test.daemon.clusterquartz.quartz.QuartzJob;@Configurationpublic class SchedulerConfig { @Autowired private DataSource dataSource; @Bean public Scheduler scheduler() throws Exception { Scheduler scheduler = schedulerFactoryBean().getScheduler(); scheduler.start(); return scheduler; } @Bean public SchedulerFactoryBean schedulerFactoryBean() throws IOException { SchedulerFactoryBean factory = new SchedulerFactoryBean(); factory.setSchedulerName("Cluster_Scheduler"); factory.setDataSource(dataSource); factory.setApplicationContextSchedulerContextKey("applicationContext"); factory.setTaskExecutor(schedulerThreadPool()); factory.setTriggers(trigger1().getObject()); factory.setQuartzProperties(quartzProperties()); return factory; } @Bean public Properties quartzProperties() throws IOException { PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties")); // 在quartz.properties中的属性被读取并注入后再初始化对象 propertiesFactoryBean.afterPropertiesSet(); return propertiesFactoryBean.getObject(); } @Bean public JobDetailFactoryBean job1() { JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean(); jobDetailFactoryBean.setJobClass(QuartzJob.class); jobDetailFactoryBean.setDurability(true); jobDetailFactoryBean.setRequestsRecovery(true); return jobDetailFactoryBean; } @Bean public CronTriggerFactoryBean trigger1() { CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean(); cronTriggerFactoryBean.setJobDetail(job1().getObject()); cronTriggerFactoryBean.setCronExpression("0/3 * * * * ?"); return cronTriggerFactoryBean; } @Bean public Executor schedulerThreadPool() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(15); executor.setMaxPoolSize(25); executor.setQueueCapacity(100); return executor; }}Quartz job类
package test.daemon.clusterquartz.quartz;import java.util.Date;import org.quartz.DisallowConcurrentExecution;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.quartz.PersistJobDataAfterExecution;import org.springframework.scheduling.quartz.QuartzJobBean;@PersistJobDataAfterExecution@DisallowConcurrentExecutionpublic class QuartzJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { // TODO Auto-generated method stub System.out.println("\nQuartz job " + new Date()); }}Spring Boot启动类
package test.daemon.clusterquartz;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Cluster { public static void main(String[] args) throws Exception { SpringApplication.run(Cluster.class, args); }}数据库sql
可以在Quartz的lib中找到适当的数据库生成文件来创建jdbc job store所需要的表。这些表用于Quartz在集群环境中的调度。
一些解释
把项目复制一份,然后改掉spring server的启动端口,启动多个项目,可以观察到只有一个项目的Quartz在运行。如果当前运行Quartz的服务器挂掉,另一台会跟进执行相同的Quartz任务。
有待思考的部分
在Quartz集群环境中,时间的同步是一个重要问题,有时间需要去看一下怎么进行时间同步来确保集群的正确性。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
我以为动态停启定时任务一般用quartz,没想到还可以通过ScheduledTaskRegistrar来拓展。但是分布式场景,建议还是用quartz吧!在spr
quartz集群能力:quartz集群分为水平集群和垂直集群,水平集群即将定时任务节点部署在不同的服务器,水平集群最大的问题就是时钟同步问题,quartz集群强
1、springboot整合quartz执行多个定时任务时报:org.quartz.ObjectAlreadyExistsException:Unabletos
最近收到了很多封邮件,都是想知道springboot整合quartz如何实现多个定时任务的,由于本人生产上并没有使用到多个定时任务,这里给个实现的思路。1、新建
定时任务在项目中经常会使用到,本文主要根据博主自己使用定时的经验分如下几点介绍定时任务:1、Quartz定时任务简介及Spring配置Quartz定时任务2、S