1
0
Эх сурвалжийг харах

enhancement #IBXBVS 给每个chain的运行加入一个runtimeId属性

everywhere.z 1 сар өмнө
parent
commit
f4fb9954de

+ 5 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java

@@ -16,6 +16,7 @@ import com.yomahub.liteflow.core.proxy.LiteFlowProxyUtil;
 import com.yomahub.liteflow.enums.CmpStepTypeEnum;
 import com.yomahub.liteflow.enums.NodeTypeEnum;
 import com.yomahub.liteflow.exception.ObjectConvertException;
+import com.yomahub.liteflow.flow.FlowBus;
 import com.yomahub.liteflow.flow.LiteflowResponse;
 import com.yomahub.liteflow.flow.element.Node;
 import com.yomahub.liteflow.flow.entity.CmpStep;
@@ -543,6 +544,10 @@ public abstract class NodeComponent{
 		return null;
 	}
 
+	public long getCurrChainRuntimeId(){
+		return FlowBus.getChain(getCurrChainId()).getRuntimeId();
+	}
+
 	protected String getMetaValueKey(){
 		Class<?> originalClass = LiteFlowProxyUtil.getUserClass(this.getClass());
 		return originalClass.getName();

+ 13 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Chain.java

@@ -10,6 +10,7 @@ package com.yomahub.liteflow.flow.element;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.BooleanUtil;
+import com.alibaba.ttl.TransmittableThreadLocal;
 import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
 import com.yomahub.liteflow.common.ChainConstant;
 import com.yomahub.liteflow.enums.ExecuteableTypeEnum;
@@ -48,6 +49,8 @@ public class Chain implements Executable{
 
     private String threadPoolExecutorClass;
 
+	private final TransmittableThreadLocal<Long> runtimeIdTL = new TransmittableThreadLocal<>();
+
 	public Chain(String chainName) {
 		this.chainId = chainName;
 	}
@@ -97,6 +100,10 @@ public class Chain implements Executable{
 	// 执行chain的主方法
 	@Override
 	public void execute(Integer slotIndex) throws Exception {
+		//生成runtimeId
+		this.runtimeIdTL.set(System.nanoTime());
+
+		//如果EL还未编译,则进行编译
 		if (BooleanUtil.isFalse(isCompiled)) {
 			LiteFlowChainELBuilder.buildUnCompileChain(this);
 		}
@@ -142,6 +149,8 @@ public class Chain implements Executable{
 				slot.setException(e);
 			}
 			throw e;
+		}finally {
+			runtimeIdTL.remove();
 		}
 	}
 
@@ -235,4 +244,8 @@ public class Chain implements Executable{
     public void setThreadPoolExecutorClass(String threadPoolExecutorClass) {
         this.threadPoolExecutorClass = threadPoolExecutorClass;
     }
+
+	public Long getRuntimeId(){
+		return runtimeIdTL.get();
+	}
 }

+ 43 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/ChainRuntimeIdSpringbootTest.java

@@ -0,0 +1,43 @@
+package com.yomahub.liteflow.test.chainRuntimeId;
+
+import com.yomahub.liteflow.core.FlowExecutor;
+import com.yomahub.liteflow.flow.LiteflowResponse;
+import com.yomahub.liteflow.slot.DefaultContext;
+import com.yomahub.liteflow.test.BaseTest;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.test.context.TestPropertySource;
+
+import javax.annotation.Resource;
+
+/**
+ * springboot环境EL常规的例子测试
+ *
+ * @author Bryan.Zhang
+ */
+@TestPropertySource(value = "classpath:/chainRuntimeId/application.properties")
+@SpringBootTest(classes = ChainRuntimeIdSpringbootTest.class)
+@EnableAutoConfiguration
+@ComponentScan({ "com.yomahub.liteflow.test.chainRuntimeId.cmp" })
+public class ChainRuntimeIdSpringbootTest extends BaseTest {
+
+	@Resource
+	private FlowExecutor flowExecutor;
+
+	// 最简单的情况
+	@Test
+	public void testRuntimeId() throws Exception {
+		LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
+		Assertions.assertTrue(response.isSuccess());
+		DefaultContext context = response.getFirstContextBean();
+		long runtimeIdMain = context.getData("a");
+		long runtimeIdSub = context.getData("b");
+		Assertions.assertTrue(runtimeIdSub > runtimeIdMain);
+	}
+
+
+
+}

+ 23 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/cmp/ACmp.java

@@ -0,0 +1,23 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @author Bryan.Zhang
+ * @email weenyc31@163.com
+ * @Date 2020/4/1
+ */
+package com.yomahub.liteflow.test.chainRuntimeId.cmp;
+
+import com.yomahub.liteflow.core.NodeComponent;
+import com.yomahub.liteflow.slot.DefaultContext;
+import org.springframework.stereotype.Component;
+
+@Component("a")
+public class ACmp extends NodeComponent {
+
+	@Override
+	public void process() {
+		System.out.println("ACmp executed!");
+		DefaultContext context = this.getFirstContextBean();
+		context.setData("a", this.getCurrChainRuntimeId());
+	}
+}

+ 24 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/cmp/BCmp.java

@@ -0,0 +1,24 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @author Bryan.Zhang
+ * @email weenyc31@163.com
+ * @Date 2020/4/1
+ */
+package com.yomahub.liteflow.test.chainRuntimeId.cmp;
+
+import com.yomahub.liteflow.core.NodeComponent;
+import com.yomahub.liteflow.slot.DefaultContext;
+import org.springframework.stereotype.Component;
+
+@Component("b")
+public class BCmp extends NodeComponent {
+
+	@Override
+	public void process() {
+		System.out.println("BCmp executed!");
+		DefaultContext context = this.getFirstContextBean();
+		context.setData("b", this.getCurrChainRuntimeId());
+	}
+
+}

+ 26 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/cmp/CCmp.java

@@ -0,0 +1,26 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @author Bryan.Zhang
+ * @email weenyc31@163.com
+ * @Date 2020/4/1
+ */
+package com.yomahub.liteflow.test.chainRuntimeId.cmp;
+
+import com.yomahub.liteflow.core.NodeComponent;
+import org.springframework.stereotype.Component;
+
+@Component("c")
+public class CCmp extends NodeComponent {
+
+	@Override
+	public void process() {
+		System.out.println("CCmp executed!");
+	}
+
+	@Override
+	public boolean isAccess() {
+		System.out.println("hello");
+		return true;
+	}
+}

+ 1 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/chainRuntimeId/application.properties

@@ -0,0 +1 @@
+liteflow.rule-source=chainRuntimeId/flow.xml

+ 11 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/chainRuntimeId/flow.xml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE flow PUBLIC  "liteflow" "liteflow.dtd">
+<flow>
+    <chain name="chain1">
+        THEN(a ,sub);
+    </chain>
+
+    <chain name="sub">
+        THEN(b, c);
+    </chain>
+</flow>