SpringBoot 1.5.x 集成 Quartz 任务调度框架

Quartz 有分 内存方式 和 数据库方式

内存方式任务信息保存在内存中, 停机会丢失, 需手动重新执行, 数据库方式: 任务信息保存在数据库中, 重点是支持集群.

内存方式 RAMJobStore 和 数据库方式 JobStoreTX, RAMJobStore 适合单机, 不支持集群,  JobStoreTX 支持集群.

下面介绍的就是 JobStoreTX 数据库方式.

1. 添加主要的相关依赖, 其他依赖这里就不说了

<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
	<version>2.3.0</version>
</dependency>

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context-support</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-tx</artifactId>
</dependency>

2. quartz 的数据库表目前有 11 个, 可以下载官方包, 在官方包中可以找到建表语句, 对应不同的数据库有相应的sql语句.

配置文件 quartz.properties

org.quartz.scheduler.instanceName=DefaultQuartzScheduler
org.quartz.scheduler.instanceid=AUTO
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=20
org.quartz.threadPool.threadPriority=5
org.quartz.jobStore.misfireThreshold=2000
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.dataSource=myDS
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.isClustered=true
org.quartz.jobStore.clusterCheckinInterval=20000
org.quartz.dataSource.myDS.driver=org.mariadb.jdbc.Driver
org.quartz.dataSource.myDS.URL=jdbc:mariadb://localhost:3306/quartz?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
#org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver
#org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
org.quartz.dataSource.myDS.user=root
org.quartz.dataSource.myDS.password=root
org.quartz.dataSource.myDS.maxConnections=5
org.quartz.dataSource.myDS.validationQuery=select 0 from dual

3. JobFactory

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

@Configuration
public class JobFactory extends SpringBeanJobFactory {

	@Autowired
	private AutowireCapableBeanFactory autowireCapableBeanFactory;

	@Override
	protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
		Object createJobInstance = super.createJobInstance(bundle);
		autowireCapableBeanFactory.autowireBean(createJobInstance);
		return createJobInstance;
	}
}

4. QuartzConfig

import java.io.IOException;
import java.util.Properties;

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.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfig {

	@Autowired
	private JobFactory jobFactory;

//	@Autowired
//	private DataSource dataSource;    // 如果使用application.properties中的数据源可以用这种方式

	@Bean
	public SchedulerFactoryBean myScheduler() throws IOException {
		SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
//		schedulerFactoryBean.setDataSource(dataSource);                  // 使用 application.properties 中的数据源
		schedulerFactoryBean.setOverwriteExistingJobs(true);
		schedulerFactoryBean.setJobFactory(jobFactory);
		schedulerFactoryBean.setQuartzProperties(quartzProperties());    // 使用 quartz.properties 中的数据源
		schedulerFactoryBean.setSchedulerName("myScheduler");
		schedulerFactoryBean.setStartupDelay(2);// 延迟两秒启动
		schedulerFactoryBean.setAutoStartup(true);
		return schedulerFactoryBean;
	}

	@Bean
	public SchedulerFactoryBean myScheduler2() throws IOException {
		SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
//		schedulerFactoryBean.setDataSource(dataSource);				   // 使用 application.properties 中的数据源
		schedulerFactoryBean.setOverwriteExistingJobs(true);
		schedulerFactoryBean.setJobFactory(jobFactory);
		schedulerFactoryBean.setQuartzProperties(quartzProperties());  // 使用 quartz.properties 中的数据源
		schedulerFactoryBean.setSchedulerName("myScheduler2");
		schedulerFactoryBean.setStartupDelay(2);// 延迟两秒启动
		schedulerFactoryBean.setAutoStartup(true);
		return schedulerFactoryBean;
	}

	private Properties quartzProperties() throws IOException {
		PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
		propertiesFactoryBean.setLocation(new ClassPathResource("quartz.properties"));
		propertiesFactoryBean.afterPropertiesSet();
		Properties properties = propertiesFactoryBean.getObject();
		return properties;
	}
}

5. JobController

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.codingos.springboot.model.JobInfo;

