定时任务修正
This commit is contained in:
@@ -10,7 +10,6 @@ import org.xyzh.common.core.page.PageParam;
|
||||
import org.xyzh.common.core.page.PageRequest;
|
||||
import org.xyzh.common.dto.crontab.TbCrontabTask;
|
||||
import org.xyzh.common.dto.crontab.TbCrontabLog;
|
||||
import org.xyzh.common.utils.IDUtils;
|
||||
import org.xyzh.crontab.pojo.CrontabItem;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
@@ -19,7 +18,6 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import org.xyzh.common.utils.spring.SpringContextUtil;
|
||||
import org.xyzh.crontab.config.CrontabProperties;
|
||||
|
||||
import java.util.Date;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@@ -57,7 +55,6 @@ public class CrontabController {
|
||||
method->{
|
||||
method.setClazz(null);
|
||||
method.setExcuete_method(null);
|
||||
method.setPath(null);
|
||||
}));
|
||||
rd.success("ok", props.getItems());
|
||||
} catch (Exception e) {
|
||||
@@ -75,25 +72,41 @@ public class CrontabController {
|
||||
public ResultDomain<TbCrontabTask> createCrontab(@RequestBody TbCrontabTask crontabItem) {
|
||||
ResultDomain<TbCrontabTask> rd = new ResultDomain<>();
|
||||
try {
|
||||
// 根据taskGroup和methodName查找配置并填充beanName和methodName
|
||||
if (crontabItem.getBeanName() == null || crontabItem.getBeanName().isEmpty()) {
|
||||
CrontabItem.CrontabMethod method = findMethodByTaskGroupAndMethodName(
|
||||
crontabItem.getTaskGroup(),
|
||||
crontabItem.getMethodName()
|
||||
);
|
||||
if (method != null) {
|
||||
crontabItem.setBeanName(method.getClazz()); // 设置Bean名称
|
||||
crontabItem.setMethodName(method.getExcuete_method()); // 设置执行方法名
|
||||
JSONObject methodParams = JSON.parseObject(crontabItem.getMethodParams());
|
||||
methodParams.put("scriptPath", method.getPath());
|
||||
crontabItem.setMethodParams(methodParams.toJSONString());
|
||||
|
||||
} else {
|
||||
rd.fail("未找到对应的配置: taskGroup=" + crontabItem.getTaskGroup()
|
||||
+ ", methodName=" + crontabItem.getMethodName());
|
||||
return rd;
|
||||
}
|
||||
// 根据前端传入的taskGroup和methodName(都是中文显示名)查找配置
|
||||
if (crontabItem.getTaskGroup() == null || crontabItem.getTaskGroup().isEmpty()) {
|
||||
rd.fail("任务分组不能为空");
|
||||
return rd;
|
||||
}
|
||||
if (crontabItem.getMethodName() == null || crontabItem.getMethodName().isEmpty()) {
|
||||
rd.fail("方法名称不能为空");
|
||||
return rd;
|
||||
}
|
||||
|
||||
// 根据taskGroup和methodName查找配置
|
||||
CrontabItem.CrontabMethod method = findMethodByTaskGroupAndMethodName(
|
||||
crontabItem.getTaskGroup(),
|
||||
crontabItem.getMethodName()
|
||||
);
|
||||
|
||||
if (method != null) {
|
||||
// 填充beanName和实际的Java方法名
|
||||
crontabItem.setBeanName(method.getClazz());
|
||||
crontabItem.setMethodName(method.getExcuete_method());
|
||||
|
||||
// 将scriptPath添加到methodParams中
|
||||
JSONObject methodParams = JSON.parseObject(crontabItem.getMethodParams());
|
||||
methodParams.put("scriptPath", method.getPath());
|
||||
crontabItem.setMethodParams(methodParams.toJSONString());
|
||||
|
||||
logger.info("创建任务 - taskGroup: {}, methodName: {}, beanName: {}, excuete_method: {}, scriptPath: {}",
|
||||
crontabItem.getTaskGroup(), method.getName(), method.getClazz(),
|
||||
method.getExcuete_method(), method.getPath());
|
||||
} else {
|
||||
rd.fail("未找到对应的配置: taskGroup=" + crontabItem.getTaskGroup()
|
||||
+ ", methodName=" + crontabItem.getMethodName());
|
||||
return rd;
|
||||
}
|
||||
|
||||
return crontabService.createTask(crontabItem);
|
||||
} catch (Exception e) {
|
||||
logger.error("创建定时任务失败", e);
|
||||
@@ -132,21 +145,46 @@ public class CrontabController {
|
||||
public ResultDomain<TbCrontabTask> updateCrontab(@RequestBody TbCrontabTask crontabItem) {
|
||||
ResultDomain<TbCrontabTask> rd = new ResultDomain<>();
|
||||
try {
|
||||
// 根据taskGroup和methodName查找配置并填充beanName和methodName
|
||||
if (crontabItem.getBeanName() == null || crontabItem.getBeanName().isEmpty()) {
|
||||
CrontabItem.CrontabMethod method = findMethodByTaskGroupAndMethodName(
|
||||
crontabItem.getTaskGroup(),
|
||||
crontabItem.getMethodName()
|
||||
);
|
||||
if (method != null) {
|
||||
crontabItem.setBeanName(method.getClazz()); // 设置Bean名称
|
||||
crontabItem.setMethodName(method.getExcuete_method()); // 设置执行方法名
|
||||
} else {
|
||||
rd.fail("未找到对应的配置: taskGroup=" + crontabItem.getTaskGroup()
|
||||
+ ", methodName=" + crontabItem.getMethodName());
|
||||
return rd;
|
||||
}
|
||||
// 确保id字段正确传递(用于数据库更新)
|
||||
if (crontabItem.getTaskId() != null && crontabItem.getID() == null) {
|
||||
crontabItem.setID(crontabItem.getTaskId());
|
||||
}
|
||||
|
||||
// 根据前端传入的taskGroup和methodName(都是中文显示名)查找配置
|
||||
if (crontabItem.getTaskGroup() == null || crontabItem.getTaskGroup().isEmpty()) {
|
||||
rd.fail("任务分组不能为空");
|
||||
return rd;
|
||||
}
|
||||
if (crontabItem.getMethodName() == null || crontabItem.getMethodName().isEmpty()) {
|
||||
rd.fail("方法名称不能为空");
|
||||
return rd;
|
||||
}
|
||||
|
||||
// 根据taskGroup和methodName查找配置
|
||||
CrontabItem.CrontabMethod method = findMethodByTaskGroupAndMethodName(
|
||||
crontabItem.getTaskGroup(),
|
||||
crontabItem.getMethodName()
|
||||
);
|
||||
|
||||
if (method != null) {
|
||||
// 填充beanName和实际的Java方法名
|
||||
crontabItem.setBeanName(method.getClazz());
|
||||
crontabItem.setMethodName(method.getExcuete_method());
|
||||
|
||||
// 将scriptPath添加到methodParams中
|
||||
JSONObject methodParams = JSON.parseObject(crontabItem.getMethodParams());
|
||||
methodParams.put("scriptPath", method.getPath());
|
||||
crontabItem.setMethodParams(methodParams.toJSONString());
|
||||
|
||||
logger.info("更新任务 - id: {}, taskGroup: {}, methodName: {}, beanName: {}, excuete_method: {}, scriptPath: {}",
|
||||
crontabItem.getID(), crontabItem.getTaskGroup(), method.getName(), method.getClazz(),
|
||||
method.getExcuete_method(), method.getPath());
|
||||
} else {
|
||||
rd.fail("未找到对应的配置: taskGroup=" + crontabItem.getTaskGroup()
|
||||
+ ", methodName=" + crontabItem.getMethodName());
|
||||
return rd;
|
||||
}
|
||||
|
||||
return crontabService.updateTask(crontabItem);
|
||||
} catch (Exception e) {
|
||||
logger.error("更新定时任务失败", e);
|
||||
|
||||
@@ -18,8 +18,6 @@ import org.xyzh.crontab.mapper.CrontabTaskMapper;
|
||||
import org.xyzh.crontab.mapper.CrontabLogMapper;
|
||||
import org.xyzh.crontab.scheduler.SchedulerManager;
|
||||
import org.xyzh.api.system.permission.ResourcePermissionService;
|
||||
import org.xyzh.common.dto.user.TbSysUser;
|
||||
import org.xyzh.common.dto.user.TbSysUserDeptRole;
|
||||
import org.xyzh.common.core.enums.ResourceType;
|
||||
import org.xyzh.system.utils.LoginUtil;
|
||||
|
||||
@@ -122,7 +120,7 @@ public class CrontabServiceImpl implements CrontabService {
|
||||
public ResultDomain<TbCrontabTask> updateTask(TbCrontabTask task) {
|
||||
ResultDomain<TbCrontabTask> resultDomain = new ResultDomain<>();
|
||||
try {
|
||||
if (task.getID() == null) {
|
||||
if (task.getTaskId() == null) {
|
||||
resultDomain.fail("任务ID不能为空");
|
||||
return resultDomain;
|
||||
}
|
||||
@@ -140,7 +138,7 @@ public class CrontabServiceImpl implements CrontabService {
|
||||
logger.info("更新定时任务成功: {}", task.getTaskName());
|
||||
|
||||
// 重新调度任务
|
||||
TbCrontabTask dbTask = taskMapper.selectTaskById(task.getID());
|
||||
TbCrontabTask dbTask = taskMapper.selectTaskById(task.getTaskId());
|
||||
if (dbTask != null && dbTask.getStatus() == 1) {
|
||||
schedulerManager.rescheduleTask(dbTask);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user