From d38ccdfdc0e7df7a0ab5ee07342bdd1a4fb8337d Mon Sep 17 00:00:00 2001 From: bryan31 Date: Tue, 27 Jul 2021 00:30:21 +0800 Subject: [PATCH] =?UTF-8?q?enhancement=20#I40DWO=20=E6=B5=81=E7=A8=8B?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E4=B8=AD=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E6=8F=8F=E8=BF=B0=EF=BC=8C=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=AD=A5=E9=AA=A4=E4=B8=AD=E5=B8=A6=E5=85=A5=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../annotation/LiteflowComponent.java | 7 +- .../yomahub/liteflow/core/NodeComponent.java | 3 +- .../yomahub/liteflow/entity/data/AbsSlot.java | 8 +- .../yomahub/liteflow/entity/data/CmpStep.java | 102 +++++++++++------- .../LiteflowComponentSpringbootTest.java | 39 +++++++ .../test/liteflowcomponent/cmp/ACmp.java | 20 ++++ .../test/liteflowcomponent/cmp/BCmp.java | 21 ++++ .../test/liteflowcomponent/cmp/CCmp.java | 21 ++++ .../test/liteflowcomponent/cmp/DCmp.java | 21 ++++ .../liteflowComponent/application.properties | 1 + .../test/resources/liteflowComponent/flow.xml | 10 ++ 11 files changed, 203 insertions(+), 50 deletions(-) create mode 100644 liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/LiteflowComponentSpringbootTest.java create mode 100644 liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/ACmp.java create mode 100644 liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/BCmp.java create mode 100644 liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/CCmp.java create mode 100644 liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/DCmp.java create mode 100644 liteflow-spring-boot-starter/src/test/resources/liteflowComponent/application.properties create mode 100644 liteflow-spring-boot-starter/src/test/resources/liteflowComponent/flow.xml diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/annotation/LiteflowComponent.java b/liteflow-core/src/main/java/com/yomahub/liteflow/annotation/LiteflowComponent.java index 068f5c71e..cff7d2a0b 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/annotation/LiteflowComponent.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/annotation/LiteflowComponent.java @@ -1,5 +1,6 @@ package com.yomahub.liteflow.annotation; +import cn.hutool.core.annotation.Alias; import org.springframework.core.annotation.AliasFor; import org.springframework.stereotype.Component; @@ -17,11 +18,11 @@ import java.lang.annotation.*; @Component public @interface LiteflowComponent { - @AliasFor(annotation = Component.class) + @AliasFor(annotation = Component.class, attribute = "value") String value() default ""; - @AliasFor("value") - String id(); + @AliasFor(annotation = Component.class, attribute = "value") + String id() default ""; String name() default ""; } 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 6a5b2ec33..ba6dabea6 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 @@ -53,7 +53,7 @@ public abstract class NodeComponent { public void execute() throws Exception{ Slot slot = this.getSlot(); LOG.info("[{}]:[O]start component[{}] execution",slot.getRequestId(),this.getClass().getSimpleName()); - slot.addStep(new CmpStep(nodeId, CmpStepType.SINGLE)); + slot.addStep(new CmpStep(nodeId, name, CmpStepType.SINGLE)); StopWatch stopWatch = new StopWatch(); stopWatch.start(); @@ -71,7 +71,6 @@ public abstract class NodeComponent { stopWatch.stop(); -// slot.addStep(new CmpStep(nodeId, CmpStepType.END)); final long timeSpent = stopWatch.getTotalTimeMillis(); // 性能统计 if (ObjectUtil.isNotNull(monitorBus)) { diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/AbsSlot.java b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/AbsSlot.java index 824bdec99..2ac1e23f8 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/AbsSlot.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/AbsSlot.java @@ -9,11 +9,11 @@ package com.yomahub.liteflow.entity.data; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - -import java.util.ArrayDeque; import java.util.Deque; import java.util.Iterator; +import java.util.Queue; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; /** * Slot的抽象类实现 @@ -42,7 +42,7 @@ public abstract class AbsSlot implements Slot { private static final String EXCEPTION = "exception"; - private Deque executeSteps = new ArrayDeque(); + private final Queue executeSteps = new ConcurrentLinkedQueue<>(); protected ConcurrentHashMap dataMap = new ConcurrentHashMap(); @@ -138,7 +138,7 @@ public abstract class AbsSlot implements Slot { return (String)dataMap.get(REQUEST_ID); } - public Deque getExecuteSteps() { + public Queue getExecuteSteps() { return executeSteps; } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/CmpStep.java b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/CmpStep.java index 3278ceabb..b8285f38f 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/CmpStep.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/entity/data/CmpStep.java @@ -1,6 +1,7 @@ /** *

Title: liteflow

*

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

