diff --git a/liteflow-rule-plugin/liteflow-rule-zk/src/main/java/com/yomahub/liteflow/parser/zk/util/ZkParserHelper.java b/liteflow-rule-plugin/liteflow-rule-zk/src/main/java/com/yomahub/liteflow/parser/zk/util/ZkParserHelper.java index 00874a018..c994b2b56 100644 --- a/liteflow-rule-plugin/liteflow-rule-zk/src/main/java/com/yomahub/liteflow/parser/zk/util/ZkParserHelper.java +++ b/liteflow-rule-plugin/liteflow-rule-zk/src/main/java/com/yomahub/liteflow/parser/zk/util/ZkParserHelper.java @@ -19,6 +19,7 @@ import org.apache.curator.framework.recipes.cache.CuratorCacheListener; import org.apache.curator.retry.RetryNTimes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -37,6 +38,8 @@ public class ZkParserHelper { private final String NODE_ITEM_XML_PATTERN = ""; + private final String NODE_ITEM_XML_WITH_LANGUAGE_PATTERN = ""; + private final String XML_PATTERN = "{}{}"; public ZkParserHelper(ZkParserVO zkParserVO) { @@ -91,8 +94,17 @@ public class ZkParserHelper { String scriptData = new String(client.getData() .forPath(StrUtil.format("{}/{}", zkParserVO.getScriptPath(), scriptNodeValue))); - scriptItemContentList.add(StrUtil.format(NODE_ITEM_XML_PATTERN, nodeSimpleVO.getNodeId(), - nodeSimpleVO.getName(), nodeSimpleVO.getType(), scriptData)); + // 有语言类型 + if (StrUtil.isNotBlank(nodeSimpleVO.getLanguage())) { + scriptItemContentList.add(StrUtil.format(NODE_ITEM_XML_WITH_LANGUAGE_PATTERN, + nodeSimpleVO.getNodeId(), nodeSimpleVO.getName(), nodeSimpleVO.getType(), + nodeSimpleVO.getLanguage(), scriptData)); + } + // 没有语言类型 + else { + scriptItemContentList.add(StrUtil.format(NODE_ITEM_XML_PATTERN, nodeSimpleVO.getNodeId(), + nodeSimpleVO.getName(), nodeSimpleVO.getType(), scriptData)); + } } scriptAllContent = StrUtil.format(NODE_XML_PATTERN, @@ -168,12 +180,25 @@ public class ZkParserHelper { LOG.info("starting reload flow config... {} path={} value={},", type.name(), path, value); String scriptNodeValue = FileNameUtil.getName(path); NodeSimpleVO nodeSimpleVO = convert(scriptNodeValue); - LiteFlowNodeBuilder.createScriptNode() - .setId(nodeSimpleVO.getNodeId()) - .setType(NodeTypeEnum.getEnumByCode(nodeSimpleVO.type)) - .setName(nodeSimpleVO.getName()) - .setScript(value) - .build(); + // 有语言类型 + if (StrUtil.isNotBlank(nodeSimpleVO.getLanguage())) { + LiteFlowNodeBuilder.createScriptNode() + .setId(nodeSimpleVO.getNodeId()) + .setType(NodeTypeEnum.getEnumByCode(nodeSimpleVO.type)) + .setName(nodeSimpleVO.getName()) + .setScript(value) + .setLanguage(nodeSimpleVO.getLanguage()) + .build(); + } + // 没有语言类型 + else { + LiteFlowNodeBuilder.createScriptNode() + .setId(nodeSimpleVO.getNodeId()) + .setType(NodeTypeEnum.getEnumByCode(nodeSimpleVO.type)) + .setName(nodeSimpleVO.getName()) + .setScript(value) + .build(); + } } else if (CuratorCacheListener.Type.NODE_DELETED.equals(type)) { LOG.info("starting reload flow config... delete path={}", path); @@ -203,6 +228,10 @@ public class ZkParserHelper { nodeSimpleVO.setName(matchItemList.get(2)); } + if (matchItemList.size() > 3) { + nodeSimpleVO.setLanguage(matchItemList.get(3)); + } + return nodeSimpleVO; } @@ -214,6 +243,8 @@ public class ZkParserHelper { private String name = ""; + private String language; + public String getNodeId() { return nodeId; } @@ -238,6 +269,14 @@ public class ZkParserHelper { this.name = name; } + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + } } diff --git a/liteflow-testcase-el/liteflow-testcase-el-zk-springboot/pom.xml b/liteflow-testcase-el/liteflow-testcase-el-zk-springboot/pom.xml index 18261781f..65228a046 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-zk-springboot/pom.xml +++ b/liteflow-testcase-el/liteflow-testcase-el-zk-springboot/pom.xml @@ -32,6 +32,13 @@ ${revision} + + com.yomahub + liteflow-script-javascript + ${revision} + test + + org.springframework.boot spring-boot-starter-test diff --git a/liteflow-testcase-el/liteflow-testcase-el-zk-springboot/src/test/java/com/yomahub/liteflow/test/zookeeper/ZkClusterWithXmlELSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-zk-springboot/src/test/java/com/yomahub/liteflow/test/zookeeper/ZkClusterWithXmlELSpringbootTest.java index 1944ec534..05415a7c8 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-zk-springboot/src/test/java/com/yomahub/liteflow/test/zookeeper/ZkClusterWithXmlELSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-zk-springboot/src/test/java/com/yomahub/liteflow/test/zookeeper/ZkClusterWithXmlELSpringbootTest.java @@ -1,7 +1,5 @@ package com.yomahub.liteflow.test.zookeeper; -import cn.hutool.core.io.FileUtil; -import cn.hutool.core.io.resource.ResourceUtil; import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.flow.LiteflowResponse; import com.yomahub.liteflow.slot.DefaultContext; @@ -11,7 +9,6 @@ import org.I0Itec.zkclient.exception.ZkMarshallingError; import org.I0Itec.zkclient.serialize.ZkSerializer; import org.apache.curator.test.InstanceSpec; import org.apache.curator.test.TestingCluster; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -23,8 +20,6 @@ import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; -import java.io.File; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; /** @@ -71,21 +66,42 @@ public class ZkClusterWithXmlELSpringbootTest extends BaseTest { String chain1Path = ZK_CHAIN_PATH + "/chain1"; zkClient.createPersistent(chain1Path, true); - zkClient.writeData(chain1Path, "THEN(a, b, c, s1);"); + zkClient.writeData(chain1Path, "THEN(a, b, c, s1, s2);"); - String script1Path = ZK_SCRIPT_PATH + "/s1:script:脚本s1"; + String chain2Path = ZK_CHAIN_PATH + "/chain2"; + zkClient.createPersistent(chain2Path, true); + zkClient.writeData(chain2Path, "THEN(a, b, c, s3);"); + + String script1Path = ZK_SCRIPT_PATH + "/s1:script:脚本s1:groovy"; zkClient.createPersistent(script1Path, true); zkClient.writeData(script1Path, "defaultContext.setData(\"test\",\"hello\");"); + String script2Path = ZK_SCRIPT_PATH + "/s2:script:脚本s2:js"; + zkClient.createPersistent(script2Path, true); + zkClient.writeData(script2Path, "defaultContext.setData(\"test1\",\"hello\");"); + + String script3Path = ZK_SCRIPT_PATH + "/s3:script:脚本s3"; + zkClient.createPersistent(script3Path, true); + zkClient.writeData(script3Path, "defaultContext.setData(\"test\",\"hello\");"); + Thread.sleep(2000L); } @Test - public void testZkNodeWithXml() { + public void testZkNodeWithXmlWithLanguage() { LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); DefaultContext context = response.getFirstContextBean(); Assert.assertTrue(response.isSuccess()); Assert.assertEquals("hello", context.getData("test")); + Assert.assertEquals("hello", context.getData("test1")); + } + + @Test + public void testZkNodeWithXml() { + LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg"); + DefaultContext context = response.getFirstContextBean(); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("hello", context.getData("test")); } } diff --git a/liteflow-testcase-el/liteflow-testcase-el-zk-springboot/src/test/java/com/yomahub/liteflow/test/zookeeper/ZkNodeWithXmlELSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-zk-springboot/src/test/java/com/yomahub/liteflow/test/zookeeper/ZkNodeWithXmlELSpringbootTest.java index da0bccd8f..2dc833d3c 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-zk-springboot/src/test/java/com/yomahub/liteflow/test/zookeeper/ZkNodeWithXmlELSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-zk-springboot/src/test/java/com/yomahub/liteflow/test/zookeeper/ZkNodeWithXmlELSpringbootTest.java @@ -1,8 +1,8 @@ package com.yomahub.liteflow.test.zookeeper; -import cn.hutool.core.io.resource.ResourceUtil; import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.flow.LiteflowResponse; +import com.yomahub.liteflow.slot.DefaultContext; import com.yomahub.liteflow.test.BaseTest; import org.I0Itec.zkclient.ZkClient; import org.I0Itec.zkclient.exception.ZkMarshallingError; @@ -20,8 +20,7 @@ import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; -import java.nio.charset.Charset; -import java.util.concurrent.CountDownLatch; +import java.nio.charset.StandardCharsets; /** * springboot环境下的zk配置源功能测试 ZK节点存储数据的格式为xml文件 @@ -52,27 +51,50 @@ public class ZkNodeWithXmlELSpringbootTest extends BaseTest { zkClient.setZkSerializer(new ZkSerializer() { @Override public byte[] serialize(final Object o) throws ZkMarshallingError { - return o.toString().getBytes(Charset.forName("UTF-8")); + return o.toString().getBytes(StandardCharsets.UTF_8); } @Override public Object deserialize(final byte[] bytes) throws ZkMarshallingError { - return new String(bytes, Charset.forName("UTF-8")); + return new String(bytes, StandardCharsets.UTF_8); } }); String chain1Path = ZK_CHAIN_PATH + "/chain1"; zkClient.createPersistent(chain1Path, true); - zkClient.writeData(chain1Path, "THEN(a, b, c, s1);"); + zkClient.writeData(chain1Path, "THEN(a, b, c, s1, s2);"); - String script1Path = ZK_SCRIPT_PATH + "/s1:script:脚本s1"; + String chain2Path = ZK_CHAIN_PATH + "/chain2"; + zkClient.createPersistent(chain2Path, true); + zkClient.writeData(chain2Path, "THEN(a, b, c, s3);"); + + String script1Path = ZK_SCRIPT_PATH + "/s1:script:脚本s1:groovy"; zkClient.createPersistent(script1Path, true); zkClient.writeData(script1Path, "defaultContext.setData(\"test\",\"hello\");"); + + String script2Path = ZK_SCRIPT_PATH + "/s2:script:脚本s2:js"; + zkClient.createPersistent(script2Path, true); + zkClient.writeData(script2Path, "defaultContext.setData(\"test1\",\"hello\");"); + + String script3Path = ZK_SCRIPT_PATH + "/s3:script:脚本s3"; + zkClient.createPersistent(script3Path, true); + zkClient.writeData(script3Path, "defaultContext.setData(\"test\",\"hello\");"); + } + + @Test + public void testZkNodeWithXmlWithLanguage() { + LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); + DefaultContext context = response.getFirstContextBean(); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("hello", context.getData("test")); + Assert.assertEquals("hello", context.getData("test1")); } @Test public void testZkNodeWithXml() { - LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); + LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg"); + DefaultContext context = response.getFirstContextBean(); Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("hello", context.getData("test")); } @AfterClass