QUARTZ 建表语句
QUARTZ 文档地址
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-quartzartifactId>
<version>2.2.6.RELEASEversion>
dependency>
或者
implementation 'org.springframework.boot:spring-boot-starter-quartz'
spring:
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
@Configuration
public class QuartzConfig implements SchedulerFactoryBeanCustomizer {
@Autowired
private DataSource quartzSource;
@Override
public void customize(SchedulerFactoryBean schedulerFactoryBean) {
// 启动延时
schedulerFactoryBean.setStartupDelay(2);
// 自动启动任务调度
schedulerFactoryBean.setAutoStartup(true);
// 是否覆盖现有作业定义
schedulerFactoryBean.setOverwriteExistingJobs(true);
// 配置数据源
//schedulerFactoryBean.setDataSource(quartzSource);
}
}
public class TestJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("定时任务开始");
}
}
public class TmsQuartzServiceImpl implements TmsQuartzService {
@Autowired
private Scheduler scheduler;
@Override
public void insertTask(TmsQuartzAddDto quartz) {
String jobName = quartz.getJobName();
String triggerName = quartz.getJobName();
String description = quartz.getDescription();
String jobClassNameKey = quartz.getJobClassName();
String cron = quartz.getCron();
String jobClassName = quartz.getJobClassName();
String jobGroup = quartz.getJobGroup();
String triggerGroup = quartz.getTriggerGroup();
Class<? extends Job> jobClass;
JobKey jobKey = new JobKey(jobName, jobGroup);
TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroup);
Assert.isTrue(CronExpression.isValidExpression(cron), () -> new CustomAssertException("cron表达式错误"));
try {
JobDetail newJobDetail = scheduler.getJobDetail(jobKey);
Trigger newTrigger = scheduler.getTrigger(triggerKey);
Assert.isNull(newJobDetail, () -> new CustomAssertException("【job】:{}已存在", jobKey));
Assert.isNull(newTrigger, () -> new CustomAssertException("【trigger】:{}已存在", triggerKey));
//构建job信息
jobClass = (Class<? extends Job>) Class.forName(jobClassName);
JobDetail jobDetail = JobBuilder.newJob(jobClass)
.withIdentity(jobKey)
.withDescription(description)
.build();
//构建一个调度器
CronTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(triggerKey)
.startNow()//立即生效
.withSchedule(CronScheduleBuilder.cronSchedule(cron).
withMisfireHandlingInstructionFireAndProceed())//触发器类型
.build();
//启动
scheduler.start();
scheduler.scheduleJob(jobDetail, trigger);
} catch (SchedulerException | ClassNotFoundException e) {
log.error("系统异常", e);
}
}
}
public void pauseTask(TmsQuartzBaseDto quartz) {
String jobName = quartz.getJobName();
String jobGroup = quartz.getJobGroupName();
JobKey jobKey = new JobKey(jobName, jobGroup);
try {
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
//暂停任务
scheduler.pauseJob(jobKey);
} catch (SchedulerException e) {
log.error("系统异常", e);
}
}
public void resumeTask(TmsQuartzBaseDto quartz) {
String jobName = quartz.getJobName();
String jobGroup = quartz.getJobGroupName();
JobKey jobKey = new JobKey(jobName, jobGroup);
try {
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
Assert.notNull(jobDetail, () -> new CustomAssertException(MSG_KEY, jobKey));
//恢复任务
scheduler.resumeJob(jobKey);
} catch (SchedulerException e) {
log.error("系统异常", e);
}
}
public void runOneTask(TmsQuartzBaseDto quartz) {
String jobName = quartz.getJobName();
String jobGroup = quartz.getJobGroupName();
JobKey jobKey = new JobKey(jobName, jobGroup);
try {
scheduler.triggerJob(jobKey);
} catch (SchedulerException e) {
log.error("运行失败", e);
}
}
public void deleteTask(TmsQuartzDeleteDto quartz) {
String jobName = quartz.getJobName();
String jobGroup = quartz.getJobGroupName();
String triggerName = quartz.getTriggerName();
String triggerGroup = quartz.getTriggerGroupName();
TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroup);
JobKey jobKey = new JobKey(jobName, jobGroup);
//停止触发器
try {
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
Trigger trigger = scheduler.getTrigger(triggerKey);
Assert.notNull(trigger, () -> new CustomAssertException("【trigger】:{}不存在", triggerKey));
Assert.notNull(jobDetail, () -> new CustomAssertException(MSG_KEY, jobKey));
//暂停触发器
scheduler.pauseTrigger(triggerKey);
//移除触发器
scheduler.unscheduleJob(triggerKey);
//移除任务
scheduler.deleteJob(jobKey);
} catch (SchedulerException e) {
log.error("系统异常", e);
}
}
public void updateCron(TmsQuartzUpdateCronDto quartz) {
String cron = quartz.getCron();
String triggerName = quartz.getJobName();
String jobClassName = quartz.getJobClassName();
String triggerGroup = quartz.getTriggerGroup();
Assert.isTrue(CronExpression.isValidExpression(cron), () -> new CustomAssertException("cron表达式错误"));
TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroup);
Trigger trigger;
try {
trigger = scheduler.getTrigger(triggerKey);
Assert.notNull(trigger, () -> new CustomAssertException("【trigger】:{}不存在", triggerKey));
CronTrigger cronTrigger = (CronTrigger) trigger;
String cronExpression = cronTrigger.getCronExpression();
if (!cronExpression.equalsIgnoreCase(cron)) {
//重新构建调度器
CronTrigger newCronTrigger = TriggerBuilder
.newTrigger()
.withIdentity(triggerName, triggerGroup)
.withSchedule(CronScheduleBuilder.cronSchedule(cron).
withMisfireHandlingInstructionFireAndProceed())
.build();
// 按新的trigger重新设置job执行
scheduler.rescheduleJob(triggerKey, newCronTrigger);
}
} catch (SchedulerException e) {
log.error("系统异常", e);
}
}