1
0
Эх сурвалжийг харах

bug #I3NSF8 空的flow文件会导致无法启动

bryan31 4 жил өмнө
parent
commit
d77af44bd1

+ 5 - 18
liteflow-core/src/main/java/com/yomahub/liteflow/parser/JsonFlowParser.java

@@ -28,6 +28,10 @@ public abstract class JsonFlowParser extends FlowParser{
 
     @Override
     public void parse(String content) throws Exception {
+        if (StrUtil.isBlank(content)){
+            return;
+        }
+
         //把字符串原生转换为json对象,如果不加第二个参数OrderedField,会无序
         JSONObject flowJsonObject = JSONObject.parseObject(content, Feature.OrderedField);
         parse(flowJsonObject);
@@ -41,28 +45,11 @@ public abstract class JsonFlowParser extends FlowParser{
                 JSONArray nodeArrayList = flowJsonObject.getJSONObject("flow").getJSONObject("nodes").getJSONArray("node");
                 String id;
                 String clazz;
-                Node node;
-                NodeComponent component;
-                Class<NodeComponent> nodeComponentClass;
                 for(int i = 0; i< nodeArrayList.size(); i++) {
                     JSONObject nodeObject = nodeArrayList.getJSONObject(i);
-                    node = new Node();
                     id = nodeObject.getString("id");
                     clazz = nodeObject.getString("class");
-                    node.setId(id);
-                    node.setClazz(clazz);
-                    nodeComponentClass = (Class<NodeComponent>)Class.forName(clazz);
-                    //以node方式配置,本质上是为了适配无spring的环境,如果有spring环境,其实不用这么配置
-                    //这里的逻辑是判断是否能从spring上下文中取到,如果没有spring,则就是new instance了
-                    component = SpringAware.registerOrGet(nodeComponentClass);
-                    if (ObjectUtil.isNull(component)) {
-                        LOG.error("couldn't find component class [{}] from spring context", clazz);
-                        component = nodeComponentClass.newInstance();
-                    }
-                    component.setNodeId(id);
-                    component.setSelf(component);
-                    node.setInstance(component);
-                    FlowBus.addNode(id, node);
+                    FlowBus.addNode(id, clazz);
                 }
             } else {
                 for(Map.Entry<String, NodeComponent> componentEntry : ComponentScanner.nodeComponentMap.entrySet()){

+ 3 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/parser/XmlFlowParser.java

@@ -32,6 +32,9 @@ public abstract class XmlFlowParser extends FlowParser{
 	private final Logger LOG = LoggerFactory.getLogger(XmlFlowParser.class);
 
 	public void parse(String content) throws Exception {
+		if (StrUtil.isBlank(content)){
+			return;
+		}
 		Document document = DocumentHelper.parseText(content);
 		parse(document);
 	}

+ 3 - 0
liteflow-core/src/main/java/com/yomahub/liteflow/parser/YmlFlowParser.java

@@ -22,6 +22,9 @@ public abstract class YmlFlowParser extends JsonFlowParser{
     @Override
     public void parseMain(String rulePath) throws Exception {
         String ruleContent = ResourceUtil.readUtf8Str(StrUtil.format("classpath:{}",rulePath));
+        if (StrUtil.isBlank(ruleContent)){
+            return;
+        }
         JSONObject ruleObject = convertToJson(ruleContent);
         parse(ruleObject.toJSONString());
     }

+ 39 - 0
liteflow-spring-boot-starter/src/test/java/com/yomahub/liteflow/test/emptyflow/EmptyFlowTest.java

@@ -0,0 +1,39 @@
+package com.yomahub.liteflow.test.emptyflow;
+
+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 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.TestPropertySource;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import javax.annotation.Resource;
+
+/**
+ * 切面场景单元测试
+ * @author Bryan.Zhang
+ */
+@RunWith(SpringRunner.class)
+@TestPropertySource(value = "classpath:/emptyFlow/application.properties")
+@SpringBootTest(classes = EmptyFlowTest.class)
+@EnableAutoConfiguration
+@Import(CustomAspect.class)
+public class EmptyFlowTest extends BaseTest {
+
+    @Resource
+    private FlowExecutor flowExecutor;
+
+    //测试自定义AOP,串行场景
+    @Test
+    public void testEmptyFlow() {
+        //不做任何事,为的是能正常启动
+    }
+}

+ 1 - 0
liteflow-spring-boot-starter/src/test/resources/emptyFlow/application.properties

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

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


+ 1 - 2
liteflow-test-springboot/src/main/java/com/yomahub/flowtest/Runner.java

@@ -9,8 +9,7 @@ public class Runner {
     public static void main(String[] args) {
         try{
             SpringApplication.run(Runner.class, args);
-
-//            System.exit(0);
+            System.exit(0);
         }catch (Throwable t){
             t.printStackTrace();
         }

+ 1 - 1
liteflow-test-springboot/src/main/java/com/yomahub/flowtest/TestFlow.java

@@ -15,7 +15,7 @@ public class TestFlow implements CommandLineRunner {
 
     @Override
     public void run(String... args) throws Exception {
-        LiteflowResponse response= flowExecutor.execute2Resp("chain4", "it's a request");
+        LiteflowResponse response= flowExecutor.execute2Resp("chain3", "it's a request");
         System.out.println(response);
     }
 }

+ 0 - 18
liteflow-test-springboot/src/main/java/com/yomahub/flowtest/aspect/ComponentAspect.java

@@ -1,18 +0,0 @@
-package com.yomahub.flowtest.aspect;
-
-import com.yomahub.liteflow.aop.ICmpAroundAspect;
-import com.yomahub.liteflow.entity.data.Slot;
-import org.springframework.stereotype.Component;
-
-@Component
-public class ComponentAspect implements ICmpAroundAspect {
-    @Override
-    public void beforeProcess(String nodeId, Slot slot) {
-        System.out.println("before process");
-    }
-
-    @Override
-    public void afterProcess(String nodeId, Slot slot) {
-        System.out.println("after process");
-    }
-}

+ 0 - 131
liteflow-test-springboot/src/main/java/com/yomahub/flowtest/curator/CuratorTest.java

@@ -1,131 +0,0 @@
-package com.yomahub.flowtest.curator;
-
-import org.apache.curator.framework.CuratorFramework;
-import org.apache.curator.framework.CuratorFrameworkFactory;
-import org.apache.curator.framework.recipes.cache.*;
-import org.apache.curator.retry.RetryNTimes;
-
-public class CuratorTest {
-
-    /** Zookeeper info */
-    private static final String ZK_ADDRESS = "123.206.92.144:2181,123.206.92.144:2182,123.206.92.144:2183";
-    private static final String ZK_PATH = "/zktest/a1/aa1";
-
-    public static void main(String[] args) throws Exception {
-        // 1.Connect to zk
-        CuratorFramework client = CuratorFrameworkFactory.newClient(
-                ZK_ADDRESS,
-                new RetryNTimes(10, 5000)
-        );
-        client.start();
-
-        checkNode(client);
-
-//        childNodeListen(client);
-
-//        removeNodeData(client);
-
-//        createNode(client);
-
-//        nodeListen(client);
-//
-//        modifyNodeData(client);
-
-        System.in.read();
-
-//        getNodeData(client);
-//
-//
-
-    }
-
-    private static void checkNode(CuratorFramework client) throws Exception {
-    	System.out.println(client.checkExists().forPath("/test"));
-    }
-
-    private static void createNode(CuratorFramework client) throws Exception {
-    	String data1 = "nice to meet you";
-        print("create", ZK_PATH, data1);
-        client.create().
-                creatingParentsIfNeeded().
-                forPath(ZK_PATH, data1.getBytes());
-    }
-
-    private static void getNodeData(CuratorFramework client) throws Exception {
-    	print("ls", "/");
-        print(client.getChildren().forPath("/"));
-        print("get", ZK_PATH);
-        print(client.getData().forPath(ZK_PATH));
-    }
-
-    private static void modifyNodeData(CuratorFramework client) throws Exception {
-    	String data2 = "world for u";
-        print("set", ZK_PATH, data2);
-        client.setData().forPath(ZK_PATH, data2.getBytes());
-        print("get", ZK_PATH);
-        print(client.getData().forPath(ZK_PATH));
-    }
-
-    private static void removeNodeData(CuratorFramework client) throws Exception {
-    	print("delete", "/zktest/dddd");
-        client.delete().forPath("/zktest/dddd");
-        print("ls", "/");
-        print(client.getChildren().forPath("/"));
-    }
-
-    private static void nodeListen(CuratorFramework client) throws Exception {
-    	final NodeCache cache = new NodeCache(client,ZK_PATH);
-        cache.start();
-
-        cache.getListenable().addListener(new NodeCacheListener() {
-
-            @Override
-            public void nodeChanged() throws Exception {
-                byte[] res = cache.getCurrentData().getData();
-                System.out.println("data: " + new String(res));
-            }
-        });
-    }
-
-    private static void childNodeListen(CuratorFramework client) throws Exception {
-    	final PathChildrenCache cache = new PathChildrenCache(client,"/zktest",true);
-        cache.start();
-
-        cache.getListenable().addListener(new PathChildrenCacheListener() {
-
-            @Override
-            public void childEvent(CuratorFramework curator, PathChildrenCacheEvent event) throws Exception {
-                switch (event.getType()) {
-                case CHILD_ADDED:
-                    System.out.println("add:" + event.getData().getPath() + ":" + new String(event.getData().getData()));
-                    break;
-                case CHILD_UPDATED:
-                    System.out.println("update:" + event.getData().getPath() + ":" + new String(event.getData().getData()));
-                    break;
-                case CHILD_REMOVED:
-                    System.out.println("remove:" + event.getData().getPath() + ":" + new String(event.getData().getData()));
-                    break;
-                default:
-                    break;
-                }
-            }
-        });
-    }
-
-
-    private static void print(String... cmds) {
-        StringBuilder text = new StringBuilder("$ ");
-        for (String cmd : cmds) {
-            text.append(cmd).append(" ");
-        }
-        System.out.println(text.toString());
-    }
-
-    private static void print(Object result) {
-        System.out.println(
-                result instanceof byte[]
-                    ? new String((byte[]) result)
-                        : result);
-    }
-
-}

+ 0 - 117
liteflow-test-springboot/src/main/java/com/yomahub/flowtest/curator/CuratorTest2.java

@@ -1,117 +0,0 @@
-package com.yomahub.flowtest.curator;
-
-import org.apache.curator.framework.CuratorFramework;
-import org.apache.curator.framework.CuratorFrameworkFactory;
-import org.apache.curator.framework.recipes.cache.*;
-import org.apache.curator.retry.RetryNTimes;
-
-public class CuratorTest2 {
-
-    /** Zookeeper info */
-    private static final String ZK_ADDRESS = "114.55.174.189:2181";
-    private static final String ZK_PATH = "/zktest/ffff";
-
-    public static void main(String[] args) throws Exception {
-        // 1.Connect to zk
-        CuratorFramework client = CuratorFrameworkFactory.newClient(
-                ZK_ADDRESS,
-                new RetryNTimes(10, 5000)
-        );
-        client.start();
-
-//        removeNodeData(client);
-
-//        createNode(client);
-
-//        nodeListen(client);
-//
-        modifyNodeData(client);
-
-    }
-
-    private static void createNode(CuratorFramework client) throws Exception {
-    	String data1 = "hello";
-        print("create", ZK_PATH, data1);
-        client.create().
-                creatingParentsIfNeeded().
-                forPath(ZK_PATH, data1.getBytes());
-    }
-
-    private static void getNodeData(CuratorFramework client) throws Exception {
-    	print("ls", "/");
-        print(client.getChildren().forPath("/"));
-        print("get", ZK_PATH);
-        print(client.getData().forPath(ZK_PATH));
-    }
-
-    private static void modifyNodeData(CuratorFramework client) throws Exception {
-    	String data2 = "world for u";
-        print("set", ZK_PATH, data2);
-        client.setData().forPath(ZK_PATH, data2.getBytes());
-        print("get", ZK_PATH);
-        print(client.getData().forPath(ZK_PATH));
-    }
-
-    private static void removeNodeData(CuratorFramework client) throws Exception {
-    	print("delete", "/zktest/dddd");
-        client.delete().forPath("/zktest/dddd");
-        print("ls", "/");
-        print(client.getChildren().forPath("/"));
-    }
-
-    private static void nodeListen(CuratorFramework client) throws Exception {
-    	final NodeCache cache = new NodeCache(client,ZK_PATH);
-        cache.start();
-
-        cache.getListenable().addListener(new NodeCacheListener() {
-
-            @Override
-            public void nodeChanged() throws Exception {
-                byte[] res = cache.getCurrentData().getData();
-                System.out.println("data: " + new String(res));
-            }
-        });
-    }
-
-    private static void childNodeListen(CuratorFramework client) throws Exception {
-    	final PathChildrenCache cache = new PathChildrenCache(client,"/zktest",true);
-        cache.start();
-
-        cache.getListenable().addListener(new PathChildrenCacheListener() {
-
-            @Override
-            public void childEvent(CuratorFramework curator, PathChildrenCacheEvent event) throws Exception {
-                switch (event.getType()) {
-                case CHILD_ADDED:
-                    System.out.println("add:" + event.getData().getPath() + ":" + new String(event.getData().getData()));
-                    break;
-                case CHILD_UPDATED:
-                    System.out.println("update:" + event.getData().getPath() + ":" + new String(event.getData().getData()));
-                    break;
-                case CHILD_REMOVED:
-                    System.out.println("remove:" + event.getData().getPath() + ":" + new String(event.getData().getData()));
-                    break;
-                default:
-                    break;
-                }
-            }
-        });
-    }
-
-
-    private static void print(String... cmds) {
-        StringBuilder text = new StringBuilder("$ ");
-        for (String cmd : cmds) {
-            text.append(cmd).append(" ");
-        }
-        System.out.println(text.toString());
-    }
-
-    private static void print(Object result) {
-        System.out.println(
-                result instanceof byte[]
-                    ? new String((byte[]) result)
-                        : result);
-    }
-
-}

+ 0 - 23
liteflow-test-springboot/src/main/java/com/yomahub/flowtest/regex/RegexTest.java

@@ -1,23 +0,0 @@
-package com.yomahub.flowtest.regex;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-public class RegexTest {
-
-	public static void main(String[] args) {
-		String str = "192.168.1.1:2181,192.168.1.2:2182,192.168.1.3:2183";
-		List<String> list = new ArrayList<String>();
-	    Pattern p = Pattern.compile("[\\w\\d][\\w\\d\\.]+\\:(\\d)+(\\,[\\w\\d][\\w\\d\\.]+\\:(\\d)+)*");
-	    Matcher m = p.matcher(str);
-	    while(m.find()){
-	        list.add(m.group());
-	    }
-	    System.out.println(list.size());
-	    System.out.println(list);
-
-	}
-
-}

+ 1 - 4
liteflow-test-springboot/src/main/resources/application.properties

@@ -1,7 +1,4 @@
-#liteflow.rule-source=config/flow.xml
-#liteflow.rule-source=config/flow.yml
-liteflow.rule-source=config/flow.json;com.yomahub.flowtest.custom.CustomXmlClass
-#liteflow.slot-size=2048
+liteflow.rule-source=config/flow.xml
 liteflow.when-max-wait-seconds=20
 liteflow.monitor.enable-log=true
 liteflow.monitor.queue-limit=300