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>

来源:湖北网站优化

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

  1. maven项目集成Quartz定时任务框架,实现批处理功能

    一.Quartz简介 主要做定时任务,即:在指定时间点或时间段,执行某项任务,可设置执行次数.时间间隔等. 二.Springcloud简介 对比传统的.庞大的.复杂的.以ssm或ssh为框架的web项 ...

  2. quartz任务调度框架与spring整合

    Quartz是什么? Quartz 是一种功能丰富的,开放源码的作业调度库,可以在几乎任何Java应用程序集成 - 从最小的独立的应用程序到规模最大电子商务系统.Quartz可以用来创建简单或复杂的日 ...

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

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...

  4. Spring集成Quartz定时任务框架介绍和Cron表达式详解

    原文地址:http://www.cnblogs.com/obullxl/archive/2011/07/10/spring-quartz-cron-integration.html 在JavaEE系统 ...

  5. Quartz 任务调度框架之Hello World

    0x01 什么是Quartz? Quartz是一个完全由java编写的开源作业调度框架. 0x02 实战Quartz Hello World 创建Maven项目,POM 文件配置如下: <pro ...

  6. Quartz —— 任务调度框架

    一.Quartz Quartz 是 OpenSymphony 开源组织在任务调度领域的一个开源项目,完全基于 Java 实现.该项目于 2009 年被 Terracotta 收购,目前是 Terrac ...

  7. Spring_Spring集成Quartz定时任务框架介绍和Cron表达式详解

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...

  8. Spring集成Quartz定时任务框架介绍

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...

  9. (转)Java任务调度框架Quartz入门教程指南(四)Quartz任务调度框架之触发器精讲SimpleTrigger和CronTrigger、最详细的Cron表达式范例

    http://blog.csdn.net/zixiao217/article/details/53075009 Quartz的主要接口类是Schedule.Job.Trigger,而触发器Trigge ...

随机推荐

  1. 利用方法HttpUtility.HtmlEncode来预处理用户输入

    利用方法HttpUtility.HtmlEncode来预处理用户输入.这样能阻止用户用链接注入JavaScript代码或HTML标记,比如//Store/Broswe?Genre=<script ...

  2. SourceTree - 对Git的使用

    SourceTree - 对Git的使用 一 .SourceTree简介 SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,拥有可视化界面,容易上手操作 ...

  3. NCRE的JAVA二级考试大纲

    全国计算机等级考试二级 Java 语言 程序设计考试大纲(2018 年版) 基本要求 1. 掌握 Java 语言的特点.实现机制和体系结构. 2. 掌握 Java 语言中面向对象的特性. 3. 掌握 ...

  4. spring boot输入数据校验(validation)

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  5. PAT Advanced 1143 Lowest Common Ancestor (30) [二叉查找树 LCA]

    题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...

  6. promise 核心 几个小问题

    1.如何改变pending的壮体 抛出异常.pending变为rejected  // throw new Error('fail')  内部抛出异常也这样 reason为抛出的error resol ...

  7. vector删除指定元素

    #pragma once #include "stdafx.h" #include<windows.h> #include <vector> #includ ...

  8. 通过Android的API对Sqlite数据库进行操作

    一.增删改查 增 改 查 删 这是删除之前 删除三条 Dao.java package com.example.databasedemo; import android.content.Content ...

  9. 学生信息的添加 Java web简单项目初试(失败)

    题目要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单 ...

  10. mysql分组和排序操作

    分组.排序操作                                                                                         sele ...