SpringBoot整合quartz 实现自定义定时器
一篇入门quartz,话不多说,直接上代码
pom文件,版本跟着父版本走即可
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--quartz-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
</dependencies>
yml文件
server:
port: 21115
spring:
application:
name: base-quartz-p21115
datasource:
url: jdbc:mysql://localhost:3306/boot-base?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
quartz:
#相关属性配置
properties:
org:
quartz:
scheduler:
#调度器实例名称
instanceName: clusteredScheduler
#调度器实例编号自动生成
instanceId: AUTO
jobStore:
#持久化方式配置
class: org.quartz.impl.jdbcjobstore.JobStoreTX
#持久化方式配置数据驱动,MySQL数据库
driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#quartz相关数据表前缀名
tablePrefix: QRTZ_
#开启分布式部署
isClustered: true
#分布式节点有效性检查时间间隔,单位:毫秒
clusterCheckinInterval: 10000
#配置是否使用
useProperties: false
threadPool:
#线程池实现类
class: org.quartz.simpl.SimpleThreadPool
#执行最大并发线程数量
threadCount: 10
#线程优先级
threadPriority: 5
#配置是否启动自动加载数据库内的定时任务,默认true
threadsInheritContextClassLoaderOfInitializingThread: true
#数据库方式
job-store-type: jdbc
#初始化表结构 放开启动项目时会自动建quartz需要的表
# jdbc:
# initialize-schema: always
启动项目时会创建以下数据库表 表含义
qrtz_blob_triggers : 以Blob 类型存储的触发器。 qrtz_calendars:存放日历信息, quartz可配置一个日历来指定一个时间范围。 qrtz_cron_triggers:存放cron类型的触发器。 qrtz_fired_triggers:存放已触发的触发器。 qrtz_job_details:存放一个jobDetail信息。 .qrtz_job_listeners:job监听器。 qrtz_locks: 存储程序的悲观锁的信息(假如使用了悲观锁)。 qrtz_paused_trigger_graps:存放暂停掉的触发器。 qrtz_scheduler_state:调度器状态。 qrtz_simple_triggers:简单触发器的信息。 qrtz_trigger_listeners:触发器监听器。 qrtz_triggers:触发器的基本信息。
和系统业务集成时需要关注这些表
qrtz_triggers,qrtz_cron_triggers,qrtz_fired_triggers,qrtz_job_details。
创建一个任务类
public class QuartzTask extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("定时任务执行时间:"+new Date().toLocaleString());
}
}
实体类
@Data
public class QuartzBean {
private String id;
private String jobName;
private String jobClass;
private Integer status;
private String cronExpression;
}
定时器接口类
public interface quartzService {
void addTask(QuartzBean quartzBean);
void pauseTask(QuartzBean quartzBean);
void resumeTask(QuartzBean quartzBean);
void runOne(QuartzBean quartzBean);
void editTask(QuartzBean quartzBean);
void deleteTask(QuartzBean quartzBean);
}
接口实现类
@Service
public class quartzServiceImpl implements quartzService {
@Autowired
private Scheduler scheduler;
@Override
public void addTask(QuartzBean quartzBean) {
JobDetail jobDetail = null;
try {
jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(quartzBean.getJobClass())).withIdentity(quartzBean.getJobName()).build();
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzBean.getCronExpression());
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(quartzBean.getId()).withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(jobDetail, trigger);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
@Override
public void pauseTask(QuartzBean quartzBean) {
try {
scheduler.pauseJob(JobKey.jobKey(quartzBean.getJobName()));
} catch (SchedulerException e) {
e.printStackTrace();
}
}
@Override
public void resumeTask(QuartzBean quartzBean) {
try {
scheduler.resumeJob(JobKey.jobKey(quartzBean.getJobName()));
} catch (SchedulerException e) {
e.printStackTrace();
}
}
@Override
public void runOne(QuartzBean quartzBean) {
try {
scheduler.triggerJob(JobKey.jobKey(quartzBean.getJobName()));
} catch (SchedulerException e) {
e.printStackTrace();
}
}
@Override
public void editTask(QuartzBean quartzBean) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(quartzBean.getJobName());
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzBean.getCronExpression());
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
scheduler.rescheduleJob(triggerKey, trigger);
} catch (SchedulerException e) {
System.out.println("更新定时任务出错:"+e.getMessage());
}
}
@Override
public void deleteTask(QuartzBean quartzBean) {
try {
scheduler.deleteJob(JobKey.jobKey(quartzBean.getJobName()));
} catch (SchedulerException e) {
e.printStackTrace();
}
}
Quartz常用方法整合大概就这么多,其他的就可以举一反三了,对你有帮助,请点赞评论下!
|