200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > quartz 2.0持久化到mysql_SpringBoot2.0整合Quartz定时任务(持久化到数据库 更为简单的方式)...

quartz 2.0持久化到mysql_SpringBoot2.0整合Quartz定时任务(持久化到数据库 更为简单的方式)...

时间:2022-04-12 07:05:25

相关推荐

quartz 2.0持久化到mysql_SpringBoot2.0整合Quartz定时任务(持久化到数据库 更为简单的方式)...

1. pom文件添加依赖

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-quartz

mysql

mysql-connector-java

runtime

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.1

com.mchange

c3p0

0.9.5.2

com.alibaba

druid-spring-boot-starter

1.1.10

2. yml配置

spring:

datasource:

url: jdbc:mysql://localhost:3306/aipyun?serverTimezone=GMT&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true

username: root

password: root123

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

type: com.alibaba.druid.pool.DruidDataSource

druid:

initialSize: 2

minIdle: 2

maxActive: 30

#StatViewServlet:

#loginUsername: admin

#loginPassword: admin

quartz:

#相关属性配置

properties:

org:

quartz:

scheduler:

instanceName: DefaultQuartzScheduler

instanceId: AUTO

jobStore:

class: org.quartz.impl.jdbcjobstore.JobStoreTX

driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate

tablePrefix: QRTZ_

isClustered: false

clusterCheckinInterval: 10000

useProperties: true

threadPool:

class: org.quartz.simpl.SimpleThreadPool

threadCount: 10

threadPriority: 5

threadsInheritContextClassLoaderOfInitializingThread: true

dataSource:

default:

URL: jdbc:mysql://localhost:3306/aipyun?characterEncoding=utf-8

user: root

password: root123

driver: com.mysql.jdbc.Driver

#数据库方式

job-store-type: jdbc

#初始化表结构

#jdbc:

#initialize-schema: never

我们可以对比下这个配置文件和之前的quartz.properties的区别,基本一模一样,只不过写的格式不一样而已。

3. 业务逻辑代码

/*** 删除job

*

*@paramtriggerName 触发器名称

*@paramtriggerGroup 触发器分组

*@paramjobName 任务名称

*@paramjobGroup 任务分组

*@throwsSchedulerException*/

public void deleteJob(String triggerName, String triggerGroup, String jobName, String jobGroup) throwsSchedulerException {

TriggerKey triggerKey=TriggerKey.triggerKey(triggerName, triggerGroup);

scheduler.pauseTrigger(triggerKey);

scheduler.unscheduleJob(triggerKey);

JobKey jobKey=JobKey.jobKey(jobName, jobGroup);

scheduler.deleteJob(jobKey);

}/*** 修改定时任务

*

*@paramoldTriggerKey 需要修改的TriggerKey 也就是唯一标识

*@paramcron 新的cron表达式*/

public voidupdateJob(TriggerKey oldTriggerKey, String cron) {

CronScheduleBuilder scheduleBuilder=CronScheduleBuilder.cronSchedule(cron);

CronTrigger cronTrigger=TriggerBuilder.newTrigger()

.withIdentity(oldTriggerKey).withSchedule(scheduleBuilder).build();try{

scheduler.rescheduleJob(oldTriggerKey, cronTrigger);

}catch(SchedulerException e) {

e.printStackTrace();

}

}/*** 新增job任务

*

*@paramjobName job名称

*@paramjobGroupName job分组名称

*@paramtriggerName 触发器名称

*@paramtriggerGroupName 触发器分组名称

*@paramjobClass 需要执行的job.class

*@paramcron cron 表达式

*@throwsSchedulerException*/

public voidaddJob(String jobName, String jobGroupName,

String triggerName, String triggerGroupName, Class jobClass, String cron)throwsSchedulerException {

CronScheduleBuilder cronScheduleBuilder=CronScheduleBuilder.cronSchedule(cron);

JobDetail jobDetail=JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();

Trigger trigger=TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName)

.withSchedule(cronScheduleBuilder).build();

scheduler.scheduleJob(jobDetail, trigger);

}

关于spring-boot-starter-quartz

鄙人用的开发工具是idea,可以直接查看源码,至于用eclispe的怎么查看源码我就不知道了。

我们找到Idea的External Libraries并且展开spring-boot-autoconfigure-2.0.0.RELEASE.jar,找到org.springframework.boot.autoconfigure.quartz,该目录就是SpringBoot为我们提供的Quartz自动化配置源码实现,在该目录下有如下所示几个类:

QuartzAutoConfiguration该类是自动配置的主类,内部配置了SchedulerFactoryBean,相当于我们之前自己配的SchedulerFactoryBean类。

JobStoreType是一个枚举:表示quartz的存储方式:RAM 或者JDBC。

QuartzProperties配置类:从yml或者properties中读取配置信息。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。