Pārlūkot izejas kodu

enhancement #I426YS 支持多个不同类型的配置方式同时解析

bryan31 3 gadi atpakaļ
vecāks
revīzija
ebc5d812f1

+ 29 - 15
liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java

@@ -8,6 +8,7 @@
  */
 package com.yomahub.liteflow.core;
 
+import cn.hutool.core.collection.ListUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.ReUtil;
 import cn.hutool.core.util.StrUtil;
@@ -98,6 +99,15 @@ public class FlowExecutor {
                     }
                 }
                 rulePathList.add(path);
+
+                //支持多类型的配置文件,分别解析
+                if (liteflowConfig.isSupportMultipleType()){
+                    if (ObjectUtil.isNotNull(parser)) {
+                        parser.parseMain(ListUtil.toList(path));
+                    } else {
+                        throw new ConfigErrorException("parse error, please check liteflow config property");
+                    }
+                }
             } catch (Exception e) {
                 String errorMsg = StrUtil.format("init flow executor cause error,cannot find the parse for path {}", path);
                 LOG.error(errorMsg, e);
@@ -105,23 +115,27 @@ public class FlowExecutor {
             }
         }
 
-        //检查Parser是否只有一个,因为多个不同的parser会造成子流程的混乱
-        if (parserNameSet.size() > 1){
-            String errorMsg = "cannot have multiple different parsers";
-            LOG.error(errorMsg);
-            throw new MultipleParsersException(errorMsg);
-        }
+        //单类型的配置文件,需要一起解析
+        if (!liteflowConfig.isSupportMultipleType()){
+            //检查Parser是否只有一个,因为多个不同的parser会造成子流程的混乱
+            if (parserNameSet.size() > 1){
+                String errorMsg = "cannot have multiple different parsers";
+                LOG.error(errorMsg);
+                throw new MultipleParsersException(errorMsg);
+            }
 
-        try{
-            if (ObjectUtil.isNotNull(parser)) {
-                parser.parseMain(rulePathList);
-            } else {
-                throw new ConfigErrorException("parse error, please check liteflow config property");
+            //进行
+            try{
+                if (ObjectUtil.isNotNull(parser)) {
+                    parser.parseMain(rulePathList);
+                } else {
+                    throw new ConfigErrorException("parse error, please check liteflow config property");
+                }
+            } catch (Exception e) {
+                String errorMsg = StrUtil.format("init flow executor cause error,can not parse rule file {}", rulePathList);
+                LOG.error(errorMsg, e);
+                throw new FlowExecutorNotInitException(errorMsg);
             }
-        } catch (Exception e) {
-            String errorMsg = StrUtil.format("init flow executor cause error,can not parse rule file {}", rulePathList);
-            LOG.error(errorMsg, e);
-            throw new FlowExecutorNotInitException(errorMsg);
         }
     }
 

+ 17 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/property/LiteflowConfig.java

@@ -8,6 +8,7 @@
 package com.yomahub.liteflow.property;
 
 import cn.hutool.core.util.ObjectUtil;
+import com.sun.org.apache.xpath.internal.operations.Bool;
 
 /**
  * liteflow的配置实体类
@@ -54,6 +55,10 @@ public class LiteflowConfig {
     //这个参数主要给编码式注册元数据的场景用的,结合FlowBus.addNode一起用
     private Boolean parseOnStart;
 
+    //这个属性为true,则支持多种不同的类型的配置
+    //但是要注意,不能将主流程和子流程分配在不同类型配置文件中
+    private Boolean supportMultipleType;
+
     public Boolean getEnable() {
         if (ObjectUtil.isNull(enable)){
             return true;
@@ -181,4 +186,16 @@ public class LiteflowConfig {
     public void setParseOnStart(Boolean parseOnStart) {
         this.parseOnStart = parseOnStart;
     }
+
+    public Boolean isSupportMultipleType() {
+        if (ObjectUtil.isNull(supportMultipleType)){
+            return true;
+        }else{
+            return supportMultipleType;
+        }
+    }
+
+    public void setSupportMultipleType(Boolean supportMultipleType) {
+        this.supportMultipleType = supportMultipleType;
+    }
 }

+ 12 - 0
liteflow-spring-boot-starter/src/main/java/com/yomahub/liteflow/springboot/LiteflowProperty.java

@@ -31,6 +31,10 @@ public class LiteflowProperty {
     //这个参数主要给编码式注册元数据的场景用的,结合FlowBus.addNode一起用
     private boolean parseOnStart;
 
+    //这个属性为true,则支持多种不同的类型的配置
+    //但是要注意,不能将主流程和子流程分配在不同类型配置文件中
+    private boolean supportMultipleType;
+
     public boolean isEnable() {
         return enable;
     }
@@ -86,4 +90,12 @@ public class LiteflowProperty {
     public void setParseOnStart(boolean parseOnStart) {
         this.parseOnStart = parseOnStart;
     }
+
+    public boolean isSupportMultipleType() {
+        return supportMultipleType;
+    }
+
+    public void setSupportMultipleType(boolean supportMultipleType) {
+        this.supportMultipleType = supportMultipleType;
+    }
 }

+ 1 - 0
liteflow-spring-boot-starter/src/main/java/com/yomahub/liteflow/springboot/LiteflowPropertyAutoConfiguration.java

@@ -34,6 +34,7 @@ public class LiteflowPropertyAutoConfiguration {
         liteflowConfig.setWhenQueueLimit(property.getWhenQueueLimit());
         liteflowConfig.setParseOnStart(property.isParseOnStart());
         liteflowConfig.setEnable(property.isEnable());
+        liteflowConfig.setSupportMultipleType(property.isSupportMultipleType());
         return liteflowConfig;
     }
 }

+ 13 - 0
liteflow-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json

@@ -1,5 +1,11 @@
 {
   "properties": [
+    {
+      "name": "liteflow.enable",
+      "type": "java.lang.Boolean",
+      "description": "Whether to turn on liteflow.",
+      "sourceType": "com.yomahub.liteflow.springboot.LiteflowProperty"
+    },
     {
       "name": "liteflow.rule-source",
       "type": "java.lang.String",
@@ -40,6 +46,13 @@
       "sourceType": "com.yomahub.liteflow.springboot.LiteflowProperty",
       "defaultValue": true
     },
+    {
+      "name": "liteflow.support-multiple-type",
+      "type": "java.lang.Boolean",
+      "description": "Whether to support multiple types of configuration.",
+      "sourceType": "com.yomahub.liteflow.springboot.LiteflowProperty",
+      "defaultValue": true
+    },
     {
       "name": "liteflow.monitor.enable-log",
       "type": "java.lang.Boolean",

+ 1 - 0
liteflow-spring-boot-starter/src/main/resources/META-INF/liteflow-default.properties

@@ -5,6 +5,7 @@ liteflow.when-max-wait-seconds=15
 liteflow.when-max-workers=4
 liteflow.when-queue-limit=512
 liteflow.parse-on-start=true
+liteflow.support-multiple-type=false
 liteflow.monitor.enable-log=false
 liteflow.monitor.queue-limit=200
 liteflow.monitor.delay=300000

+ 42 - 0
liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/multipleType/LiteflowMultipleTypeSpringbootTest.java

@@ -0,0 +1,42 @@
+package com.yomahub.liteflow.test.multipleType;
+
+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:/multipleType/application.properties")
+@SpringBootTest(classes = LiteflowMultipleTypeSpringbootTest.class)
+@EnableAutoConfiguration
+@ComponentScan({"com.yomahub.liteflow.test.multipleType.cmp"})
+public class LiteflowMultipleTypeSpringbootTest extends BaseTest {
+
+    @Autowired
+    private FlowExecutor flowExecutor;
+
+    @Test
+    public void testConfig() {
+        LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain1", "arg");
+        Assert.assertTrue(response.isSuccess());
+        Assert.assertEquals("a==>b==>c==>b==>a", response.getSlot().printStep());
+        response = flowExecutor.execute2Resp("chain3", "arg");
+        Assert.assertTrue(response.isSuccess());
+        Assert.assertEquals("a==>b==>c", response.getSlot().printStep());
+    }
+}

+ 20 - 0
liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/multipleType/cmp/ACmp.java

@@ -0,0 +1,20 @@
+/**
+ * <p>Title: liteflow</p>
+ * <p>Description: 轻量级的组件式流程框架</p>
+ * @author Bryan.Zhang
+ * @email weenyc31@163.com
+ * @Date 2020/4/1
+ */
+package com.yomahub.liteflow.test.multipleType.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!");
+	}
+}

+ 21 - 0
liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/multipleType/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.multipleType.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-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/multipleType/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.multipleType.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("CCmp executed!");
+	}
+
+}

+ 2 - 0
liteflow-spring-boot-starter/src/test/resources/multipleType/application.properties

@@ -0,0 +1,2 @@
+liteflow.support-multiple-type=true
+liteflow.rule-source=multipleType/flow.xml,multipleType/flow.yml

+ 10 - 0
liteflow-spring-boot-starter/src/test/resources/multipleType/flow.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<flow>
+    <chain name="chain1">
+        <then value="a,b,chain2"/>
+    </chain>
+
+    <chain name="chain2">
+        <then value="c,b,a"/>
+    </chain>
+</flow>

+ 6 - 0
liteflow-spring-boot-starter/src/test/resources/multipleType/flow.yml

@@ -0,0 +1,6 @@
+flow:
+  chain:
+    - name: chain3
+      condition:
+        - type: then
+          value: 'a,b,c'