@RestController
public class JobController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private Scheduler myScheduler;   // 这个注入应该和 config 类中 @Bean 的方法名相同
	@Autowired
	private Scheduler myScheduler2;  // 这个注入应该和 config 类中 @Bean 的方法名相同

	/**
	 * 创建Job
	 */
	@PostMapping("/createJob")
	public void createJob(@RequestBody JobInfo jobInfo) {
		Scheduler scheduler = getScheduler(jobInfo.getSchedulerName());

		Class<? extends Job> jobClass = null;
		try {
			jobClass = (Class<? extends Job>) Class.forName(jobInfo.getJobClassName());
		} catch (ClassNotFoundException e) {
			logger.error("create Job " + jobInfo.getJobName() + " error: " + e.getMessage(), e);
		}
		JobKey jobKey = new JobKey(jobInfo.getJobName(), jobInfo.getJobGroup());
		JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobKey).storeDurably().build();
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(jobInfo.getCron());
		TriggerKey triggerKey = new TriggerKey(jobInfo.getTriggerName(), jobInfo.getTriggerGroup());
		CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).forJob(jobDetail).withSchedule(scheduleBuilder).build();
		try {
			scheduler.scheduleJob(jobDetail, cronTrigger);
			scheduler.pauseJob(jobKey);   // 如果此处不暂停, 就会直接运行job
		} catch (SchedulerException e) {
			logger.error("create Job " + jobInfo.getJobName() + " error: " + e.getMessage(), e);
		}
	}

	@PostMapping("/jobStrat")
	public void jobStrat(@RequestBody JobInfo jobInfo) {
		Scheduler scheduler = getScheduler(jobInfo.getSchedulerName());
		JobKey jobKey = new JobKey(jobInfo.getJobName(), jobInfo.getJobGroup());
		try {
			scheduler.resumeJob(jobKey);  // 恢复执行job
		} catch (SchedulerException e) {
			logger.error("Job " + jobInfo.getJobName() + " start error: " + e.getMessage(), e);
		}
	}

	@PostMapping("/jobStop")
	public void jobStop(@RequestBody JobInfo jobInfo) {
		Scheduler scheduler = getScheduler(jobInfo.getSchedulerName());
		JobKey jobKey = new JobKey(jobInfo.getJobName(), jobInfo.getJobGroup());
		try {
			scheduler.pauseJob(jobKey);  // 暂停运行job
		} catch (SchedulerException e) {
			logger.error("Job " + jobInfo.getJobName() + " stop error: " + e.getMessage(), e);
		}

	}

	@PostMapping("/jobEdit")
	public void jobEdit(@RequestBody JobInfo jobInfo) {
		Scheduler scheduler = getScheduler(jobInfo.getSchedulerName());
		JobKey jobKey = new JobKey(jobInfo.getJobName(), jobInfo.getJobGroup());
		try {
			TriggerKey triggerKey = new TriggerKey(jobInfo.getTriggerName(), jobInfo.getTriggerGroup());
			CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(jobInfo.getCron());
			CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).forJob(jobKey).withSchedule(scheduleBuilder).build();
			scheduler.rescheduleJob(triggerKey, cronTrigger);  // 更新对应的 trigger
		} catch (SchedulerException e) {
			logger.error("Job " + jobInfo.getJobName() + " edit error: " + e.getMessage(), e);
		}
	}

	@PostMapping("/jobDelete")
	public void jobDelete(@RequestBody JobInfo jobInfo) {
		Scheduler scheduler = getScheduler(jobInfo.getSchedulerName());
		JobKey jobKey = new JobKey(jobInfo.getJobName(), jobInfo.getJobGroup());
		try {
			scheduler.deleteJob(jobKey);  // 删除job和对应的trigger
		} catch (SchedulerException e) {
			logger.error("Job " + jobInfo.getJobName() + " stop error: " + e.getMessage(), e);
		}
	}

	private Scheduler getScheduler(String schedulerName) {
		Scheduler scheduler = null;
		switch (schedulerName) {
		case "myScheduler":
			scheduler = myScheduler;
			break;
		case "myScheduler2":
			scheduler = myScheduler2;
			break;
		default:
			break;
		}
		return scheduler;
	}
}

6. Job 类

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.codingos.springboot.service.Job1Service;

public class Job1 extends QuartzJobBean {

	@Autowired
	private Job1Service job1Service;

	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		job1Service.service1();
	}
}

7. JobInfo 类

public class JobInfo {

	private String jobName;
	private String jobGroup;
	private String jobClassName;
	private String triggerName;
	private String triggerGroup;
	private String schedulerName;
	private String prevExecuteTime;
	private String nextExecuteTime;
	private String cron;
	private String triggerState;

	public JobInfo() {
	}

