添加定时任务
This commit is contained in:
parent
ade6432bf4
commit
553941d0c4
|
|
@ -44,10 +44,11 @@ public class ScanJob implements Job {
|
|||
for (String key : map.keySet()) {
|
||||
TaskVo vo= JSON.parseObject(map.get(key), TaskVo.class);
|
||||
String status=vo.getStatus();
|
||||
//关闭直接删除
|
||||
if("0".equals(status)){
|
||||
int failNum=vo.getFailNum();
|
||||
//失败次数 成功了
|
||||
if(failNum>vo.getErrTimes() || "1".equals(vo.getIsSuccess()) || "0".equals(status)){
|
||||
service.deleteJob(vo);
|
||||
}else{
|
||||
} else{
|
||||
//添加或者修改定时任务
|
||||
service.addTask(vo);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
package com.bonus.emergencyrap.task.job;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.bonus.emergencyrap.constant.TextConstants;
|
||||
import com.bonus.emergencyrap.task.service.QuartzJobService;
|
||||
import com.bonus.emergencyrap.utils.TextFileUtils;
|
||||
import com.bonus.emergencyrap.utils.UploadFile;
|
||||
import com.bonus.emergencyrap.vo.TaskVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 修改定时任务状态
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class UpdateSuccessJob implements Job {
|
||||
|
||||
@Autowired
|
||||
private QuartzJobService service;
|
||||
|
||||
@Override
|
||||
public void execute(JobExecutionContext context) throws JobExecutionException {
|
||||
try {
|
||||
String filePtah=UploadFile.getFilePath(TextConstants.TASK);
|
||||
Map<String,String> map=TextFileUtils.readFileMaps(filePtah);
|
||||
TextFileUtils.deleteFile(filePtah);
|
||||
TextFileUtils.createFile(filePtah);
|
||||
int i=0;
|
||||
for (String key : map.keySet()) {
|
||||
if(i!=0){
|
||||
TextFileUtils.appendToFile(filePtah,"\n");
|
||||
}
|
||||
i++;
|
||||
String json = map.get(key);
|
||||
TaskVo vo= JSON.parseObject(json, TaskVo.class);
|
||||
vo.setIsSuccess("0");
|
||||
vo.setFailNum(0);
|
||||
appendToFile(filePtah,json,key,true);
|
||||
}
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new JobExecutionException(e);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 添加数据
|
||||
* @param filePath
|
||||
* @param content
|
||||
* @param id
|
||||
* @param append
|
||||
*/
|
||||
public void appendToFile(String filePath,String content,String id,boolean append){
|
||||
if(append){
|
||||
TextFileUtils.appendToFile(filePath,"\n");
|
||||
}else{
|
||||
TextFileUtils.createFile(filePath);
|
||||
}
|
||||
TextFileUtils.appendToFile(filePath,id);
|
||||
TextFileUtils.appendToFile(filePath,"=");
|
||||
TextFileUtils.appendToFile(filePath,content);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ import com.alibaba.fastjson2.JSON;
|
|||
import com.bonus.emergencyrap.task.job.ActuatorJob;
|
||||
import com.bonus.emergencyrap.task.job.ScanJob;
|
||||
|
||||
import com.bonus.emergencyrap.task.job.UpdateSuccessJob;
|
||||
import com.bonus.emergencyrap.vo.TaskVo;
|
||||
import org.quartz.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -28,12 +29,12 @@ public class QuartzJobService {
|
|||
|
||||
|
||||
// 固定任务 用来扫描全部的定时任务
|
||||
public void startSampleJobWithSimpleTrigger() throws SchedulerException {
|
||||
public void startSampleJobWithSimpleTrigger(TaskVo vo) throws SchedulerException {
|
||||
// 定义任务
|
||||
JobDetail jobDetail = JobBuilder.newJob(ScanJob.class)
|
||||
.usingJobData("taskId","555555555555")
|
||||
.withIdentity("scanJob", "scanJobGroup")
|
||||
.withDescription("扫描器")
|
||||
.usingJobData("taskId",vo.getTaskId())
|
||||
.withIdentity(vo.getTaskId(), "scanJobGroup")
|
||||
.withDescription(vo.getTaskName())
|
||||
.storeDurably()
|
||||
.build();
|
||||
|
||||
|
|
@ -42,15 +43,15 @@ public class QuartzJobService {
|
|||
// 从配置文件获取间隔时间,创建触发器
|
||||
Trigger trigger = TriggerBuilder.newTrigger()
|
||||
.withIdentity("scanJobTrigger", "scanJobGroup")
|
||||
.usingJobData("taskId","555555555555")
|
||||
.withDescription("扫描器")
|
||||
.usingJobData("taskId",vo.getTaskId())
|
||||
.withDescription(vo.getTaskName())
|
||||
.startNow()
|
||||
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
|
||||
.withIntervalInSeconds(taskIntervalSeconds)
|
||||
.withIntervalInSeconds(vo.getTime())
|
||||
.repeatForever())
|
||||
.build();
|
||||
scheduler.scheduleJob(jobDetail, trigger);
|
||||
logger.info("已启动任务扫描,间隔时间: {}秒", taskIntervalSeconds);
|
||||
logger.info("已启动任务扫描,间隔时间: {}秒", vo.getTime());
|
||||
} else {
|
||||
logger.info("任务已存在,无需重复启动");
|
||||
}
|
||||
|
|
@ -84,10 +85,19 @@ public class QuartzJobService {
|
|||
|
||||
|
||||
|
||||
// 启动示例任务
|
||||
// 默认任务 无可删除
|
||||
public void startSampleJob() throws SchedulerException {
|
||||
// 可以根据需要选择启动哪种类型的任务
|
||||
startSampleJobWithSimpleTrigger();
|
||||
TaskVo vo=new TaskVo();
|
||||
vo.setTaskId("555555555555");
|
||||
vo.setTime(taskIntervalSeconds);
|
||||
vo.setTaskName("定时任务扫描器");
|
||||
startSampleJobWithSimpleTrigger(vo);
|
||||
TaskVo vo2=new TaskVo();
|
||||
vo2.setTaskId("9999999999999");
|
||||
vo2.setTaskName("刷新任务的失败次数及成功率");
|
||||
vo2.setCorn("0 0 1 * * ?");
|
||||
addTask(vo2);
|
||||
}
|
||||
|
||||
// 暂停任务
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ public class TaskVo {
|
|||
/**
|
||||
* 失败次数
|
||||
*/
|
||||
private String errTimes;
|
||||
private int errTimes;
|
||||
/**
|
||||
* 分辨率
|
||||
*/
|
||||
|
|
@ -102,6 +102,16 @@ public class TaskVo {
|
|||
*/
|
||||
private String corn;
|
||||
|
||||
/**
|
||||
* 是否成功
|
||||
*/
|
||||
private String isSuccess="0";
|
||||
/**
|
||||
* 失败次数
|
||||
*/
|
||||
private int failNum;
|
||||
|
||||
private int time;
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue