Explorar o código

生成node的实例id

jay li hai 7 meses
pai
achega
44afec1083

+ 27 - 2
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<? extends NodeExecutor> nodeExecutorClass) {
 		this.nodeExecutorClass = nodeExecutorClass;
 	}
+	public String getInstanceId() {
+		return instanceId;
+	}
 
+	public void setInstanceId(String instanceId) {
+		this.instanceId = instanceId;
+	}
 	public String getTag() {
 		return this.getRefNode().getTag();
 	}

+ 4 - 0
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);
 	}

+ 22 - 1
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)) {

+ 16 - 0
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<CmpStep> 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);
 	}

+ 27 - 0
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<String> strings = extractValues(executeStepStrWithInstanceId);
+		System.out.println(executeStepStrWithInstanceId);
+
+		Assertions.assertEquals(strings.size(), 4);
+	}
+
+	public static Set<String> extractValues(String input) {
+		Set<String> values = new HashSet<>();
+		Pattern pattern = Pattern.compile("\\[(.*?)]");
+		Matcher matcher = pattern.matcher(input);
+		while (matcher.find()) {
+			values.add(matcher.group(1));
+		}
+		return values;
+	}
+
 }

+ 4 - 0
liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/resources/base/flow.el.xml

@@ -3,4 +3,8 @@
     <chain name="chain1">
         THEN(a,b,c,d);
     </chain>
+
+    <chain name="chain2">
+        THEN(a,a,a,a);
+    </chain>
 </flow>