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 e948909e0..8e168c5a8 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 @@ -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(); diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Chain.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Chain.java index f0c2e49b1..82472fac7 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Chain.java +++ b/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 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(); + } } diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/ChainRuntimeIdSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/ChainRuntimeIdSpringbootTest.java new file mode 100644 index 000000000..4561e1ab3 --- /dev/null +++ b/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); + } + + + +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/cmp/ACmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/cmp/ACmp.java new file mode 100644 index 000000000..fcf99dd58 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/cmp/ACmp.java @@ -0,0 +1,23 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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()); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/cmp/BCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/cmp/BCmp.java new file mode 100644 index 000000000..8b15fd889 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/cmp/BCmp.java @@ -0,0 +1,24 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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()); + } + +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/cmp/CCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/cmp/CCmp.java new file mode 100644 index 000000000..8627cad83 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/chainRuntimeId/cmp/CCmp.java @@ -0,0 +1,26 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @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; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/chainRuntimeId/application.properties b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/chainRuntimeId/application.properties new file mode 100644 index 000000000..0fb11a537 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/chainRuntimeId/application.properties @@ -0,0 +1 @@ +liteflow.rule-source=chainRuntimeId/flow.xml \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/chainRuntimeId/flow.xml b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/chainRuntimeId/flow.xml new file mode 100644 index 000000000..a6ce44159 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/chainRuntimeId/flow.xml @@ -0,0 +1,11 @@ + + + + + THEN(a ,sub); + + + + THEN(b, c); + + \ No newline at end of file