	public String getJobName() {
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	public String getCron() {
		return cron;
	}

	public void setCron(String cron) {
		this.cron = cron;
	}

	public String getTriggerState() {
		return triggerState;
	}

	public void setTriggerState(String triggerState) {
		this.triggerState = triggerState;
	}

	public String getJobGroup() {
		return jobGroup;
	}

	public void setJobGroup(String jobGroup) {
		this.jobGroup = jobGroup;
	}

	public String getTriggerName() {
		return triggerName;
	}

	public void setTriggerName(String triggerName) {
		this.triggerName = triggerName;
	}

	public String getTriggerGroup() {
		return triggerGroup;
	}

	public void setTriggerGroup(String triggerGroup) {
		this.triggerGroup = triggerGroup;
	}

	public String getPrevExecuteTime() {
		return prevExecuteTime;
	}

	public void setPrevExecuteTime(String prevExecuteTime) {
		this.prevExecuteTime = prevExecuteTime;
	}

	public String getNextExecuteTime() {
		return nextExecuteTime;
	}

	public void setNextExecuteTime(String nextExecuteTime) {
		this.nextExecuteTime = nextExecuteTime;
	}

	public String getJobClassName() {
		return jobClassName;
	}

	public void setJobClassName(String jobClassName) {
		this.jobClassName = jobClassName;
	}

	public String getSchedulerName() {
		return schedulerName;
	}

	public void setSchedulerName(String schedulerName) {
		this.schedulerName = schedulerName;
	}
}

最后, 说一下, 如果是 SpringBoot 2.x 集成 Quartz 框架, 添加依赖就非常方便了

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

来源:湖北网站优化

原文地址:https://www.cnblogs.com/1994july/p/12080424.html

时间: 2024-11-07 01:18:46

SpringBoot 1.5.x 集成 Quartz 任务调度框架的相关文章

配置quartz任务调度框架

<properties> <quartz.version>2.2.2</quartz.version> </properties> <dependencyManagement> <dependencies> <!-- quartz任务调度框架 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId&g

quartz任务调度框架与spring整合

Quartz是什么? Quartz 是一种功能丰富的,开放源码的作业调度库,可以在几乎任何Java应用程序集成 - 从最小的独立的应用程序到规模最大电子商务系统.Quartz可以用来创建简单或复杂的日程安排执行几十,几百,甚至是十万的作业数 - 作业被定义为标准的Java组件,可以执行几乎任何东西,可以编程让它们执行. Quartz调度包括许多企业级功能,如JTA事务和集群支持. 通过触发器结合cron表达式,可以实现丰富的执行策略满足生产需求. maven引入quartz包 <dependen

(2)Spring集成Quartz定时任务框架介绍和Cron表达式详解

在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但时调度控制非常不方便,并且我们需要大量的代码.使用Quartz框架无疑是非常好的选择,并且与Spring可以非常方便的集成,下面介绍它们集成方法和Cron表达式的详细介绍. 一.增加所依赖的JAR包1.增加Spring的Maven依赖 <dependency> <groupId>org

Quartz任务调度框架使用

一.什么是Quartz Quartz是一个完全由java编写的开源作业调度框架.不要让作业调度这个术语吓着你.尽管Quartz框架整合了许多额外功能, 但就其简易形式看,你会发现它易用得简直让人受不了! 下载官网:http://www.quartz-scheduler.org/ 可以在maven项目中导入对应的坐标 二.Quartz快速入门 建立一个maven项目 导入Quartz的maven坐标 其中官网中提供了一下案例可以参考一下 网站:http://www.quartz-scheduler

Quartz 任务调度框架整合Spring使用

简介 Quartz是一个完全由java编写的开源作业调度框架,其实是启动定时任务的框架.使用时只需创建一个org.quartz.Job接口的实现类,实现唯一的方法:public void execute(JobExecutionContext context) throws JobExecutionException,将你的逻辑任务添加到execute()方法即可. 一.基本概念 (一).执行对象 Job :工作任务 ,你要做什么 Trigger:执行工作任务 触发条件 , 什么时间执行, 多久

SSH中 集成Quartz任务调度一 普通定时任务

废话不多说,我的任务的配置文件都是交给 applicationContext.xml 处理的! <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns

Quartz任务调度框架

Quartz使用总结 教程

Quartz任务调度框架的简单应用

首先创建Maven项目并添加依赖 1 <!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz --> 2 <dependency> 3 <groupId>org.quartz-scheduler</groupId> 4 <artifactId>quartz</artifactId> 5 <version>2.2.3</version>

Quartz任务调度实践

最近在写一个任务调度程序,需要每隔几秒查询数据库,并取出数据做一些处理操作.使用到了Quartz任务调度框架. 基本概念 Quartz包含几个重要的对象,分别为任务(Job),触发器(Trigger),调度器(Scheduler) Job:一个接口只有一个方法void execute(),我们需要执行的任务就需要实现这个接口,在execute中实现我们要做的事情. JobDetail:在Quartz执行每次执行Job时,都需要创建一个Job实例,所以它直接接受一个实现类以便运行时实例化,还需要一