新增任务异常统一处理,节点工具类,配置工具类
parent
cdead16f6c
commit
af9f5d64bb
|
@ -1,16 +1,14 @@
|
|||
package com.muyu.quest.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.muyu.common.core.annotation.Excel;
|
||||
import com.muyu.common.core.validation.custom.IsSysFieldsType;
|
||||
import com.muyu.common.core.validation.custom.IsSysNodeType;
|
||||
import com.muyu.common.core.web.domain.BaseEntity;
|
||||
import lombok.*;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
|
||||
/**
|
||||
* 节点配置对象 node_disposition
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package com.muyu.quest.advice;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: TaskAdvice
|
||||
* @Description: 任务统一异常处理
|
||||
* @CreatedDate: 2024/9/1 下午4:12
|
||||
* @FilePath: com.muyu.quest.advice
|
||||
*/
|
||||
|
||||
|
||||
import com.alibaba.nacos.api.model.v2.Result;
|
||||
import com.muyu.quest.exception.TaskException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: TaskAdvice
|
||||
* @Description: 任务统一异常处理
|
||||
* @CreatedDate: 2024/9/1 下午4:12
|
||||
* @FilePath: com.muyu.quest.advice
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class TaskAdvice {
|
||||
@ExceptionHandler(TaskException.class)
|
||||
public Result<String> commonException(TaskException e) {
|
||||
log.error("任务异常处理,异常信息:[{}]",e.toString());
|
||||
return Result.failure(e.getMessage());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.muyu.quest.exception;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: TaskException
|
||||
* @Description: 任务统一异常
|
||||
* @CreatedDate: 2024/9/1 下午4:11
|
||||
* @FilePath: com.muyu.quest.exception
|
||||
*/
|
||||
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: TaskException
|
||||
* @Description: 任务统一异常
|
||||
* @CreatedDate: 2024/9/1 下午4:11
|
||||
* @FilePath: com.muyu.quest.exception
|
||||
*/
|
||||
|
||||
@Component
|
||||
public class TaskException extends RuntimeException {
|
||||
public TaskException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TaskException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public TaskException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public TaskException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
protected TaskException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
}
|
|
@ -1,13 +1,22 @@
|
|||
package com.muyu.quest.service.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.common.core.utils.StringUtils;
|
||||
import com.muyu.quest.domain.Node;
|
||||
import com.muyu.quest.domain.NodeDisposition;
|
||||
import com.muyu.quest.exception.TaskException;
|
||||
import com.muyu.quest.req.NodeReq;
|
||||
import com.muyu.quest.req.TaskReq;
|
||||
import com.muyu.quest.resp.TaskResp;
|
||||
import com.muyu.quest.service.INodeDispositionService;
|
||||
import com.muyu.quest.service.INodeService;
|
||||
import com.muyu.quest.utils.NodeUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.muyu.quest.mapper.TaskMapper;
|
||||
import com.muyu.quest.domain.Task;
|
||||
|
@ -26,6 +35,11 @@ import javax.annotation.Resource;
|
|||
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task>
|
||||
implements TaskService {
|
||||
|
||||
@Resource
|
||||
private INodeService nodeService;
|
||||
@Resource
|
||||
private INodeDispositionService dispositionService;
|
||||
|
||||
/**
|
||||
* 查询任务
|
||||
*
|
||||
|
@ -80,11 +94,6 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task>
|
|||
this.removeBatchByIds(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String taskCode) {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询任务列表
|
||||
*
|
||||
|
@ -110,5 +119,50 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task>
|
|||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行任务
|
||||
* 获取任务所有节点配置信息 拼接sql语句
|
||||
* @param taskCode 任务编码
|
||||
* @return 成功与否
|
||||
*/
|
||||
@Override
|
||||
public String execute(String taskCode) {
|
||||
NodeReq nodeReq = new NodeReq();
|
||||
nodeReq.setTaskCode(taskCode);
|
||||
List<Node> nodes = nodeService.selectNodeList(nodeReq);
|
||||
HashMap<String, List<Node>> nodeMap = NodeUtils.nodeInit(nodes);
|
||||
// 从开始节点 开始执行
|
||||
// 1.获取开始节点编码
|
||||
Node startNode = nodeMap.get("start").get(0);
|
||||
// 2.查询上一级节点为开始节点的节点
|
||||
List<Node> nextNode = NodeUtils.getNextNode(startNode, nodes);
|
||||
if (nextNode == null || nextNode.isEmpty()){
|
||||
throw new TaskException("任务无意义");
|
||||
}
|
||||
// 3.处理节点
|
||||
nextNode.forEach(node -> {
|
||||
// 3.1 查询节点配置信息
|
||||
NodeDisposition nodeDisposition = new NodeDisposition();
|
||||
nodeDisposition.setNodeCode(node.getNodeCode());
|
||||
List<NodeDisposition> dispList = dispositionService.selectNodeDispositionList(nodeDisposition);
|
||||
if (dispList.isEmpty()){
|
||||
throw new TaskException("节点 "+node.getNodeName()+" 配置信息为空");
|
||||
}
|
||||
String type = node.getNodeType();
|
||||
if (StringUtils.equals(type,"table")) {
|
||||
HashMap<String, List<NodeDisposition>> dispMap = new HashMap<>();
|
||||
// 整理所有配置
|
||||
dispList.forEach(disp -> {
|
||||
List<NodeDisposition> dispositions = dispMap.get(disp.getDispKey());
|
||||
if (dispositions == null || dispositions.isEmpty()) {
|
||||
dispositions = new ArrayList<>();
|
||||
}
|
||||
dispositions.add(disp);
|
||||
dispMap.put(disp.getDispKey(), dispositions);
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package com.muyu.quest.utils;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: DispUtils
|
||||
* @Description: 节点配置工具
|
||||
* @CreatedDate: 2024/9/1 下午4:27
|
||||
* @FilePath: com.muyu.quest.utils
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: DispUtils
|
||||
* @Description: 节点配置工具
|
||||
* @CreatedDate: 2024/9/1 下午4:27
|
||||
* @FilePath: com.muyu.quest.utils
|
||||
*/
|
||||
|
||||
public class DispUtils {
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
package com.muyu.quest.utils;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: NodeUtils
|
||||
* @Description: 节点处理工具
|
||||
* @CreatedDate: 2024/9/1 下午4:27
|
||||
* @FilePath: com.muyu.quest.utils
|
||||
*/
|
||||
|
||||
|
||||
import com.muyu.common.core.utils.StringUtils;
|
||||
import com.muyu.quest.domain.Node;
|
||||
import com.muyu.quest.exception.TaskException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: NodeUtils
|
||||
* @Description: 节点处理工具
|
||||
* @CreatedDate: 2024/9/1 下午4:27
|
||||
* @FilePath: com.muyu.quest.utils
|
||||
*/
|
||||
|
||||
public class NodeUtils {
|
||||
/**
|
||||
* 节点初始化
|
||||
* 将所有节点按节点类型分类
|
||||
* 并判断任务流程是否完整
|
||||
* @param nodes 节点列表
|
||||
* @return 节点Map
|
||||
*/
|
||||
public static HashMap<String, List<Node>> nodeInit(List<Node> nodes){
|
||||
if (nodes == null || nodes.isEmpty()){
|
||||
throw new TaskException("节点列表为空");
|
||||
}
|
||||
HashMap<String, List<Node>> nodeMap = new HashMap<>();
|
||||
// 整理所有节点
|
||||
nodes.forEach(node -> {
|
||||
List<Node> nodeList = nodeMap.get(node.getNodeType());
|
||||
if (nodeList == null || nodeList.isEmpty()) {
|
||||
nodeList = new ArrayList<>();
|
||||
}
|
||||
nodeList.add(node);
|
||||
nodeMap.put(node.getNodeType(), nodeList);
|
||||
});
|
||||
List<Node> start = nodeMap.get("start");
|
||||
List<Node> end = nodeMap.get("end");
|
||||
System.out.println(nodeMap);
|
||||
if (start == null || start.isEmpty()){
|
||||
throw new TaskException("未找到开始节点");
|
||||
}
|
||||
if (end == null || end.isEmpty()){
|
||||
throw new TaskException("未找到结束节点");
|
||||
}
|
||||
return nodeMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下级节点
|
||||
* @param node 当前节点
|
||||
* @param nodes 所有节点
|
||||
* @return 下级节点列表
|
||||
*/
|
||||
public static List<Node> getNextNode(Node node, List<Node> nodes){
|
||||
if (nodes == null || nodes.isEmpty()){
|
||||
throw new TaskException("节点列表为空");
|
||||
}else if (node == null){
|
||||
throw new TaskException("当前节点为空");
|
||||
}
|
||||
return nodes
|
||||
.stream()
|
||||
.filter(nodeIndex -> StringUtils.equals(node.getNodeCode() ,nodeIndex.getNodePreCode()))
|
||||
.filter(nodeIndex -> !StringUtils.equals(node.getNodeType() , "end"))
|
||||
.toList();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue