1.SpringBoot整合Quartz使用内存持久化
1.1.什么是内存持久化
Quartz的内存持久化是指Quartz框架在默认情况下将任务调度的运行信息保存在内存中。这种方式的优点是可以提供最佳的性能,因为内存中数据访问最快。然而,内存持久化的缺点是缺乏数据的持久性。当程序停止或系统崩溃时,所有运行的信息都会丢失。
1.2.步骤如下
1.2.1. 添加依赖
首先,在pom.xml文件中添加Quartz的依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
1.2.2. 创建Job类
实现org.quartz.Job接口或org.quartz.StatefulJob接口,并覆盖execute方法。
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
// 执行作业逻辑
}
}
1.2.3. 创建JobDetail和Trigger
使用JobBuilder和TriggerBuilder来创建JobDetail和Trigger实例,并通过调度器将它们关联起来。
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class QuartzConfig {
//创建JobDetailr实例
@Bean
public JobDetail myJobDetail() {
return JobBuilder.newJob(MyJob.class)
.withIdentity("myJob", "group1")
.storeDurably()
.build();
}
//创建调度器和Trigge并管理jobDetail
@Bean
public Trigger myTrigger() {
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5) //每5秒执行一次任务
.repeatForever();
return TriggerBuilder.newTrigger()
.forJob(myJobDetail())
.withIdentity("myTrigger", "group1")
.withSchedule(scheduleBuilder)
.build();
}
}
1.2.4. 启动应用
运行你的Spring Boot应用,Quartz将会根据配置自动启动,并按照你定义的Trigger来触发并执行作业。
1.2.实例
定时执行任务,查找数据库中预约时间过期的数据并将车位状态实时更新。
1.2.1数据库数据说明
预约记录表单
车位表单中的id作为预约表单的外键
1.2.2.关键代码
对job类进行修改
package com.duhong.quartz;
import com.duhong.entity.OrderParking;
import com.duhong.service.OrderService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.batch.BatchProperties;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.List;
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class UpdateJob implements Job {
@Autowired
OrderService orderService;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//查询所有预约记录
List<OrderParking> orderParkings = orderService.selectRecord();
for (OrderParking orderParking : orderParkings) {
System.out.println("作业执行");
System.out.println("到期时间"+orderParking.getDeadtime());
System.out.println("当前时间"+LocalDateTime.now());
//未到预约时间
if(orderParking.getDeadtime().isBefore( LocalDateTime.now())){
//更新车位状态
orderService.updateInitStatus(orderParking.getId());
System.out.println("更新成功");
}else{
System.out.println("未到预约时间");
}
}
}
}
1.2.3.执行结果
2.SpringBoot整合Quartz使用mysql持久化
2.1.为啥要使用数据库持久化
Quartz 是一个开源的任务调度框架,用于在 Java 应用中执行定时任务。Quartz 本身不直接提供任务持久化的功能,但它支持将任务、触发器和其他相关信息存储到数据库中,以便在应用程序重启或服务器崩溃后恢复。
选择 MySQL 作为 Quartz 的持久化存储有以下几个原因:
- 通用性和普及性:MySQL 是一个广泛使用的开源关系型数据库管理系统,具有很高的通用性和普及性。很多开发者和团队都熟悉 MySQL,因此选择 MySQL 作为 Quartz 的持久化存储可以简化开发和维护过程。
- 稳定性和可靠性:MySQL 是一个成熟、稳定且经过广泛验证的数据库系统。它提供了良好的数据完整性和可靠性保证,可以确保 Quartz 任务调度的准确性和一致性。
- 性能优化:MySQL 在处理大量数据和复杂查询方面表现出色。通过合理的索引和查询优化,可以确保 Quartz 在持久化任务时具有良好的性能表现。
- 社区支持和资源:由于 MySQL 的广泛使用和开源性质,它拥有庞大的用户社区和丰富的资源。这意味着在遇到问题时,可以更容易地找到解决方案或获得帮助。
- 扩展性和灵活性:MySQL 支持多种存储引擎和配置选项,可以根据实际需求进行灵活的配置和扩展。这使得它成为一个适合各种规模应用程序的持久化存储选择。
2.2.持久化步骤
基于对quartz使用内存持久化,我们只需添加数据库表及修改properities文件即可文章来源:https://www.toymoban.com/news/detail-831972.html
2.2.1.添加数据库表
create table if not exists parking_place
(
id int auto_increment
primary key,
location varchar(255) null,
status int default 0 null,
price double null,
type varchar(255) null
);
create table if not exists qrtz_calendars
(
SCHED_NAME varchar(120) not null,
CALENDAR_NAME varchar(190) not null,
CALENDAR blob not null,
primary key (SCHED_NAME, CALENDAR_NAME)
);
create table if not exists qrtz_fired_triggers
(
SCHED_NAME varchar(120) not null,
ENTRY_ID varchar(95) not null,
TRIGGER_NAME varchar(190) not null,
TRIGGER_GROUP varchar(190) not null,
INSTANCE_NAME varchar(190) not null,
FIRED_TIME bigint(13) not null,
SCHED_TIME bigint(13) not null,
PRIORITY int not null,
STATE varchar(16) not null,
JOB_NAME varchar(190) null,
JOB_GROUP varchar(190) null,
IS_NONCONCURRENT varchar(1) null,
REQUESTS_RECOVERY varchar(1) null,
primary key (SCHED_NAME, ENTRY_ID)
);
create index IDX_QRTZ_FT_INST_JOB_REQ_RCVRY
on qrtz_fired_triggers (SCHED_NAME, INSTANCE_NAME, REQUESTS_RECOVERY);
create index IDX_QRTZ_FT_JG
on qrtz_fired_triggers (SCHED_NAME, JOB_GROUP);
create index IDX_QRTZ_FT_J_G
on qrtz_fired_triggers (SCHED_NAME, JOB_NAME, JOB_GROUP);
create index IDX_QRTZ_FT_TG
on qrtz_fired_triggers (SCHED_NAME, TRIGGER_GROUP);
create index IDX_QRTZ_FT_TRIG_INST_NAME
on qrtz_fired_triggers (SCHED_NAME, INSTANCE_NAME);
create index IDX_QRTZ_FT_T_G
on qrtz_fired_triggers (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP);
create table if not exists qrtz_job_details
(
SCHED_NAME varchar(120) not null,
JOB_NAME varchar(190) not null,
JOB_GROUP varchar(190) not null,
DESCRIPTION varchar(250) null,
JOB_CLASS_NAME varchar(250) not null,
IS_DURABLE varchar(1) not null,
IS_NONCONCURRENT varchar(1) not null,
IS_UPDATE_DATA varchar(1) not null,
REQUESTS_RECOVERY varchar(1) not null,
JOB_DATA blob null,
primary key (SCHED_NAME, JOB_NAME, JOB_GROUP)
);
create index IDX_QRTZ_J_GRP
on qrtz_job_details (SCHED_NAME, JOB_GROUP);
create index IDX_QRTZ_J_REQ_RECOVERY
on qrtz_job_details (SCHED_NAME, REQUESTS_RECOVERY);
create table if not exists qrtz_locks
(
SCHED_NAME varchar(120) not null,
LOCK_NAME varchar(40) not null,
primary key (SCHED_NAME, LOCK_NAME)
);
create table if not exists qrtz_paused_trigger_grps
(
SCHED_NAME varchar(120) not null,
TRIGGER_GROUP varchar(190) not null,
primary key (SCHED_NAME, TRIGGER_GROUP)
);
create table if not exists qrtz_scheduler_state
(
SCHED_NAME varchar(120) not null,
INSTANCE_NAME varchar(190) not null,
LAST_CHECKIN_TIME bigint(13) not null,
CHECKIN_INTERVAL bigint(13) not null,
primary key (SCHED_NAME, INSTANCE_NAME)
);
create table if not exists qrtz_triggers
(
SCHED_NAME varchar(120) not null,
TRIGGER_NAME varchar(190) not null,
TRIGGER_GROUP varchar(190) not null,
JOB_NAME varchar(190) not null,
JOB_GROUP varchar(190) not null,
DESCRIPTION varchar(250) null,
NEXT_FIRE_TIME bigint(13) null,
PREV_FIRE_TIME bigint(13) null,
PRIORITY int null,
TRIGGER_STATE varchar(16) not null,
TRIGGER_TYPE varchar(8) not null,
START_TIME bigint(13) not null,
END_TIME bigint(13) null,
CALENDAR_NAME varchar(190) null,
MISFIRE_INSTR smallint(2) null,
JOB_DATA blob null,
primary key (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP),
constraint qrtz_triggers_ibfk_1
foreign key (SCHED_NAME, JOB_NAME, JOB_GROUP) references qrtz_job_details (SCHED_NAME, JOB_NAME, JOB_GROUP)
);
create table if not exists qrtz_blob_triggers
(
SCHED_NAME varchar(120) not null,
TRIGGER_NAME varchar(190) not null,
TRIGGER_GROUP varchar(190) not null,
BLOB_DATA blob null,
primary key (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP),
constraint qrtz_blob_triggers_ibfk_1
foreign key (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP) references qrtz_triggers (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP)
);
create index SCHED_NAME
on qrtz_blob_triggers (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP);
create table if not exists qrtz_cron_triggers
(
SCHED_NAME varchar(120) not null,
TRIGGER_NAME varchar(190) not null,
TRIGGER_GROUP varchar(190) not null,
CRON_EXPRESSION varchar(120) not null,
TIME_ZONE_ID varchar(80) null,
primary key (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP),
constraint qrtz_cron_triggers_ibfk_1
foreign key (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP) references qrtz_triggers (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP)
);
create table if not exists qrtz_simple_triggers
(
SCHED_NAME varchar(120) not null,
TRIGGER_NAME varchar(190) not null,
TRIGGER_GROUP varchar(190) not null,
REPEAT_COUNT bigint(7) not null,
REPEAT_INTERVAL bigint(12) not null,
TIMES_TRIGGERED bigint(10) not null,
primary key (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP),
constraint qrtz_simple_triggers_ibfk_1
foreign key (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP) references qrtz_triggers (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP)
);
create table if not exists qrtz_simprop_triggers
(
SCHED_NAME varchar(120) not null,
TRIGGER_NAME varchar(190) not null,
TRIGGER_GROUP varchar(190) not null,
STR_PROP_1 varchar(512) null,
STR_PROP_2 varchar(512) null,
STR_PROP_3 varchar(512) null,
INT_PROP_1 int null,
INT_PROP_2 int null,
LONG_PROP_1 bigint null,
LONG_PROP_2 bigint null,
DEC_PROP_1 decimal(13,4) null,
DEC_PROP_2 decimal(13,4) null,
BOOL_PROP_1 varchar(1) null,
BOOL_PROP_2 varchar(1) null,
primary key (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP),
constraint qrtz_simprop_triggers_ibfk_1
foreign key (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP) references qrtz_triggers (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP)
);
create index IDX_QRTZ_T_C
on qrtz_triggers (SCHED_NAME, CALENDAR_NAME);
create index IDX_QRTZ_T_G
on qrtz_triggers (SCHED_NAME, TRIGGER_GROUP);
create index IDX_QRTZ_T_J
on qrtz_triggers (SCHED_NAME, JOB_NAME, JOB_GROUP);
create index IDX_QRTZ_T_JG
on qrtz_triggers (SCHED_NAME, JOB_GROUP);
create index IDX_QRTZ_T_NEXT_FIRE_TIME
on qrtz_triggers (SCHED_NAME, NEXT_FIRE_TIME);
create index IDX_QRTZ_T_NFT_MISFIRE
on qrtz_triggers (SCHED_NAME, MISFIRE_INSTR, NEXT_FIRE_TIME);
create index IDX_QRTZ_T_NFT_ST
on qrtz_triggers (SCHED_NAME, TRIGGER_STATE, NEXT_FIRE_TIME);
create index IDX_QRTZ_T_NFT_ST_MISFIRE
on qrtz_triggers (SCHED_NAME, MISFIRE_INSTR, NEXT_FIRE_TIME, TRIGGER_STATE);
create index IDX_QRTZ_T_NFT_ST_MISFIRE_GRP
on qrtz_triggers (SCHED_NAME, MISFIRE_INSTR, NEXT_FIRE_TIME, TRIGGER_GROUP, TRIGGER_STATE);
create index IDX_QRTZ_T_N_G_STATE
on qrtz_triggers (SCHED_NAME, TRIGGER_GROUP, TRIGGER_STATE);
create index IDX_QRTZ_T_N_STATE
on qrtz_triggers (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP, TRIGGER_STATE);
create index IDX_QRTZ_T_STATE
on qrtz_triggers (SCHED_NAME, TRIGGER_STATE);
2.2.2.持久化配置
quartz:
# 将任务等保存化到数据库
job-store-type: jdbc
# 程序结束时会等待quartz相关的内容结束
wait-for-jobs-to-complete-on-shutdown: true
overwrite-existing-jobs: true
properties:
org:
quartz:
scheduler:
# scheduler的实例名
instanceName: scheduler
#auto:自动生成唯一值
instanceId: AUTO
# 持久化相关
jobStore:
# 升级 springboot 版本,注释 class 属性
# class: org.quartz.impl.jdbcjobstore.JobStoreTX
# 数据库类型
driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
# 表示数据库表名前缀
tablePrefix: QRTZ_
useProperties: false
# 线程池相关
threadPool:
class: org.quartz.simpl.SimpleThreadPool
# 线程数
threadCount: 10
# 线程优先级
threadPriority: 5
threadsInheritContextClassLoaderOfInitializingThread: true
2.2.3.重启实例,持久化成功
文章来源地址https://www.toymoban.com/news/detail-831972.html
到了这里,关于Quartz---Springboot项目整合Quartz---实现内存、数据库持久化的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!