Commit 84dcaa58 by zhouhaibao

[+]执行器处理

1 parent 72bf77e2
......@@ -17,4 +17,13 @@ public final class Constants extends BasicConstants {
public static final Charset CHARSET = Charset.forName("utf-8");
public static final String RES_SUCCESS = "SUCCESS";
public static final String RES_KEY = "code";
public static final String RES_DATE_KEY = "data";
public static final String RES_FAIL = "FAIL";
public static final String RES_FAIL_MESSAGE = "FAIL_MESSAGE";
}
......@@ -29,119 +29,121 @@ import lombok.extern.slf4j.Slf4j;
@Component
public class SopInstNodeExecuteJobHandler {
@Resource
private SopService sopService;
@XxlJob(SopXxlJobUtil.SOP_NODE_EXECUTE_JOB_ID)
public void handler(String param1) throws Exception {
String json = XxlJobHelper.getJobParam();
SopInstNodeParam param = JSONUtil.toBean(json, SopInstNodeParam.class);
SopXxlJobUtil.log(log, "【执行Sop节点】{}", param);
SopStruct sop = sopService.loadSopByInstNodeId(param.getInstNodeId());
// 执行sop里面的instNodeId节点。
SopInstNode node = sop.getInstNodeMap().get(param.getInstNodeId());
boolean avaliable = checkPreIds(sop, node, param);
if (!avaliable) {
return;
}
SopXxlJobUtil.log(log, "【执行Sop节点】{}【{}:{}】 开始执行......", sop.getInst().getInstName(), node.getNodeId(), node.getNodeName());
// 节点执行
execute(sop, node);
// 记录节点执行结果
}
/**
* 检查本节点的前置任务是否完成,如果前置任务是异步任务,则忽略
*
* @param sop
* @param node
* @param param
* @return
*/
private boolean checkPreIds(SopStruct sop, SopInstNode node, SopInstNodeParam param) {
Pair<Set<Integer>, Set<Integer>> pair = sop.getInstLinkMap().get(node.getNodeId());
// 前置任务为空,直接执行
if (pair.getKey().isEmpty()) {
return true;
}
// 前置任务已完成,可以执行
if (param.getFinishedIds().containsAll(pair.getKey())) {
return true;
}
// 前置任务是异步任务,可以执行
for (Integer preId : pair.getKey()) {
SopInstNode preNode = sop.getInstNodeMap().get(preId);
if (preNode != null && preNode.getNodeAsync() != 1 && !param.getFinishedIds().contains(preId)) {
SopXxlJobUtil.log(log, "【执行Sop节点】【{}:{}】,存在前置任务未完成:【{}:{}】", sop.getInst().getInstId(), sop.getInst().getInstName(),
preNode.getInstId(), preNode.getNodeName());
return false;
}
}
return true;
}
/**
* 节点执行时,申请sop锁
*
* @param node 节点信息
* @param sop 执行器
*/
public void execute(SopStruct sop, SopInstNode node) {
SopNodeType type = SopNodeType.fromBusType(node.getNodeType());
SopNodeExecutor instance = SopNodeExecutorFactory.getInstance(type.getHanderImp(), SopNodeExecutor.class);
// 执行sop节点前,锁定sop
if (SopHelper.lockInst(sop.getInst())) {
try {
// 重新加载sop
sop = reloadAndCreateBatch(sop, node);
// 执行sop节点
ExecuteStruct result = instance.execute(sop, node);
updateBatch(sop, node, result);
updateNextNodes(sop, node);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
SopHelper.unlockInst(sop.getInst());
}
} else {
SopXxlJobUtil.log(log, "【执行Sop节点】【{}:{}】,申请实例锁失败,本次不执行!", sop.getInst().getInstId(), sop.getInst().getInstName());
return;
}
}
private void updateNextNodes(SopStruct sop, SopInstNode node) {
SopXxlJobUtil.finishSopNodeJob(sop, node);
}
private void updateBatch(SopStruct sop, SopInstNode node, ExecuteStruct result) {
SopInstNodeBatch nodeBatch = sop.getNodeBatch();
nodeBatch.setSnapshot(JSONUtil.toJsonStr(result));
nodeBatch.setUpdateTime(new Date());
sopService.updateInstNodeBatch(nodeBatch);
}
private SopStruct reloadAndCreateBatch(SopStruct sop, SopInstNode node) {
sop = sopService.loadSop(sop.getInst().getInstId());
// 创建节点执行批次
SopInstNodeBatch curBatch = sopService.createNodeBatch(sop.getInst().getBatchNo(), node.getInstNodeId());
sop.setNodeBatch(curBatch);
return sop;
}
@Resource
private SopService sopService;
@XxlJob(SopXxlJobUtil.SOP_NODE_EXECUTE_JOB_ID)
public void handler(String param1) throws Exception {
String json = XxlJobHelper.getJobParam();
SopInstNodeParam param = JSONUtil.toBean(json, SopInstNodeParam.class);
SopXxlJobUtil.log(log, "【执行Sop节点】{}", param);
SopStruct sop = sopService.loadSopByInstNodeId(param.getInstNodeId());
// 执行sop里面的instNodeId节点。
SopInstNode node = sop.getInstNodeMap().get(param.getInstNodeId());
boolean avaliable = checkPreIds(sop, node, param);
if (!avaliable) {
return;
}
SopXxlJobUtil.log(log, "【执行Sop节点】{}【{}:{}】 开始执行......", sop.getInst().getInstName(), node.getNodeId(), node.getNodeName());
// 节点执行
execute(sop, node);
// 记录节点执行结果
}
/**
* 检查本节点的前置任务是否完成,如果前置任务是异步任务,则忽略
*
* @param sop
* @param node
* @param param
* @return
*/
private boolean checkPreIds(SopStruct sop, SopInstNode node, SopInstNodeParam param) {
Pair<Set<Integer>, Set<Integer>> pair = sop.getInstLinkMap().get(node.getNodeId());
// 前置任务为空,直接执行
if (pair.getKey().isEmpty()) {
return true;
}
// 前置任务已完成,可以执行
if (param.getFinishedIds().containsAll(pair.getKey())) {
return true;
}
// 前置任务是异步任务,可以执行
for (Integer preId : pair.getKey()) {
SopInstNode preNode = sop.getInstNodeMap().get(preId);
if (preNode != null && preNode.getNodeAsync() != 1 && !param.getFinishedIds().contains(preId)) {
SopXxlJobUtil.log(log, "【执行Sop节点】【{}:{}】,存在前置任务未完成:【{}:{}】", sop.getInst().getInstId(), sop.getInst().getInstName(),
preNode.getInstId(), preNode.getNodeName());
return false;
}
}
return true;
}
/**
* 节点执行时,申请sop锁
*
* @param node 节点信息
* @param sop 执行器
*/
public void execute(SopStruct sop, SopInstNode node) {
SopNodeType type = SopNodeType.fromBusType(node.getNodeType());
SopNodeExecutor instance = SopNodeExecutorFactory.getInstance(type.getHanderImp(), SopNodeExecutor.class);
// 执行sop节点前,锁定sop
if (SopHelper.lockInst(sop.getInst())) {
try {
// 重新加载sop
sop = reloadAndCreateBatch(sop, node);
// 执行sop节点
ExecuteStruct result = instance.execute(sop, node);
//更新节点批次快照信息
updateBatch(sop, node, result);
// 更新下个节点信息
updateNextNodes(sop, node);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
SopHelper.unlockInst(sop.getInst());
}
} else {
SopXxlJobUtil.log(log, "【执行Sop节点】【{}:{}】,申请实例锁失败,本次不执行!", sop.getInst().getInstId(), sop.getInst().getInstName());
return;
}
}
private void updateNextNodes(SopStruct sop, SopInstNode node) {
SopXxlJobUtil.finishSopNodeJob(sop, node);
}
private void updateBatch(SopStruct sop, SopInstNode node, ExecuteStruct result) {
SopInstNodeBatch nodeBatch = sop.getNodeBatch();
nodeBatch.setSnapshot(JSONUtil.toJsonStr(result));
nodeBatch.setUpdateTime(new Date());
sopService.updateInstNodeBatch(nodeBatch);
}
private SopStruct reloadAndCreateBatch(SopStruct sop, SopInstNode node) {
sop = sopService.loadSop(sop.getInst().getInstId());
// 创建节点执行批次
SopInstNodeBatch curBatch = sopService.createNodeBatch(sop.getInst().getBatchNo(), node.getInstNodeId());
sop.setNodeBatch(curBatch);
return sop;
}
}
......@@ -10,23 +10,24 @@ import lombok.Getter;
/**
* node执行过程管理
*
*
* @author kome
* @date 2023年9月19日 下午5:09:42
*/
@Getter
public class ExecuteStruct implements Serializable, Cloneable {
//
/**
* 各节点的初始化参数表
*/
private Map<String, Object> paramMap = Maps.newHashMap();
/**
* 各节点的输入参数表
*/
private Map<String, Object> executeMap = Maps.newHashMap();
/**
* 各节点的执行结果表
*/
private Map<String, Object> resultMap = Maps.newHashMap();
//
/**
* 各节点的初始化参数表
*/
private Map<String, Object> paramMap = Maps.newHashMap();
/**
* 各节点的输入参数表
*/
private Map<String, Object> executeMap = Maps.newHashMap();
/**
* 各节点的执行结果表
*/
private Map<String, Object> resultMap = Maps.newHashMap();
}
\ No newline at end of file
......@@ -27,7 +27,6 @@ public interface SopNodeExecutor {
/**
* 通过sop模板+sop实例输入参数+sop跑批快照,获得本节点执行的输入参数
*
* @param sopStruct
* @param node
* @return
*/
......@@ -54,7 +53,6 @@ public interface SopNodeExecutor {
*
* @param sop SOP
* @param node SOP节点
* @param executeStruct 输入参数
* @return 输出参数
* @throws Exception
*/
......
package cn.com.glsx.process.modules.sop.execute.executor;
import cn.com.glsx.process.common.constant.Constants;
import org.springframework.stereotype.Service;
import cn.com.glsx.process.modules.entity.SopInstNode;
......@@ -8,14 +9,20 @@ import cn.com.glsx.process.modules.sop.execute.ExecuteStruct;
import cn.com.glsx.process.modules.sop.execute.SopNodeExecutor;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
@Slf4j
@Service
public class EndExecutor implements SopNodeExecutor {
@Override
public ExecuteStruct execute(SopStruct sop, SopInstNode node) throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public ExecuteStruct execute(SopStruct sop, SopInstNode node) throws Exception {
ExecuteStruct executeStruct = prepare(sop, node);
Map<String, Object> resultMap = executeStruct.getResultMap();
resultMap.put(Constants.RES_KEY, Constants.RES_SUCCESS);
return executeStruct;
}
}
\ No newline at end of file
package cn.com.glsx.process.modules.sop.execute.executor;
import cn.com.glsx.process.common.constant.Constants;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
......@@ -12,25 +13,38 @@ import cn.com.glsx.process.modules.sop.execute.SopNodeExecutor;
import cn.com.glsx.process.modules.sop.execute.plugin.java.DynamicEngine;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
@Slf4j
@Service
public class JavaExecutor implements SopNodeExecutor {
@Override
public ExecuteStruct execute(SopStruct sop, SopInstNode node) throws Exception {
// 处理开始节点
String nodeContent = node.getNodeContent();
JSONObject paramMap = JSON.parseObject(nodeContent);
String javacode = paramMap.get("source") + "";
// java参数处理,执行java
DynamicEngine de = DynamicEngine.getInstance();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", 22);
Object instance = de.execute("Controller", javacode, "execute", new Class[] { JSON.class }, new Object[] { jsonObject });
// 处理返回结果
return null;
}
@Override
public ExecuteStruct execute(SopStruct sop, SopInstNode node) throws Exception {
ExecuteStruct executeStruct = prepare(sop, node);
Map<String, Object> resultMap = executeStruct.getResultMap();
try {
// 处理开始节点
String nodeContent = node.getNodeContent();
JSONObject paramMap = JSON.parseObject(nodeContent);
String javacode = paramMap.get("source") + "";
// java参数处理,执行java
DynamicEngine de = DynamicEngine.getInstance();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", 22);
Object instance = de.execute("Controller", javacode, "execute", new Class[]{JSON.class}, new Object[]{jsonObject});
// 处理返回结果
resultMap.put(Constants.RES_DATE_KEY, instance);
resultMap.put(Constants.RES_KEY, Constants.RES_SUCCESS);
} catch (Exception e) {
String message = e.getMessage();
resultMap.put(Constants.RES_FAIL_MESSAGE, message);
resultMap.put(Constants.RES_KEY, Constants.RES_FAIL);
}
return executeStruct;
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@
package cn.com.glsx.process.modules.sop.execute.executor;
import cn.com.glsx.process.common.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -18,6 +19,8 @@ import cn.com.glsx.process.modules.sop.execute.plugin.dbconn.FlowDataAdapterImpl
import cn.com.glsx.process.modules.sop.execute.plugin.dbconn.SopBizAdapter;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
@Slf4j
@Service
public class SqlExecutor implements SopNodeExecutor {
......@@ -25,22 +28,31 @@ public class SqlExecutor implements SopNodeExecutor {
@Autowired
private SopDataSourceService sopDataSourceService;
@Override
public ExecuteStruct execute(SopStruct sop, SopInstNode node) throws Exception {
// 获取配置参数信息
String nodeContent = node.getNodeContent();
JSONObject paramMap = JSON.parseObject(nodeContent);
String sqlcode = paramMap.get("source") + "";
String dataSourceId = paramMap.get("dataSourceId") + "";
// 执行sql插件
SopDataSource byId = sopDataSourceService.findByName(dataSourceId);
SopBizAdapter mcgBizAdapter = new FlowDataAdapterImpl(byId);
int i = mcgBizAdapter.executeUpdate(sqlcode);
// 处理执行结果
return null;
@Override
public ExecuteStruct execute(SopStruct sop, SopInstNode node) throws Exception {
ExecuteStruct executeStruct = prepare(sop, node);
Map<String, Object> resultMap = executeStruct.getResultMap();
try {
// 获取配置参数信息
String nodeContent = node.getNodeContent();
JSONObject paramMap = JSON.parseObject(nodeContent);
String sqlcode = paramMap.get("source") + "";
String dataSourceId = paramMap.get("dataSourceId") + "";
// 执行sql插件
SopDataSource byId = sopDataSourceService.findByName(dataSourceId);
SopBizAdapter mcgBizAdapter = new FlowDataAdapterImpl(byId);
int i = mcgBizAdapter.executeUpdate(sqlcode);
// 处理执行结果
resultMap.put(Constants.RES_DATE_KEY, i);
resultMap.put(Constants.RES_KEY, Constants.RES_SUCCESS);
} catch (Exception e) {
String message = e.getMessage();
resultMap.put(Constants.RES_FAIL_MESSAGE, message);
resultMap.put(Constants.RES_KEY, Constants.RES_FAIL);
}
return executeStruct;
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ package cn.com.glsx.process.modules.sop.execute.executor;
import java.util.List;
import java.util.Map;
import cn.com.glsx.process.common.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -24,24 +25,34 @@ import lombok.extern.slf4j.Slf4j;
@Service
public class SqlQueryExecutor implements SopNodeExecutor {
@Autowired
private SopDataSourceService sopDataSourceService;
@Override
public ExecuteStruct execute(SopStruct sop, SopInstNode node) throws Exception {
// 获取配置参数信息
String nodeContent = node.getNodeContent();
JSONObject paramMap = JSON.parseObject(nodeContent);
String sqlcode = paramMap.get("source") + "";
String dataSourceId = paramMap.get("dataSourceId") + "";
// 执行sql插件
SopDataSource byId = sopDataSourceService.findByName(dataSourceId);
SopBizAdapter mcgBizAdapter = new FlowDataAdapterImpl(byId);
List<Map<String, Object>> maps = mcgBizAdapter.tableQuery(sqlcode);
// 处理执行结果
return null;
}
@Autowired
private SopDataSourceService sopDataSourceService;
@Override
public ExecuteStruct execute(SopStruct sop, SopInstNode node) throws Exception {
ExecuteStruct executeStruct = prepare(sop, node);
Map<String, Object> resultMap = executeStruct.getResultMap();
try {
// 获取配置参数信息
String nodeContent = node.getNodeContent();
JSONObject paramMap = JSON.parseObject(nodeContent);
String sqlcode = paramMap.get("source") + "";
String dataSourceId = paramMap.get("dataSourceId") + "";
// 执行sql插件
SopDataSource byId = sopDataSourceService.findByName(dataSourceId);
SopBizAdapter mcgBizAdapter = new FlowDataAdapterImpl(byId);
List<Map<String, Object>> maps = mcgBizAdapter.tableQuery(sqlcode);
// 处理执行结果
resultMap.put(Constants.RES_DATE_KEY, maps);
resultMap.put(Constants.RES_KEY, Constants.RES_SUCCESS);
} catch (Exception e) {
String message = e.getMessage();
resultMap.put(Constants.RES_FAIL_MESSAGE, message);
resultMap.put(Constants.RES_KEY, Constants.RES_FAIL);
}
return executeStruct;
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ package cn.com.glsx.process.modules.sop.execute.executor;
import javax.annotation.Resource;
import cn.com.glsx.process.common.constant.Constants;
import org.springframework.stereotype.Service;
import cn.com.glsx.process.modules.entity.SopInstNode;
......@@ -14,12 +15,14 @@ import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
/**
* 开始节点:
* 1.从SopInst提取初始化参数;
* 2.记录开始节点快照,更新Sop快照;
* 3.更新触发后置任务;
*
*
* @author kome
* @date 2023年9月19日 下午5:42:59
*/
......@@ -27,29 +30,29 @@ import lombok.extern.slf4j.Slf4j;
@Service
public class StartExecutor implements SopNodeExecutor {
@Resource
private SopService sopService;
@Override
public ExecuteStruct execute(SopStruct sop, SopInstNode node) throws Exception {
@Resource
private SopService sopService;
ExecuteStruct executeStruct = prepare(sop, node);
@Override
public ExecuteStruct execute(SopStruct sop, SopInstNode node) throws Exception {
ExecuteStruct executeStruct = prepare(sop, node);
// 更新节点快照
Map<String, Object> resultMap = executeStruct.getResultMap();
resultMap.put(Constants.RES_KEY, Constants.RES_SUCCESS);
return executeStruct;
}
return executeStruct;
}
public void updateBatch() {
public void updateBatch() {
}
}
public static void main(String[] args) {
String json = "{\"merchantId\":{\"paramName\":\"merchantId\",\"paramDesc\":\"门店id,分配给门店时需要赋值\",\"valueType\":\"String\",\"defaultValue\":\"\",\"required\":true},\"stages\":{\"paramName\":\"stages\",\"paramDesc\":\"旅程阶段,值类型为Json\",\"valueType\":\"Json\",\"defaultValue\":{\"stage1\":{\"value\":\"潜客\",\"editable\":false,\"conditions\":[\"链接(#膨胀金活动名称#) 反馈\",\"链接(#膨胀金活动名称#) 反馈\"]},\"stage2\":{\"value\":\"高意向\",\"editable\":true,\"conditions\":[\"链接(#膨胀金活动名称#) 反馈\",\"链接(#膨胀金活动名称#) 反馈\"]},\"stage3\":{\"value\":\"下单\",\"editable\":false,\"conditions\":[\"链接(#膨胀金活动名称#) 反馈\",\"链接(#膨胀金活动名称#) 反馈\"]},\"stage4\":{\"value\":\"战败\",\"editable\":false,\"conditions\":[\"链接(#膨胀金活动名称#) 反馈\",\"链接(#膨胀金活动名称#) 反馈\"]}},\"required\":true},\"users\":{\"paramName\":\"users\",\"paramDesc\":\"通过膨胀金活动导入的用户信息\",\"valueType\":\"Json\",\"defaultValue\":{\"u0001\":{\"userId\":\"u0001\",\"userName\":\"张三\",\"followId\":\"跟进人id1\",\"textContent\":\"文案内容:尊敬的${userName},优惠券领取url.cn/dE32C1Ab7\"},\"u0002\":{\"userId\":\"u0002\",\"userName\":\"李四\",\"followId\":\"跟进人id2\",\"textContent\":\"文案内容:尊敬的${userName},优惠券领取url.cn/dE32C1Ab7\"}},\"required\":true}}";
public static void main(String[] args) {
String json = "{\"merchantId\":{\"paramName\":\"merchantId\",\"paramDesc\":\"门店id,分配给门店时需要赋值\",\"valueType\":\"String\",\"defaultValue\":\"\",\"required\":true},\"stages\":{\"paramName\":\"stages\",\"paramDesc\":\"旅程阶段,值类型为Json\",\"valueType\":\"Json\",\"defaultValue\":{\"stage1\":{\"value\":\"潜客\",\"editable\":false,\"conditions\":[\"链接(#膨胀金活动名称#) 反馈\",\"链接(#膨胀金活动名称#) 反馈\"]},\"stage2\":{\"value\":\"高意向\",\"editable\":true,\"conditions\":[\"链接(#膨胀金活动名称#) 反馈\",\"链接(#膨胀金活动名称#) 反馈\"]},\"stage3\":{\"value\":\"下单\",\"editable\":false,\"conditions\":[\"链接(#膨胀金活动名称#) 反馈\",\"链接(#膨胀金活动名称#) 反馈\"]},\"stage4\":{\"value\":\"战败\",\"editable\":false,\"conditions\":[\"链接(#膨胀金活动名称#) 反馈\",\"链接(#膨胀金活动名称#) 反馈\"]}},\"required\":true},\"users\":{\"paramName\":\"users\",\"paramDesc\":\"通过膨胀金活动导入的用户信息\",\"valueType\":\"Json\",\"defaultValue\":{\"u0001\":{\"userId\":\"u0001\",\"userName\":\"张三\",\"followId\":\"跟进人id1\",\"textContent\":\"文案内容:尊敬的${userName},优惠券领取url.cn/dE32C1Ab7\"},\"u0002\":{\"userId\":\"u0002\",\"userName\":\"李四\",\"followId\":\"跟进人id2\",\"textContent\":\"文案内容:尊敬的${userName},优惠券领取url.cn/dE32C1Ab7\"}},\"required\":true}}";
JSONObject paramObj = JSONUtil.parseObj(json);
System.out.println(paramObj.get("merchantId"));
JSONObject paramObj = JSONUtil.parseObj(json);
System.out.println(paramObj.get("merchantId"));
}
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!