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 45063b317..0e8bd67f4 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 @@ -137,6 +137,10 @@ public abstract class NodeComponent{ final long timeSpent = stopWatch.getTotalTimeMillis(); LOG.info("component[{}] finished in {} milliseconds", this.getDisplayName(), timeSpent); + // 步骤自定义数据设置 + cmpStep.setStepData(this.getRefNode().getStepData()); + + // 结束时间设置 cmpStep.setEndTime(new Date()); // 往CmpStep中放入时间消耗信息 @@ -487,6 +491,10 @@ public abstract class NodeComponent{ return this.getRefNode().getPreNLoopObject(n); } + public void setStepData(Object stepData) { + this.getRefNode().setStepData(stepData); + } + @Deprecated public void invoke(String chainId, Object param) throws Exception { FlowExecutorHolder.loadInstance().invoke(chainId, param, this.getSlotIndex()); diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Node.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Node.java index 5b95f098d..ad3023f41 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Node.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/Node.java @@ -95,6 +95,9 @@ public class Node implements Executable, Cloneable, Rollbackable{ // isContinueOnError 结果 private TransmittableThreadLocal isContinueOnErrorResult = new TransmittableThreadLocal<>(); + // step自定义数据 + private ThreadLocal stepDataTL = new ThreadLocal<>(); + public Node() { } @@ -243,6 +246,7 @@ public class Node implements Executable, Cloneable, Rollbackable{ removeLoopIndex(); removeAccessResult(); removeIsContinueOnErrorResult(); + removeStepData(); } } @@ -539,6 +543,19 @@ public class Node implements Executable, Cloneable, Rollbackable{ isCloned = cloned; } + public Object getStepData(){ + return this.stepDataTL.get(); + } + + + public void setStepData(Object stepData) { + this.stepDataTL.set(stepData); + } + + public void removeStepData() { + this.stepDataTL.remove(); + } + @Override public Node clone() throws CloneNotSupportedException { Node node = (Node)super.clone(); @@ -548,6 +565,7 @@ public class Node implements Executable, Cloneable, Rollbackable{ node.slotIndexTL = new TransmittableThreadLocal<>(); node.isEndTL = new TransmittableThreadLocal<>(); node.isContinueOnErrorResult = new TransmittableThreadLocal<>(); + node.stepDataTL = new ThreadLocal<>(); node.lock4LoopIndex = new ReentrantLock(); node.lock4LoopObj = new ReentrantLock(); node.bindDataMap = new HashMap<>(); 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 a8539da65..30a645690 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 @@ -56,6 +56,9 @@ public class CmpStep { // 当前执行的node private Node refNode; + // 自定义步骤数据 + private Object stepData; + public CmpStep(String nodeId, String nodeName, CmpStepTypeEnum stepType, String instanceId) { this.nodeId = nodeId; @@ -262,4 +265,12 @@ public class CmpStep { public void setEndTime(Date endTime) { this.endTime = endTime; } + + public Object getStepData() { + return stepData; + } + + public void setStepData(Object stepData) { + this.stepData = stepData; + } } diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/StepDataSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/StepDataSpringbootTest.java new file mode 100644 index 000000000..378abe7db --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/StepDataSpringbootTest.java @@ -0,0 +1,53 @@ +package com.yomahub.liteflow.test.stepData; + +import cn.hutool.core.util.StrUtil; +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.flow.LiteflowResponse; +import com.yomahub.liteflow.flow.entity.CmpStep; +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; +import java.util.function.Consumer; + +/** + * springboot环境EL常规的例子测试 + * + * @author Bryan.Zhang + */ +@TestPropertySource(value = "classpath:/stepData/application.properties") +@SpringBootTest(classes = StepDataSpringbootTest.class) +@EnableAutoConfiguration +@ComponentScan({ "com.yomahub.liteflow.test.stepData.cmp" }) +public class StepDataSpringbootTest extends BaseTest { + + @Resource + private FlowExecutor flowExecutor; + + // 测试step data,每个step都不一样的数据 + @Test + public void testStepData1() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); + Assertions.assertTrue(response.isSuccess()); + response.getExecuteStepQueue().forEach( + cmpStep -> Assertions.assertEquals(StrUtil.format("step_{}", cmpStep.getNodeId()), cmpStep.getStepData()) + ); + } + + // 测试step data,即便是2个相同的节点,step data也可以不一样 + @Test + public void testStepData2() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg"); + final Object[] data = {null}; + response.getExecuteStepQueue().forEach(cmpStep -> { + Assertions.assertNotEquals(data[0], cmpStep.getStepData()); + data[0] = cmpStep.getStepData(); + }); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/cmp/ACmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/cmp/ACmp.java new file mode 100644 index 000000000..85637be73 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/cmp/ACmp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.stepData.cmp; + +import com.yomahub.liteflow.core.NodeComponent; +import org.springframework.stereotype.Component; + +@Component("a") +public class ACmp extends NodeComponent { + + @Override + public void process() { + this.setStepData("step_a"); + System.out.println("ACmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/cmp/BCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/cmp/BCmp.java new file mode 100644 index 000000000..3371de8dd --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/cmp/BCmp.java @@ -0,0 +1,22 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.stepData.cmp; + +import com.yomahub.liteflow.core.NodeComponent; +import org.springframework.stereotype.Component; + +@Component("b") +public class BCmp extends NodeComponent { + + @Override + public void process() { + this.setStepData("step_b"); + System.out.println("BCmp executed!"); + } + +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/cmp/CCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/cmp/CCmp.java new file mode 100644 index 000000000..9ad38a4f8 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/cmp/CCmp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.stepData.cmp; + +import com.yomahub.liteflow.core.NodeComponent; +import org.springframework.stereotype.Component; + +@Component("c") +public class CCmp extends NodeComponent { + + @Override + public void process() { + this.setStepData("step_c"); + System.out.println("CCmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/cmp/DCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/cmp/DCmp.java new file mode 100644 index 000000000..48a57b7ac --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/stepData/cmp/DCmp.java @@ -0,0 +1,20 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.stepData.cmp; + +import com.yomahub.liteflow.core.NodeComponent; +import org.springframework.stereotype.Component; + +@Component("d") +public class DCmp extends NodeComponent { + + @Override + public void process() { + this.setStepData(System.nanoTime()); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/stepData/application.properties b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/stepData/application.properties new file mode 100644 index 000000000..12c922651 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/stepData/application.properties @@ -0,0 +1 @@ +liteflow.rule-source=stepData/flow.el.xml \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/stepData/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/stepData/flow.el.xml new file mode 100644 index 000000000..a422ff149 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/stepData/flow.el.xml @@ -0,0 +1,11 @@ + + + + + THEN(a,b,c); + + + + THEN(d,d); + + \ No newline at end of file