代码补全
This commit is contained in:
@@ -4,9 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||||||
import com.zyplayer.doc.data.repository.manage.entity.BackupTask;
|
import com.zyplayer.doc.data.repository.manage.entity.BackupTask;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* 备份任务Mapper 接口
|
||||||
* Mapper 接口
|
|
||||||
* </p>
|
|
||||||
*
|
*
|
||||||
* @author diantu
|
* @author diantu
|
||||||
* @since 2023-03-03
|
* @since 2023-03-03
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
package com.zyplayer.doc.db.framework.utils;
|
package com.zyplayer.doc.db.framework.utils;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.quartz.*;
|
import org.quartz.*;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.slf4j.Logger;
|
||||||
import org.springframework.stereotype.Component;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,10 +11,10 @@ import org.springframework.stereotype.Component;
|
|||||||
* @author diantu
|
* @author diantu
|
||||||
* @since 2023年3月8日
|
* @since 2023年3月8日
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
|
||||||
@Component
|
|
||||||
public class QuartzManagerUtils {
|
public class QuartzManagerUtils {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(QuartzManagerUtils.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 参数传递key
|
* 参数传递key
|
||||||
*/
|
*/
|
||||||
@@ -26,109 +25,103 @@ public class QuartzManagerUtils {
|
|||||||
*/
|
*/
|
||||||
public static final String CLASS_NAME = "com.zyplayer.doc.db.framework.db.job.BackupJob";
|
public static final String CLASS_NAME = "com.zyplayer.doc.db.framework.db.job.BackupJob";
|
||||||
|
|
||||||
/**
|
|
||||||
* 程序调度器
|
|
||||||
*/
|
|
||||||
// @Autowired
|
|
||||||
// private Scheduler scheduler;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加定时任务
|
* 添加定时任务
|
||||||
*/
|
*/
|
||||||
// public void add(Integer id, String cronExpression, String param, Boolean status) {
|
public static void add(Scheduler scheduler,Long id, String cronExpression, String param, Boolean status) {
|
||||||
// try {
|
try {
|
||||||
// // 构建job信息
|
// 构建job信息
|
||||||
// JobDetail jobDetail = JobBuilder.newJob(getClass(CLASS_NAME).getClass()).withIdentity(getKey(id)).usingJobData(PARAM_KEY, param).build();
|
JobDetail jobDetail = JobBuilder.newJob(getClass(CLASS_NAME).getClass()).withIdentity(getKey(id)).usingJobData(PARAM_KEY, param).build();
|
||||||
// // 表达式调度构建器(即任务执行的时间)
|
// 表达式调度构建器(即任务执行的时间)
|
||||||
// CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
|
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
|
||||||
// // 按新的cronExpression表达式构建一个新的trigger
|
// 按新的cronExpression表达式构建一个新的trigger
|
||||||
// CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getKey(id)).withSchedule(scheduleBuilder).build();
|
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getKey(id)).withSchedule(scheduleBuilder).build();
|
||||||
// // 创建定时任务
|
// 创建定时任务
|
||||||
// scheduler.scheduleJob(jobDetail, trigger);
|
scheduler.scheduleJob(jobDetail, trigger);
|
||||||
// // 停止
|
// 停止
|
||||||
// if (!status) {
|
if (!status) {
|
||||||
// stop(id);
|
stop(scheduler,id);
|
||||||
// }
|
}
|
||||||
// } catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// log.error("添加定时任务失败:{}", e.getMessage());
|
logger.error("添加定时任务失败:{}", e.getMessage());
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 编辑定时任务
|
* 编辑定时任务
|
||||||
*/
|
*/
|
||||||
// public void update(Integer id, String cronExpression, String param, Boolean status) {
|
public void update(Scheduler scheduler,Long id, String cronExpression, String param, Boolean status) {
|
||||||
// try {
|
try {
|
||||||
// // 判断是否存在,存在先删除
|
// 判断是否存在,存在先删除
|
||||||
// if (scheduler.checkExists(JobKey.jobKey(getKey(id)))) {
|
if (scheduler.checkExists(JobKey.jobKey(getKey(id)))) {
|
||||||
// scheduler.deleteJob(JobKey.jobKey(getKey(id)));
|
scheduler.deleteJob(JobKey.jobKey(getKey(id)));
|
||||||
// }
|
}
|
||||||
// // 再创建
|
// 再创建
|
||||||
// add(id, cronExpression, param, status);
|
add(scheduler,id, cronExpression, param, status);
|
||||||
// } catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// log.error("修改定时任务失败:{}", e.getMessage());
|
logger.error("修改定时任务失败:{}", e.getMessage());
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 暂停任务
|
* 暂停任务
|
||||||
*/
|
*/
|
||||||
// public void stop(Integer id) {
|
public static void stop(Scheduler scheduler, Long id) {
|
||||||
// try {
|
try {
|
||||||
// scheduler.pauseJob(JobKey.jobKey(getKey(id)));
|
scheduler.pauseJob(JobKey.jobKey(getKey(id)));
|
||||||
// } catch (SchedulerException e) {
|
} catch (SchedulerException e) {
|
||||||
// // 暂停定时任务失败
|
// 暂停定时任务失败
|
||||||
// log.error("暂停定时任务失败:{}", e.getMessage());
|
logger.error("暂停定时任务失败:{}", e.getMessage());
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 恢复任务
|
* 恢复任务
|
||||||
*/
|
*/
|
||||||
// public void start(Integer id) {
|
public void start(Scheduler scheduler,Long id) {
|
||||||
// try {
|
try {
|
||||||
// scheduler.resumeJob(JobKey.jobKey(getKey(id)));
|
scheduler.resumeJob(JobKey.jobKey(getKey(id)));
|
||||||
// } catch (SchedulerException e) {
|
} catch (SchedulerException e) {
|
||||||
// // 暂停定时任务失败
|
// 暂停定时任务失败
|
||||||
// log.error("启动定时任务失败:{}", e.getMessage());
|
logger.error("启动定时任务失败:{}", e.getMessage());
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 立即执行一次
|
* 立即执行一次
|
||||||
*/
|
*/
|
||||||
// public void run(Integer id) {
|
public void run(Scheduler scheduler,Long id) {
|
||||||
// try {
|
try {
|
||||||
// scheduler.triggerJob(JobKey.jobKey(getKey(id)));
|
scheduler.triggerJob(JobKey.jobKey(getKey(id)));
|
||||||
// } catch (SchedulerException e) {
|
} catch (SchedulerException e) {
|
||||||
// // 暂停定时任务失败
|
// 暂停定时任务失败
|
||||||
// log.error("执行定时任务失败:{}", e.getMessage());
|
logger.error("执行定时任务失败:{}", e.getMessage());
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除定时任务
|
* 删除定时任务
|
||||||
*/
|
*/
|
||||||
// public void delete(Integer id) {
|
public void delete(Scheduler scheduler,Long id) {
|
||||||
// try {
|
try {
|
||||||
// // 停止触发器
|
// 停止触发器
|
||||||
// scheduler.pauseTrigger(TriggerKey.triggerKey(getKey(id)));
|
scheduler.pauseTrigger(TriggerKey.triggerKey(getKey(id)));
|
||||||
// // 移除触发器
|
// 移除触发器
|
||||||
// scheduler.unscheduleJob(TriggerKey.triggerKey(getKey(id)));
|
scheduler.unscheduleJob(TriggerKey.triggerKey(getKey(id)));
|
||||||
// // 删除任务
|
// 删除任务
|
||||||
// scheduler.deleteJob(JobKey.jobKey(getKey(id)));
|
scheduler.deleteJob(JobKey.jobKey(getKey(id)));
|
||||||
// } catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// log.error("删除定时任务失败:{}", e.getMessage());
|
logger.error("删除定时任务失败:{}", e.getMessage());
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据类名获取类
|
* 根据类名获取类
|
||||||
*/
|
*/
|
||||||
private Job getClass(String className) throws Exception {
|
private static Job getClass(String className) throws Exception {
|
||||||
Class<?> class1 = Class.forName(className);
|
Class<?> class1 = Class.forName(className);
|
||||||
return (Job) class1.newInstance();
|
return (Job) class1.newInstance();
|
||||||
}
|
}
|
||||||
@@ -136,7 +129,7 @@ public class QuartzManagerUtils {
|
|||||||
/**
|
/**
|
||||||
* 拼接key
|
* 拼接key
|
||||||
*/
|
*/
|
||||||
public String getKey(Integer id) {
|
public static String getKey(Long id) {
|
||||||
return "dbBackUp-" + id;
|
return "dbBackUp-" + id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.zyplayer.doc.db.service.backup;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 备份任务
|
||||||
|
*
|
||||||
|
* @author diantu
|
||||||
|
* @since 2023年3月8日
|
||||||
|
*/
|
||||||
|
public interface BackupTaskService {
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package com.zyplayer.doc.db.service.backup.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.zyplayer.doc.data.repository.manage.entity.BackupTask;
|
||||||
|
import com.zyplayer.doc.data.repository.manage.mapper.BackupTaskMapper;
|
||||||
|
import com.zyplayer.doc.db.framework.utils.QuartzManagerUtils;
|
||||||
|
import com.zyplayer.doc.db.service.backup.BackupTaskService;
|
||||||
|
import org.quartz.Scheduler;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 备份任务实现
|
||||||
|
*
|
||||||
|
* @author diantu
|
||||||
|
* @since 2023年3月8日
|
||||||
|
*/
|
||||||
|
public class BackupTaskServiceImpl implements BackupTaskService {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(BackupTaskServiceImpl.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Scheduler scheduler;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BackupTaskMapper backupTaskMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统启动执行
|
||||||
|
*/
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
List<BackupTask> list = backupTaskMapper.selectList(null);
|
||||||
|
if (CollectionUtil.isNotEmpty(list)) {
|
||||||
|
for (BackupTask item : list) {
|
||||||
|
try {
|
||||||
|
QuartzManagerUtils.add(scheduler,item.getId(), item.getCron(), JSON.toJSONString(item.getParam()), item.getStatus());
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user