+ * * @author Bryan.Zhang * @email weenyc31@163.com * @Date 2020/4/1 @@ -10,62 +11,81 @@ package com.yomahub.liteflow.entity.data; import java.text.MessageFormat; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; /** * 组件步骤对象 * @author Bryan.Zhang */ public class CmpStep { - private String nodeId; - private CmpStepType stepType; + private String nodeId; - public CmpStep(String nodeId, CmpStepType stepType) { - this.nodeId = nodeId; - this.stepType = stepType; - } + private String nodeName; - public String getNodeId() { - return nodeId; - } + private CmpStepType stepType; - public void setNodeId(String nodeId) { - this.nodeId = nodeId; - } + public CmpStep(String nodeId, String nodeName, CmpStepType stepType) { + this.nodeId = nodeId; + this.nodeName = nodeName; + this.stepType = stepType; + } - public CmpStepType getStepType() { - return stepType; - } + public String getNodeId() { + return nodeId; + } - public void setStepType(CmpStepType stepType) { - this.stepType = stepType; - } + public void setNodeId(String nodeId) { + this.nodeId = nodeId; + } - @Override - public String toString() { - if(stepType.equals(CmpStepType.SINGLE)) { - return MessageFormat.format("{0}", nodeId); - }else { - return MessageFormat.format("{0}({1})", nodeId,stepType); - } + public CmpStepType getStepType() { + return stepType; + } + public void setStepType(CmpStepType stepType) { + this.stepType = stepType; + } - } + public String getNodeName() { + return nodeName; + } - @Override - public boolean equals(Object obj) { - if (ObjectUtil.isNull(obj)) { - return false; - }else { - if(getClass() != obj.getClass()) { - return false; - }else { - if(((CmpStep)obj).getNodeId().equals(this.getNodeId())) { - return true; - }else { - return false; - } + public void setNodeName(String nodeName) { + this.nodeName = nodeName; + } + + @Override + public String toString() { + if (stepType.equals(CmpStepType.SINGLE)) { + if (StrUtil.isBlank(nodeName)){ + return StrUtil.format("{}", nodeId); + }else{ + return StrUtil.format("{}[{}]", nodeId, nodeName); } - } - } + } else { + if (StrUtil.isBlank(nodeName)){ + return StrUtil.format("{}({})", nodeId, stepType); + }else{ + return StrUtil.format("{}[{}]({})", nodeId, nodeName, stepType); + } + } + } + + @Override + public boolean equals(Object obj) { + if (ObjectUtil.isNull(obj)) { + return false; + } else { + if (getClass() != obj.getClass()) { + return false; + } else { + if (((CmpStep) obj).getNodeId().equals(this.getNodeId())) { + return true; + } else { + return false; + } + } + } + } } diff --git a/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/LiteflowComponentSpringbootTest.java b/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/LiteflowComponentSpringbootTest.java new file mode 100644 index 000000000..caadeaf8c --- /dev/null +++ b/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/LiteflowComponentSpringbootTest.java @@ -0,0 +1,39 @@ +package com.yomahub.liteflow.test.liteflowcomponent; + +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.entity.data.DefaultSlot; +import com.yomahub.liteflow.entity.data.LiteflowResponse; +import com.yomahub.liteflow.test.BaseTest; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +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 org.springframework.test.context.junit4.SpringRunner; + + +/** + * 测试springboot下的enable参数 + * @author Bryan.Zhang + * @since 2.5.10 + */ +@RunWith(SpringRunner.class) +@TestPropertySource(value = "classpath:/liteflowComponent/application.properties") +@SpringBootTest(classes = LiteflowComponentSpringbootTest.class) +@EnableAutoConfiguration +@ComponentScan({"com.yomahub.liteflow.test.liteflowComponent.cmp"}) +public class LiteflowComponentSpringbootTest extends BaseTest { + + @Autowired + private FlowExecutor flowExecutor; + + @Test + public void testConfig() { + LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("a[A组件]==>b[B组件]==>c[C组件]==>b[B组件]==>a[A组件]==>d", response.getSlot().printStep()); + } +} diff --git a/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/ACmp.java b/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/ACmp.java new file mode 100644 index 000000000..96ec64d8f --- /dev/null +++ b/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/ACmp.java @@ -0,0 +1,20 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.liteflowcomponent.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent(id = "a", name = "A组件") +public class ACmp extends NodeComponent { + + @Override + public void process() { + System.out.println("ACmp executed!"); + } +} diff --git a/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/BCmp.java b/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/BCmp.java new file mode 100644 index 000000000..9d1c0b233 --- /dev/null +++ b/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/BCmp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.liteflowcomponent.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent(id = "b", name = "B组件") +public class BCmp extends NodeComponent { + + @Override + public void process() { + System.out.println("BCmp executed!"); + } + +} diff --git a/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/CCmp.java b/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/CCmp.java new file mode 100644 index 000000000..f01e577d6 --- /dev/null +++ b/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/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.liteflowcomponent.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent(id = "c", name = "C组件") +public class CCmp extends NodeComponent { + + @Override + public void process() { + System.out.println("CCmp executed!"); + } + +} diff --git a/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/DCmp.java b/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/DCmp.java new file mode 100644 index 000000000..06d4d268f --- /dev/null +++ b/liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/liteflowcomponent/cmp/DCmp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.liteflowcomponent.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent("d") +public class DCmp extends NodeComponent { + + @Override + public void process() { + System.out.println("DCmp executed!"); + } + +} diff --git a/liteflow-spring-boot-starter/src/test/resources/liteflowComponent/application.properties b/liteflow-spring-boot-starter/src/test/resources/liteflowComponent/application.properties new file mode 100644 index 000000000..62a916d4a --- /dev/null +++ b/liteflow-spring-boot-starter/src/test/resources/liteflowComponent/application.properties @@ -0,0 +1 @@ +liteflow.rule-source=liteflowComponent/flow.xml \ No newline at end of file diff --git a/liteflow-spring-boot-starter/src/test/resources/liteflowComponent/flow.xml b/liteflow-spring-boot-starter/src/test/resources/liteflowComponent/flow.xml new file mode 100644 index 000000000..62def0c37 --- /dev/null +++ b/liteflow-spring-boot-starter/src/test/resources/liteflowComponent/flow.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file