SpringBoot与Quartz集成实现分布式定时任务集群的代码实例

网友投稿 427 2023-01-11


SpringBoot与Quartz集成实现分布式定时任务集群的代码实例

Spring Boot与Quartz集成实现分布式定时任务集群

直接贴代码

POM

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

test.daemon

clusterquartz

0.0.1-SNAPSHOT

jar

clusterquartz

http://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

1.4.1.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-jdbc

org.springframework.boot

spring-boot-starter-logging

org.springframework

spring-context-support

mysql

mysql-connector-java

com.alibaba

druid

1.0.13

com.h2database

h2

org.quartz-scheduler

quartz

2.2.1

org.quartz-scheduler

quartz-jobs

2.2.1

junit

junit

test

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

test.daemon

clusterquartz

0.0.1-SNAPSHOT

jar

clusterquartz

http://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

1.4.1.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-jdbc

org.springframework.boot

spring-boot-starter-logging

org.springframework

spring-context-support

mysql

mysql-connector-java

com.alibaba

druid

1.0.13

com.h2database

h2

org.quartz-scheduler

quartz

2.2.1

org.quartz-scheduler

quartz-jobs

2.2.1

junit

junit

test

application.yml

server:

port: 80

spring:

datasource:

url: jdbc:mysql://localhost:3306/quartz

username: admin

password: admin

driver-class-name: com.mysql.jdbc.Driver

quartz.properties

#============================================================================

# Configure JobStore

# Using Spring datasource in SchedulerConfig.java

# Spring uses LocalDataSourceJobStore extension of JobStoreCMT

#============================================================================

org.quartz.jobStore.useProperties=false

org.quartz.jobStDkAiXCowore.tablePrefix = QRTZ_

org.quartz.jobStore.isClustered = true

org.quartz.jobStore.clusterCheckinInterval = 5000

org.quartz.jobStore.misfireThreshold = 60000

org.quartz.jobStore.txIsolationLevelReadCommitted = true

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX

org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate

#============================================================================

# Configure Main Scheduler Properties

# Needed to manage cluster instances

#============================================================================

org.quartz.scheduler.instanceName = ClusterQuartz

org.quartz.scheduler.instanceId= AUTO

org.quartz.scheduler.rmi.export = false

org.quartz.scheduler.rmi.proxy = false

org.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 = true

Spring配置类

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;

@Configuration

public 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

@DisallowConcurrentExecution

public 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;

@SpringBootApplication

public 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集群环境中,时间的同步是一个重要问题,有时间需要去看一下怎么进行时间同步来确保集群的正确性。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解Java设计模式——迭代器模式
下一篇:桌面程序的接口测试是啥(软件接口测试实战详解)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~