diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java b/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java index c5909b0a4..6a4059ae3 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java @@ -26,6 +26,8 @@ import com.yomahub.liteflow.slot.DataBus; import com.yomahub.liteflow.slot.Slot; import com.yomahub.liteflow.spi.holder.CmpAroundAspectHolder; import com.yomahub.liteflow.util.JsonUtil; +import cn.hutool.crypto.*; +import cn.hutool.crypto.digest.*; import java.lang.reflect.Method; import java.util.Date; @@ -48,6 +50,8 @@ public abstract class NodeComponent{ private String name; + private String instanceId; + private NodeTypeEnum type; // 这是自己的实例,取代this @@ -85,7 +89,7 @@ public abstract class NodeComponent{ Slot slot = this.getSlot(); // 在元数据里加入step信息 - CmpStep cmpStep = new CmpStep(nodeId, name, CmpStepTypeEnum.SINGLE); + CmpStep cmpStep = new CmpStep(nodeId, name, CmpStepTypeEnum.SINGLE, generateInstanceId()); cmpStep.setTag(this.getTag()); cmpStep.setInstance(this); cmpStep.setRefNode(this.getRefNode()); @@ -153,7 +157,7 @@ public abstract class NodeComponent{ return; } - CmpStep cmpStep = new CmpStep(nodeId, name, CmpStepTypeEnum.SINGLE); + CmpStep cmpStep = new CmpStep(nodeId, name, CmpStepTypeEnum.SINGLE, generateInstanceId()); cmpStep.setTag(this.getTag()); cmpStep.setInstance(this); cmpStep.setRefNode(this.getRefNode()); @@ -237,6 +241,21 @@ public abstract class NodeComponent{ this.getRefNode().setIsContinueOnErrorResult(isContinueOnError); } + public String generateInstanceId() { + Digester sha256 = SecureUtil.sha256(); + byte[] hashBytes = sha256.digest(this.getNodeId() + System.nanoTime()); + + StringBuilder sb = new StringBuilder(); + for (byte b : hashBytes) { + if (sb.length() >= 6) { + break; + } + sb.append(String.format("%02x", b)); + } + + return sb.toString(); + } + public Integer getSlotIndex() { return this.getRefNode().getSlotIndex(); } @@ -320,7 +339,13 @@ public abstract class NodeComponent{ public void setNodeExecutorClass(Class nodeExecutorClass) { this.nodeExecutorClass = nodeExecutorClass; } + public String getInstanceId() { + return instanceId; + } + public void setInstanceId(String instanceId) { + this.instanceId = instanceId; + } public String getTag() { return this.getRefNode().getTag(); } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/LiteflowResponse.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/LiteflowResponse.java index 34f960a8e..4c24df652 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/LiteflowResponse.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/LiteflowResponse.java @@ -156,6 +156,10 @@ public class LiteflowResponse { return getExecuteStepStrWithoutTime(); } + public String getExecuteStepStrWithInstanceId() { + return this.getSlot().getExecuteStepStrWithInstanceId(); + } + public String getExecuteStepStrWithTime() { return this.getSlot().getExecuteStepStr(true); } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/entity/CmpStep.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/entity/CmpStep.java index 4bade848e..39ffdf6d6 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/entity/CmpStep.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/entity/CmpStep.java @@ -23,6 +23,8 @@ import java.util.Date; */ public class CmpStep { + private String instanceId; + private String nodeId; private String nodeName; @@ -54,10 +56,19 @@ public class CmpStep { private Node refNode; - public CmpStep(String nodeId, String nodeName, CmpStepTypeEnum stepType) { + public CmpStep(String nodeId, String nodeName, CmpStepTypeEnum stepType, String instanceId) { this.nodeId = nodeId; this.nodeName = nodeName; this.stepType = stepType; + this.instanceId = instanceId; + } + + public String getInstanceId() { + return instanceId; + } + + public void setInstanceId(String instanceId) { + this.instanceId = instanceId; } public String getNodeId() { @@ -147,6 +158,16 @@ public class CmpStep { } } + public String buildStringWithInstanceId() { + if (stepType.equals(CmpStepTypeEnum.SINGLE)) { + return StrUtil.format("{}[{}]", nodeId, instanceId); + } + else { + // 目前没有其他的类型 + return null; + } + } + public String buildStringWithTime() { if (stepType.equals(CmpStepTypeEnum.SINGLE)) { if (StrUtil.isBlank(nodeName)) { diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/slot/Slot.java b/liteflow-core/src/main/java/com/yomahub/liteflow/slot/Slot.java index 1a5886e66..5fe5e430a 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/slot/Slot.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/slot/Slot.java @@ -357,6 +357,22 @@ public class Slot { return this.executeStepsStr; } + + public String getExecuteStepStrWithInstanceId() { + StringBuilder str = new StringBuilder(); + CmpStep cmpStep; + for (Iterator it = executeSteps.iterator(); it.hasNext();) { + cmpStep = it.next(); + str.append(cmpStep.buildStringWithInstanceId()); + + if (it.hasNext()) { + str.append("==>"); + } + } + this.executeStepsStr = str.toString(); + return this.executeStepsStr; + } + public String getExecuteStepStr() { return getExecuteStepStr(false); } diff --git a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/base/BaseCommonELSpringTest.java b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/base/BaseCommonELSpringTest.java index b3e3a7e2a..edd94ccf4 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/base/BaseCommonELSpringTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/base/BaseCommonELSpringTest.java @@ -10,6 +10,10 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; import javax.annotation.Resource; +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; @ExtendWith(SpringExtension.class) @ContextConfiguration("classpath:/base/application.xml") @@ -25,4 +29,27 @@ public class BaseCommonELSpringTest extends BaseTest { Assertions.assertEquals("a==>b==>c==>d", response.getExecuteStepStr()); } + @Test + public void testBaseCommon2() { + LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg"); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("a==>a==>a==>a", response.getExecuteStepStr()); + + String executeStepStrWithInstanceId = response.getExecuteStepStrWithInstanceId(); + Set strings = extractValues(executeStepStrWithInstanceId); + System.out.println(executeStepStrWithInstanceId); + + Assertions.assertEquals(strings.size(), 4); + } + + public static Set extractValues(String input) { + Set values = new HashSet<>(); + Pattern pattern = Pattern.compile("\\[(.*?)]"); + Matcher matcher = pattern.matcher(input); + while (matcher.find()) { + values.add(matcher.group(1)); + } + return values; + } + } diff --git a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/resources/base/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/resources/base/flow.el.xml index f5d546c5b..9a3a13326 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/resources/base/flow.el.xml +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/resources/base/flow.el.xml @@ -3,4 +3,8 @@ THEN(a,b,c,d); + + + THEN(a,a,a,a); + \ No newline at end of file