From d918e1c64b51a3cd3caaa0d20ca1f43704b3e6e5 Mon Sep 17 00:00:00 2001 From: "everywhere.z" Date: Mon, 13 Mar 2023 18:32:14 +0800 Subject: [PATCH] =?UTF-8?q?feature=20#I6MPYF=20=E5=A2=9E=E5=8A=A0CATCH?= =?UTF-8?q?=E8=AF=AD=E6=B3=95=E8=A1=A8=E8=BE=BE=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../builder/el/operator/CatchOperator.java | 26 ++++++++ .../exception/CatchErrorException.java | 29 +++++++++ .../element/condition/CatchCondition.java | 63 +++++++++++++++++++ .../test/catchcase/CatchELSpringbootTest.java | 45 +++++++++++++ .../liteflow/test/catchcase/cmp/ACmp.java | 22 +++++++ .../liteflow/test/catchcase/cmp/BCmp.java | 21 +++++++ .../liteflow/test/catchcase/cmp/CCmp.java | 21 +++++++ .../liteflow/test/catchcase/cmp/DCmp.java | 21 +++++++ .../catchcase/application.properties | 1 + .../src/test/resources/catchcase/flow.el.xml | 15 +++++ 10 files changed, 264 insertions(+) create mode 100644 liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/CatchOperator.java create mode 100644 liteflow-core/src/main/java/com/yomahub/liteflow/exception/CatchErrorException.java create mode 100644 liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/CatchCondition.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/CatchELSpringbootTest.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/ACmp.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/BCmp.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/CCmp.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/DCmp.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/catchcase/application.properties create mode 100644 liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/catchcase/flow.el.xml diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/CatchOperator.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/CatchOperator.java new file mode 100644 index 000000000..4682409e3 --- /dev/null +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/CatchOperator.java @@ -0,0 +1,26 @@ +package com.yomahub.liteflow.builder.el.operator; + +import com.yomahub.liteflow.builder.el.operator.base.BaseOperator; +import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper; +import com.yomahub.liteflow.flow.element.Executable; +import com.yomahub.liteflow.flow.element.condition.CatchCondition; + +/** + * EL规则中的CATCH的操作符 + * 用法:CATCH...DO... + * @author Bryan.Zhang + * @since 2.9.8 + */ +public class CatchOperator extends BaseOperator { + @Override + public CatchCondition build(Object[] objects) throws Exception { + OperatorHelper.checkObjectSizeEq(objects, 1); + + Executable catchItem = OperatorHelper.convert(objects[0], Executable.class); + + CatchCondition catchCondition = new CatchCondition(); + catchCondition.setCatchItem(catchItem); + + return catchCondition; + } +} diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/exception/CatchErrorException.java b/liteflow-core/src/main/java/com/yomahub/liteflow/exception/CatchErrorException.java new file mode 100644 index 000000000..ae4081837 --- /dev/null +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/exception/CatchErrorException.java @@ -0,0 +1,29 @@ +package com.yomahub.liteflow.exception; + +/** + * 类型错误异常 + * @author Yun + */ +public class CatchErrorException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + /** + * 异常信息 + */ + private String message; + + public CatchErrorException(String message) { + this.message = message; + } + + @Override + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + +} diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/CatchCondition.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/CatchCondition.java new file mode 100644 index 000000000..a56d17f54 --- /dev/null +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/CatchCondition.java @@ -0,0 +1,63 @@ +package com.yomahub.liteflow.flow.element.condition; + +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import com.yomahub.liteflow.enums.ConditionTypeEnum; +import com.yomahub.liteflow.exception.CatchErrorException; +import com.yomahub.liteflow.flow.element.Executable; +import com.yomahub.liteflow.slot.DataBus; +import com.yomahub.liteflow.slot.Slot; + +/** + * Catch Condition + * @author Bryan.Zhang + * @since 2.9.8 + */ +public class CatchCondition extends Condition{ + + @Override + public void executeCondition(Integer slotIndex) throws Exception { + Slot slot = DataBus.getSlot(slotIndex); + try{ + Executable catchExecutable = this.getCatchItem(); + if (ObjectUtil.isNull(catchExecutable)){ + String errorInfo = StrUtil.format("[{}]:no catch item find", slot.getRequestId()); + throw new CatchErrorException(errorInfo); + } + catchExecutable.setCurrChainId(this.getCurrChainId()); + catchExecutable.execute(slotIndex); + }catch (Exception e){ + Executable doExecutable = this.getDoItem(); + if (ObjectUtil.isNull(doExecutable)){ + String errorInfo = StrUtil.format("[{}]:no catch do item find", slot.getRequestId()); + throw new CatchErrorException(errorInfo); + } + doExecutable.setCurrChainId(this.getCurrChainId()); + doExecutable.execute(slotIndex); + //catch之后需要把exception给清除掉 + //正如同java的catch一样,异常自己处理了,属于正常流程了,整个流程状态应该是成功的 + DataBus.getSlot(slotIndex).removeException(); + } + } + + @Override + public ConditionTypeEnum getConditionType() { + return ConditionTypeEnum.TYPE_CATCH; + } + + public Executable getCatchItem(){ + return this.getExecutableOne(ConditionKey.CATCH_KEY); + } + + public void setCatchItem(Executable executable){ + this.addExecutable(ConditionKey.CATCH_KEY, executable); + } + + public Executable getDoItem(){ + return this.getExecutableOne(ConditionKey.DO_KEY); + } + + public void setDoItem(Executable executable){ + this.addExecutable(ConditionKey.DO_KEY, executable); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/CatchELSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/CatchELSpringbootTest.java new file mode 100644 index 000000000..ffe41767f --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/CatchELSpringbootTest.java @@ -0,0 +1,45 @@ +package com.yomahub.liteflow.test.catchcase; + +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.flow.LiteflowResponse; +import com.yomahub.liteflow.test.BaseTest; +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.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +/** + * springboot环境EL常规的例子测试 + * @author Bryan.Zhang + */ +@RunWith(SpringRunner.class) +@TestPropertySource(value = "classpath:/catchcase/application.properties") +@SpringBootTest(classes = CatchELSpringbootTest.class) +@EnableAutoConfiguration +@ComponentScan({"com.yomahub.liteflow.test.catchcase.cmp"}) +public class CatchELSpringbootTest extends BaseTest { + + @Resource + private FlowExecutor flowExecutor; + + @Test + public void testCatch1() throws Exception{ + LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("a==>c", response.getExecuteStepStrWithoutTime()); + Assert.assertNull(response.getCause()); + } + + @Test + public void testCatch2() throws Exception{ + LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg"); + Assert.assertFalse(response.isSuccess()); + Assert.assertEquals("a==>d", response.getExecuteStepStrWithoutTime()); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/ACmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/ACmp.java new file mode 100644 index 000000000..cf3c9a38e --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/ACmp.java @@ -0,0 +1,22 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.catchcase.cmp; + +import com.yomahub.liteflow.core.NodeComponent; +import org.springframework.stereotype.Component; + +@Component("a") +public class ACmp extends NodeComponent { + + @Override + public void process() { + System.out.println("ACmp executed!"); + int i = 1/0; + System.out.println(i); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/BCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/BCmp.java new file mode 100644 index 000000000..47b9b2795 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/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.catchcase.cmp; + +import com.yomahub.liteflow.core.NodeComponent; +import org.springframework.stereotype.Component; + +@Component("b") +public class BCmp extends NodeComponent { + + @Override + public void process() { + System.out.println("BCmp executed!"); + } + +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/CCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/CCmp.java new file mode 100644 index 000000000..4d8034647 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/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.catchcase.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(this.getSlot().getException().getMessage()); + } + +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/DCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/DCmp.java new file mode 100644 index 000000000..c44b8976a --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/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.catchcase.cmp; + +import com.yomahub.liteflow.core.NodeComponent; +import org.springframework.stereotype.Component; + +@Component("d") +public class DCmp extends NodeComponent { + + @Override + public void process() { + throw new RuntimeException("test"); + } + +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/catchcase/application.properties b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/catchcase/application.properties new file mode 100644 index 000000000..6f8b8094e --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/catchcase/application.properties @@ -0,0 +1 @@ +liteflow.rule-source=catchcase/flow.el.xml \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/catchcase/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/catchcase/flow.el.xml new file mode 100644 index 000000000..026ef934a --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/catchcase/flow.el.xml @@ -0,0 +1,15 @@ + + + + + CATCH( + THEN(a,b) + ).DO(c) + + + + CATCH( + THEN(a,b) + ).DO(d) + + \ No newline at end of file