From 225d3f12b759711c599565786b83d367a2011cbb Mon Sep 17 00:00:00 2001 From: bryan31 Date: Tue, 30 Mar 2021 02:21:17 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=B7=A5=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yomahub/liteflow/core/FlowExecutor.java | 6 +- liteflow-test/pom.xml | 67 +++++++++++++++++++ .../liteflow/test/aop/LiteflowAOPTest.java | 25 +++++++ .../yomahub/liteflow/test/aop/TestRunner.java | 16 +++++ .../yomahub/liteflow/test/aop/cmp1/AComp.java | 21 ++++++ .../yomahub/liteflow/test/aop/cmp1/BComp.java | 21 ++++++ .../yomahub/liteflow/test/aop/cmp1/CComp.java | 21 ++++++ .../yomahub/liteflow/test/aop/cmp2/DComp.java | 21 ++++++ .../yomahub/liteflow/test/aop/cmp2/EComp.java | 21 ++++++ liteflow-test/src/test/resources/aop/flow.xml | 11 +++ .../src/test/resources/application.properties | 1 + pom.xml | 1 + 12 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 liteflow-test/pom.xml create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/TestRunner.java create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/AComp.java create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BComp.java create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CComp.java create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DComp.java create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/EComp.java create mode 100644 liteflow-test/src/test/resources/aop/flow.xml create mode 100644 liteflow-test/src/test/resources/application.properties diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java b/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java index 84b20153b..fe2a83c8a 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java @@ -101,15 +101,15 @@ public class FlowExecutor { execute(chainId, param, slotClazz, slotIndex,true); } - public LiteflowResponse execute(String chainId, Object param) throws Exception { + public LiteflowResponse execute(String chainId, Object param) throws Exception { return execute(chainId, param, DefaultSlot.class, null, false); } - public LiteflowResponse execute(String chainId, Object param, Class slotClazz) throws Exception { + public LiteflowResponse execute(String chainId, Object param, Class slotClazz) throws Exception { return execute(chainId, param, slotClazz,null,false); } - public LiteflowResponse execute(String chainId, Object param, Class slotClazz, Integer slotIndex, + public LiteflowResponse execute(String chainId, Object param, Class slotClazz, Integer slotIndex, boolean isInnerChain) throws Exception { Slot slot = null; diff --git a/liteflow-test/pom.xml b/liteflow-test/pom.xml new file mode 100644 index 000000000..9b535dc19 --- /dev/null +++ b/liteflow-test/pom.xml @@ -0,0 +1,67 @@ + + + + liteflow + com.yomahub + 2.5.0-SNAPSHOT + + 4.0.0 + + liteflow-test + + + + + + org.springframework.boot + spring-boot-dependencies + 2.0.5.RELEASE + pom + import + + + + + + + org.springframework.boot + spring-boot-starter + + + + com.yomahub + liteflow-spring-boot-starter + ${project.parent.version} + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + maven-deploy-plugin + + true + + + + + + \ No newline at end of file diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java new file mode 100644 index 000000000..b75988948 --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java @@ -0,0 +1,25 @@ +package com.yomahub.liteflow.test.aop; + +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.entity.data.LiteflowResponse; +import com.yomahub.liteflow.entity.data.Slot; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = TestRunner.class) +public class LiteflowAOPTest { + + @Resource + private FlowExecutor flowExecutor; + + @Test + public void testAop() throws Exception{ + LiteflowResponse response= flowExecutor.execute("chain2", "it's a request"); + System.out.println(response); + } +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/TestRunner.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/TestRunner.java new file mode 100644 index 000000000..a8cea9995 --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/TestRunner.java @@ -0,0 +1,16 @@ +package com.yomahub.liteflow.test.aop; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class TestRunner { + public static void main(String[] args) { + try{ + SpringApplication.run(TestRunner.class, args); + System.exit(0); + }catch (Throwable t){ + t.printStackTrace(); + } + } +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/AComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/AComp.java new file mode 100644 index 000000000..5e1716956 --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/AComp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.aop.cmp1; + +import com.yomahub.liteflow.core.NodeComponent; +import org.springframework.stereotype.Component; + +@Component("a") +public class AComp extends NodeComponent { + + @Override + public void process() { + System.out.println("Acomp executed!"); + } + +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BComp.java new file mode 100644 index 000000000..3e42b4ce4 --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BComp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.aop.cmp1; + +import com.yomahub.liteflow.core.NodeComponent; +import org.springframework.stereotype.Component; + +@Component("b") +public class BComp extends NodeComponent { + + @Override + public void process() { + System.out.println("Bcomp executed!"); + } + +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CComp.java new file mode 100644 index 000000000..63a6d37f5 --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CComp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.aop.cmp1; + +import com.yomahub.liteflow.core.NodeComponent; +import org.springframework.stereotype.Component; + +@Component("c") +public class CComp extends NodeComponent { + + @Override + public void process() { + System.out.println("Ccomp executed!"); + } + +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DComp.java new file mode 100644 index 000000000..e10c6b155 --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DComp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.aop.cmp2; + +import com.yomahub.liteflow.core.NodeComponent; +import org.springframework.stereotype.Component; + +@Component("d") +public class DComp extends NodeComponent { + + @Override + public void process() { + System.out.println("Dcomp executed!"); + } + +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/EComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/EComp.java new file mode 100644 index 000000000..dca2147a5 --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/EComp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.aop.cmp2; + +import com.yomahub.liteflow.core.NodeComponent; +import org.springframework.stereotype.Component; + +@Component("e") +public class EComp extends NodeComponent { + + @Override + public void process() { + System.out.println("Ecomp executed!"); + } + +} diff --git a/liteflow-test/src/test/resources/aop/flow.xml b/liteflow-test/src/test/resources/aop/flow.xml new file mode 100644 index 000000000..fcbc8b715 --- /dev/null +++ b/liteflow-test/src/test/resources/aop/flow.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/liteflow-test/src/test/resources/application.properties b/liteflow-test/src/test/resources/application.properties new file mode 100644 index 000000000..f9075d1c6 --- /dev/null +++ b/liteflow-test/src/test/resources/application.properties @@ -0,0 +1 @@ +liteflow.rule-source=aop/flow.xml \ No newline at end of file diff --git a/pom.xml b/pom.xml index 9ab409d0e..1b3458fd3 100644 --- a/pom.xml +++ b/pom.xml @@ -210,6 +210,7 @@ liteflow-core liteflow-spring-boot-starter + liteflow-test liteflow-test-springboot liteflow-test-spring From 1735c95cd182e2ca992a09b985c744e6f2d93e6c Mon Sep 17 00:00:00 2001 From: bryan31 Date: Tue, 30 Mar 2021 13:49:53 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liteflow-spring-boot-starter/pom.xml | 5 +++-- liteflow-test-spring/pom.xml | 7 +++++++ liteflow-test-springboot/pom.xml | 2 +- liteflow-test/pom.xml | 5 ++++- .../liteflow/test/aop/LiteflowAOPTest.java | 8 +++++++- .../yomahub/liteflow/test/aop/TestRunner.java | 16 ---------------- ...ion.properties => application-aop.properties} | 0 pom.xml | 1 + 8 files changed, 23 insertions(+), 21 deletions(-) delete mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/TestRunner.java rename liteflow-test/src/test/resources/{application.properties => application-aop.properties} (100%) diff --git a/liteflow-spring-boot-starter/pom.xml b/liteflow-spring-boot-starter/pom.xml index 57b54e9c5..8430bb604 100644 --- a/liteflow-spring-boot-starter/pom.xml +++ b/liteflow-spring-boot-starter/pom.xml @@ -22,12 +22,12 @@ org.springframework.boot spring-boot-autoconfigure - 2.0.5.RELEASE + ${springboot.version} org.springframework.boot spring-boot-configuration-processor - 2.0.5.RELEASE + ${springboot.version} @@ -36,6 +36,7 @@ org.springframework.boot spring-boot-maven-plugin + ${springboot.version} org.apache.maven.plugins diff --git a/liteflow-test-spring/pom.xml b/liteflow-test-spring/pom.xml index 1e6b94083..ab7150a40 100644 --- a/liteflow-test-spring/pom.xml +++ b/liteflow-test-spring/pom.xml @@ -53,7 +53,14 @@ + org.apache.maven.plugins + maven-surefire-plugin + 2.18.1 + + + org.apache.maven.plugins maven-deploy-plugin + 2.8.2 true diff --git a/liteflow-test-springboot/pom.xml b/liteflow-test-springboot/pom.xml index 389664a49..207a2a203 100644 --- a/liteflow-test-springboot/pom.xml +++ b/liteflow-test-springboot/pom.xml @@ -18,7 +18,7 @@ org.springframework.boot spring-boot-dependencies - 2.0.5.RELEASE + ${springboot.version} pom import diff --git a/liteflow-test/pom.xml b/liteflow-test/pom.xml index 9b535dc19..fc6a4c25a 100644 --- a/liteflow-test/pom.xml +++ b/liteflow-test/pom.xml @@ -17,7 +17,7 @@ org.springframework.boot spring-boot-dependencies - 2.0.5.RELEASE + ${springboot.version} pom import @@ -54,9 +54,12 @@ org.springframework.boot spring-boot-maven-plugin + ${springboot.version} + org.apache.maven.plugins maven-deploy-plugin + 2.8.2 true diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java index b75988948..609d22ef4 100644 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java @@ -5,13 +5,19 @@ import com.yomahub.liteflow.entity.data.LiteflowResponse; import com.yomahub.liteflow.entity.data.Slot; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @RunWith(SpringRunner.class) -@SpringBootTest(classes = TestRunner.class) +@ActiveProfiles("aop") +@SpringBootTest(classes = LiteflowAOPTest.class) +@EnableAutoConfiguration +@ComponentScan public class LiteflowAOPTest { @Resource diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/TestRunner.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/TestRunner.java deleted file mode 100644 index a8cea9995..000000000 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/TestRunner.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.yomahub.liteflow.test.aop; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class TestRunner { - public static void main(String[] args) { - try{ - SpringApplication.run(TestRunner.class, args); - System.exit(0); - }catch (Throwable t){ - t.printStackTrace(); - } - } -} diff --git a/liteflow-test/src/test/resources/application.properties b/liteflow-test/src/test/resources/application-aop.properties similarity index 100% rename from liteflow-test/src/test/resources/application.properties rename to liteflow-test/src/test/resources/application-aop.properties diff --git a/pom.xml b/pom.xml index 1b3458fd3..a5a22648b 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,7 @@ UTF-8 1.8 + 2.0.5.RELEASE 5.0.9.RELEASE 1.7.21 1.2.17 From 0ab3d2f4cc8c0e62e00d0809e057e5214da183b6 Mon Sep 17 00:00:00 2001 From: bryan31 Date: Tue, 30 Mar 2021 16:52:20 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow/aop/ICmpAroundAspect.java | 4 +- .../yomahub/liteflow/core/NodeComponent.java | 4 +- .../flowtest/aspect/ComponentAspect.java | 4 +- liteflow-test/pom.xml | 6 ++ .../liteflow/test/aop/LFCustomAOPTest.java | 53 +++++++++++++++++ .../liteflow/test/aop/LFGlobalAOPTest.java | 57 +++++++++++++++++++ .../liteflow/test/aop/LiteflowAOPTest.java | 31 ---------- .../liteflow/test/aop/aspect/CmpAspect.java | 18 ++++++ .../test/aop/aspect/CustomAspect.java | 27 +++++++++ .../test/aop/cmp1/{AComp.java => ACmp.java} | 2 +- .../test/aop/cmp1/{BComp.java => BCmp.java} | 2 +- .../test/aop/cmp1/{CComp.java => CCmp.java} | 2 +- .../test/aop/cmp2/{DComp.java => DCmp.java} | 2 +- .../test/aop/cmp2/{EComp.java => ECmp.java} | 2 +- 14 files changed, 172 insertions(+), 42 deletions(-) create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFGlobalAOPTest.java delete mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CmpAspect.java create mode 100644 liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CustomAspect.java rename liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/{AComp.java => ACmp.java} (90%) rename liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/{BComp.java => BCmp.java} (90%) rename liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/{CComp.java => CCmp.java} (90%) rename liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/{DComp.java => DCmp.java} (90%) rename liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/{EComp.java => ECmp.java} (90%) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/aop/ICmpAroundAspect.java b/liteflow-core/src/main/java/com/yomahub/liteflow/aop/ICmpAroundAspect.java index 7a2421d17..410a959c4 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/aop/ICmpAroundAspect.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/aop/ICmpAroundAspect.java @@ -16,7 +16,7 @@ import com.yomahub.liteflow.entity.data.Slot; */ public interface ICmpAroundAspect { - void beforeProcess(Slot slot); + void beforeProcess(String nodeId, Slot slot); - void afterProcess(Slot slot); + void afterProcess(String nodeId, Slot slot); } 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 927e4a17a..79865b752 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,14 +53,14 @@ public abstract class NodeComponent { // process前置处理 if (ObjectUtil.isNotNull(ComponentScaner.cmpAroundAspect)) { - ComponentScaner.cmpAroundAspect.beforeProcess(slot); + ComponentScaner.cmpAroundAspect.beforeProcess(this.getNodeId(), slot); } process(); // process后置处理 if (ObjectUtil.isNotNull(ComponentScaner.cmpAroundAspect)) { - ComponentScaner.cmpAroundAspect.afterProcess(slot); + ComponentScaner.cmpAroundAspect.afterProcess(this.getNodeId(), slot); } stopWatch.stop(); diff --git a/liteflow-test-springboot/src/main/java/com/yomahub/flowtest/aspect/ComponentAspect.java b/liteflow-test-springboot/src/main/java/com/yomahub/flowtest/aspect/ComponentAspect.java index aba478de7..48898ec45 100644 --- a/liteflow-test-springboot/src/main/java/com/yomahub/flowtest/aspect/ComponentAspect.java +++ b/liteflow-test-springboot/src/main/java/com/yomahub/flowtest/aspect/ComponentAspect.java @@ -7,12 +7,12 @@ import org.springframework.stereotype.Component; @Component public class ComponentAspect implements ICmpAroundAspect { @Override - public void beforeProcess(Slot slot) { + public void beforeProcess(String nodeId, Slot slot) { System.out.println("before process"); } @Override - public void afterProcess(Slot slot) { + public void afterProcess(String nodeId, Slot slot) { System.out.println("after process"); } } diff --git a/liteflow-test/pom.xml b/liteflow-test/pom.xml index fc6a4c25a..89c137087 100644 --- a/liteflow-test/pom.xml +++ b/liteflow-test/pom.xml @@ -36,6 +36,12 @@ ${project.parent.version} + + org.aspectj + aspectjweaver + 1.8.13 + + org.springframework.boot spring-boot-starter-test diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java new file mode 100644 index 000000000..b3e9e9033 --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java @@ -0,0 +1,53 @@ +package com.yomahub.liteflow.test.aop; + +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.entity.data.LiteflowResponse; +import com.yomahub.liteflow.entity.data.Slot; +import com.yomahub.liteflow.test.aop.aspect.CustomAspect; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +/** + * 切面场景单元测试 + * @author Bryan.Zhang + */ +@RunWith(SpringRunner.class) +@ActiveProfiles("aop") +@SpringBootTest(classes = LFCustomAOPTest.class) +@EnableAutoConfiguration +@Import(CustomAspect.class) +@ComponentScan({"com.yomahub.liteflow.test.aop.cmp1","com.yomahub.liteflow.test.aop.cmp2"}) +public class LFCustomAOPTest { + + @Resource + private FlowExecutor flowExecutor; + + //测试自定义AOP,串行场景 + @Test + public void testCustomAopS() throws Exception{ + LiteflowResponse response= flowExecutor.execute("chain1", "it's a request"); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("before_after", response.getData().getData("a")); + Assert.assertEquals("before_after", response.getData().getData("b")); + Assert.assertEquals("before_after", response.getData().getData("c")); + } + + //测试自定义AOP,并行场景 + @Test + public void testGlobalAopP() throws Exception{ + LiteflowResponse response= flowExecutor.execute("chain2", "it's a request"); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("before_after", response.getData().getData("a")); + Assert.assertEquals("before_after", response.getData().getData("b")); + Assert.assertEquals("before_after", response.getData().getData("c")); + } +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFGlobalAOPTest.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFGlobalAOPTest.java new file mode 100644 index 000000000..d060bfc00 --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFGlobalAOPTest.java @@ -0,0 +1,57 @@ +package com.yomahub.liteflow.test.aop; + +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.entity.data.LiteflowResponse; +import com.yomahub.liteflow.entity.data.Slot; +import com.yomahub.liteflow.test.aop.aspect.CmpAspect; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +/** + * 切面场景单元测试 + * @author Bryan.Zhang + */ +@RunWith(SpringRunner.class) +@ActiveProfiles("aop") +@SpringBootTest(classes = LFGlobalAOPTest.class) +@EnableAutoConfiguration +@Import(CmpAspect.class) +@ComponentScan({"com.yomahub.liteflow.test.aop.cmp1","com.yomahub.liteflow.test.aop.cmp2"}) +public class LFGlobalAOPTest { + + @Resource + private FlowExecutor flowExecutor; + + //测试全局AOP,串行场景 + @Test + public void testGlobalAopS() throws Exception{ + LiteflowResponse response= flowExecutor.execute("chain1", "it's a request"); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("before_after", response.getData().getData("a")); + Assert.assertEquals("before_after", response.getData().getData("b")); + Assert.assertEquals("before_after", response.getData().getData("c")); + Assert.assertEquals("before_after", response.getData().getData("d")); + Assert.assertEquals("before_after", response.getData().getData("e")); + } + + //测试全局AOP,并行场景 + @Test + public void testGlobalAopP() throws Exception{ + LiteflowResponse response= flowExecutor.execute("chain2", "it's a request"); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("before_after", response.getData().getData("a")); + Assert.assertEquals("before_after", response.getData().getData("b")); + Assert.assertEquals("before_after", response.getData().getData("c")); + Assert.assertEquals("before_after", response.getData().getData("d")); + Assert.assertEquals("before_after", response.getData().getData("e")); + } +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java deleted file mode 100644 index 609d22ef4..000000000 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LiteflowAOPTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.yomahub.liteflow.test.aop; - -import com.yomahub.liteflow.core.FlowExecutor; -import com.yomahub.liteflow.entity.data.LiteflowResponse; -import com.yomahub.liteflow.entity.data.Slot; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; - -import javax.annotation.Resource; - -@RunWith(SpringRunner.class) -@ActiveProfiles("aop") -@SpringBootTest(classes = LiteflowAOPTest.class) -@EnableAutoConfiguration -@ComponentScan -public class LiteflowAOPTest { - - @Resource - private FlowExecutor flowExecutor; - - @Test - public void testAop() throws Exception{ - LiteflowResponse response= flowExecutor.execute("chain2", "it's a request"); - System.out.println(response); - } -} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CmpAspect.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CmpAspect.java new file mode 100644 index 000000000..028d5801b --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CmpAspect.java @@ -0,0 +1,18 @@ +package com.yomahub.liteflow.test.aop.aspect; + +import cn.hutool.core.util.StrUtil; +import com.yomahub.liteflow.aop.ICmpAroundAspect; +import com.yomahub.liteflow.entity.data.Slot; +import org.springframework.stereotype.Component; + +public class CmpAspect implements ICmpAroundAspect { + @Override + public void beforeProcess(String nodeId, Slot slot) { + slot.setData(nodeId, "before"); + } + + @Override + public void afterProcess(String nodeId, Slot slot) { + slot.setData(nodeId, StrUtil.format("{}_{}", slot.getData(nodeId), "after")); + } +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CustomAspect.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CustomAspect.java new file mode 100644 index 000000000..7277a8ac5 --- /dev/null +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/aspect/CustomAspect.java @@ -0,0 +1,27 @@ +package com.yomahub.liteflow.test.aop.aspect; + +import cn.hutool.core.util.StrUtil; +import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.entity.data.Slot; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; + +@Aspect +public class CustomAspect { + + @Pointcut("execution(* com.yomahub.liteflow.test.aop.cmp1.*.process())") + public void cut() { + } + + @Around("cut()") + public Object around(ProceedingJoinPoint jp) throws Throwable { + NodeComponent cmp = (NodeComponent) jp.getThis(); + Slot slot = cmp.getSlot(); + slot.setData(cmp.getNodeId(), "before"); + Object returnObj = jp.proceed(); + slot.setData(cmp.getNodeId(), StrUtil.format("{}_{}", slot.getData(cmp.getNodeId()), "after")); + return returnObj; + } +} diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/AComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/ACmp.java similarity index 90% rename from liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/AComp.java rename to liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/ACmp.java index 5e1716956..7fd947163 100644 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/AComp.java +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/ACmp.java @@ -11,7 +11,7 @@ import com.yomahub.liteflow.core.NodeComponent; import org.springframework.stereotype.Component; @Component("a") -public class AComp extends NodeComponent { +public class ACmp extends NodeComponent { @Override public void process() { diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BCmp.java similarity index 90% rename from liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BComp.java rename to liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BCmp.java index 3e42b4ce4..d99d2a1e6 100644 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BComp.java +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/BCmp.java @@ -11,7 +11,7 @@ import com.yomahub.liteflow.core.NodeComponent; import org.springframework.stereotype.Component; @Component("b") -public class BComp extends NodeComponent { +public class BCmp extends NodeComponent { @Override public void process() { diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CCmp.java similarity index 90% rename from liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CComp.java rename to liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CCmp.java index 63a6d37f5..7a2181438 100644 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CComp.java +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp1/CCmp.java @@ -11,7 +11,7 @@ import com.yomahub.liteflow.core.NodeComponent; import org.springframework.stereotype.Component; @Component("c") -public class CComp extends NodeComponent { +public class CCmp extends NodeComponent { @Override public void process() { diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DCmp.java similarity index 90% rename from liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DComp.java rename to liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DCmp.java index e10c6b155..eb8ae7648 100644 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DComp.java +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/DCmp.java @@ -11,7 +11,7 @@ import com.yomahub.liteflow.core.NodeComponent; import org.springframework.stereotype.Component; @Component("d") -public class DComp extends NodeComponent { +public class DCmp extends NodeComponent { @Override public void process() { diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/EComp.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/ECmp.java similarity index 90% rename from liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/EComp.java rename to liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/ECmp.java index dca2147a5..ce0319d44 100644 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/EComp.java +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/cmp2/ECmp.java @@ -11,7 +11,7 @@ import com.yomahub.liteflow.core.NodeComponent; import org.springframework.stereotype.Component; @Component("e") -public class EComp extends NodeComponent { +public class ECmp extends NodeComponent { @Override public void process() { From 9e43170a0b7327e0874ae95a31007f387275fa37 Mon Sep 17 00:00:00 2001 From: bryan31 Date: Tue, 30 Mar 2021 19:15:53 +0800 Subject: [PATCH 4/4] =?UTF-8?q?enhancement=20#I3CT11=20=E8=83=BD=E5=AF=B9?= =?UTF-8?q?=E4=BB=BB=E6=84=8F=E5=8C=85=E7=9A=84=E4=B8=8B=E7=9A=84=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E8=BF=9B=E8=A1=8C=E5=88=87=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yomahub/liteflow/core/NodeComponent.java | 16 ++++++++++++++-- .../java/com/yomahub/liteflow/flow/FlowBus.java | 1 + .../yomahub/liteflow/spring/ComponentScaner.java | 15 ++++++--------- ...iteflowComponentScannerAutoConfiguration.java | 2 ++ .../META-INF/liteflow-default.properties | 2 +- .../liteflow/test/aop/LFCustomAOPTest.java | 5 ++++- 6 files changed, 28 insertions(+), 13 deletions(-) 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 79865b752..1f2f06074 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 @@ -34,13 +34,17 @@ public abstract class NodeComponent { private static final Logger LOG = LoggerFactory.getLogger(NodeComponent.class); - private TransmittableThreadLocal slotIndexTL = new TransmittableThreadLocal(); + private TransmittableThreadLocal slotIndexTL = new TransmittableThreadLocal<>(); @Autowired(required = false) private MonitorBus monitorBus; private String nodeId; + //这是自己的实例,取代this + //为何要设置这个,用this不行么,因为如果有aop去切的话,this在spring的aop里是切不到的。self对象有可能是代理过的对象 + private NodeComponent self; + //是否结束整个流程,这个只对串行流程有效,并行流程无效 private TransmittableThreadLocal isEndTL = new TransmittableThreadLocal<>(); @@ -56,7 +60,7 @@ public abstract class NodeComponent { ComponentScaner.cmpAroundAspect.beforeProcess(this.getNodeId(), slot); } - process(); + self.process(); // process后置处理 if (ObjectUtil.isNotNull(ComponentScaner.cmpAroundAspect)) { @@ -154,4 +158,12 @@ public abstract class NodeComponent { public void setNodeId(String nodeId) { this.nodeId = nodeId; } + + public NodeComponent getSelf() { + return self; + } + + public void setSelf(NodeComponent self) { + this.self = self; + } } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java index 4ce599809..e98efc9bf 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java @@ -14,6 +14,7 @@ import cn.hutool.core.map.MapUtil; import com.yomahub.liteflow.entity.flow.Chain; import com.yomahub.liteflow.entity.flow.Node; +import com.yomahub.liteflow.util.SpringAware; /** * 流程元数据类 diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/spring/ComponentScaner.java b/liteflow-core/src/main/java/com/yomahub/liteflow/spring/ComponentScaner.java index 2eafea230..136b3aea4 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/spring/ComponentScaner.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/spring/ComponentScaner.java @@ -14,6 +14,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; import org.springframework.core.Ordered; import org.springframework.core.PriorityOrdered; @@ -25,7 +26,7 @@ import com.yomahub.liteflow.util.LOGOPrinter; * 组件扫描类,只要是NodeComponent的实现类,都可以被这个扫描器扫到 * @author Bryan.Zhang */ -public class ComponentScaner implements BeanPostProcessor, PriorityOrdered { +public class ComponentScaner implements InstantiationAwareBeanPostProcessor { private static final Logger LOG = LoggerFactory.getLogger(ComponentScaner.class); @@ -39,19 +40,20 @@ public class ComponentScaner implements BeanPostProcessor, PriorityOrdered { } @Override - public int getOrder() { - return Ordered.LOWEST_PRECEDENCE; + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + return bean; } @SuppressWarnings("rawtypes") @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { Class clazz = bean.getClass(); // 组件的扫描发现,扫到之后缓存到类属性map中 if (NodeComponent.class.isAssignableFrom(clazz)) { LOG.info("component[{}] has been found", beanName); NodeComponent nodeComponent = (NodeComponent) bean; nodeComponent.setNodeId(beanName); + nodeComponent.setSelf(nodeComponent); nodeComponentMap.put(beanName, nodeComponent); } @@ -63,9 +65,4 @@ public class ComponentScaner implements BeanPostProcessor, PriorityOrdered { return bean; } - - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { - return bean; - } } diff --git a/liteflow-spring-boot-starter/src/main/java/com/yomahub/liteflow/springboot/LiteflowComponentScannerAutoConfiguration.java b/liteflow-spring-boot-starter/src/main/java/com/yomahub/liteflow/springboot/LiteflowComponentScannerAutoConfiguration.java index efeb21516..a415dbf8c 100644 --- a/liteflow-spring-boot-starter/src/main/java/com/yomahub/liteflow/springboot/LiteflowComponentScannerAutoConfiguration.java +++ b/liteflow-spring-boot-starter/src/main/java/com/yomahub/liteflow/springboot/LiteflowComponentScannerAutoConfiguration.java @@ -3,12 +3,14 @@ package com.yomahub.liteflow.springboot; import com.yomahub.liteflow.spring.ComponentScaner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; /** * 组件扫描器自动装配类 * @author Bryan.Zhang */ @Configuration +@EnableAspectJAutoProxy(exposeProxy = true) public class LiteflowComponentScannerAutoConfiguration { @Bean diff --git a/liteflow-spring-boot-starter/src/main/resources/META-INF/liteflow-default.properties b/liteflow-spring-boot-starter/src/main/resources/META-INF/liteflow-default.properties index 60ac4ce71..832357d8e 100644 --- a/liteflow-spring-boot-starter/src/main/resources/META-INF/liteflow-default.properties +++ b/liteflow-spring-boot-starter/src/main/resources/META-INF/liteflow-default.properties @@ -1,6 +1,6 @@ liteflow.rule-source=config/flow.xml liteflow.slot-size=1024 -liteflow.when-max-wait-second=15 +liteflow.when-max-wait-seconds=15 liteflow.when-max-workers=4 liteflow.when-queue-limit=512 liteflow.monitor.enable-log=false diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java index b3e9e9033..b6b67f2cc 100644 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java @@ -1,9 +1,12 @@ package com.yomahub.liteflow.test.aop; import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.core.NodeComponent; import com.yomahub.liteflow.entity.data.LiteflowResponse; import com.yomahub.liteflow.entity.data.Slot; import com.yomahub.liteflow.test.aop.aspect.CustomAspect; +import com.yomahub.liteflow.test.aop.cmp1.ACmp; +import com.yomahub.liteflow.util.SpringAware; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -43,7 +46,7 @@ public class LFCustomAOPTest { //测试自定义AOP,并行场景 @Test - public void testGlobalAopP() throws Exception{ + public void testCustomAopP() throws Exception{ LiteflowResponse response= flowExecutor.execute("chain2", "it's a request"); Assert.assertTrue(response.isSuccess()); Assert.assertEquals("before_after", response.getData().getData("a"));