浏览代码

feature #I6MPYF 增加CATCH语法表达式

everywhere.z 2 年之前
父节点
当前提交
d918e1c64b

+ 26 - 0
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<CatchCondition> {
+    @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;
+    }
+}

+ 29 - 0
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;
+    }
+
+}

+ 63 - 0
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);
+    }
+}

+ 45 - 0
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());
+    }
+}

+ 22 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/ACmp.java

@@ -0,0 +1,22 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @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);
+	}
+}

+ 21 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/BCmp.java

@@ -0,0 +1,21 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @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!");
+	}
+
+}

+ 21 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/CCmp.java

@@ -0,0 +1,21 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @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());
+	}
+
+}

+ 21 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/cmp/DCmp.java

@@ -0,0 +1,21 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @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");
+	}
+
+}

+ 1 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/catchcase/application.properties

@@ -0,0 +1 @@
+liteflow.rule-source=catchcase/flow.el.xml

+ 15 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/catchcase/flow.el.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE flow PUBLIC  "liteflow" "liteflow.dtd">
+<flow>
+    <chain name="chain1">
+        CATCH(
+            THEN(a,b)
+        ).DO(c)
+    </chain>
+
+    <chain name="chain2">
+        CATCH(
+            THEN(a,b)
+        ).DO(d)
+    </chain>
+</flow>