feat #I6O2YE zk 外置存储支持多脚本混合调用

This commit is contained in:
gaibu
2023-03-20 22:18:21 +08:00
parent ec50e3a0af
commit 775f5ec791
4 changed files with 44 additions and 16 deletions

View File

@@ -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;
@@ -35,7 +36,7 @@ public class ZkParserHelper {
private final String NODE_XML_PATTERN = "<nodes>{}</nodes>";
private final String NODE_ITEM_XML_PATTERN = "<node id=\"{}\" name=\"{}\" type=\"{}\"><![CDATA[{}]]></node>";
private final String NODE_ITEM_XML_PATTERN = "<node id=\"{}\" name=\"{}\" type=\"{}\" language=\"{}\"><![CDATA[{}]]></node>";
private final String XML_PATTERN = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><flow>{}{}</flow>";
@@ -92,7 +93,7 @@ public class ZkParserHelper {
.forPath(StrUtil.format("{}/{}", zkParserVO.getScriptPath(), scriptNodeValue)));
scriptItemContentList.add(StrUtil.format(NODE_ITEM_XML_PATTERN, nodeSimpleVO.getNodeId(),
nodeSimpleVO.getName(), nodeSimpleVO.getType(), scriptData));
nodeSimpleVO.getName(), nodeSimpleVO.getType(), nodeSimpleVO.getLanguage(), scriptData));
}
scriptAllContent = StrUtil.format(NODE_XML_PATTERN,
@@ -203,6 +204,10 @@ public class ZkParserHelper {
nodeSimpleVO.setName(matchItemList.get(2));
}
if (matchItemList.size() > 3) {
nodeSimpleVO.setLanguage(matchItemList.get(3));
}
return nodeSimpleVO;
}
@@ -214,6 +219,8 @@ public class ZkParserHelper {
private String name = "";
private String language;
public String getNodeId() {
return nodeId;
}
@@ -238,6 +245,14 @@ public class ZkParserHelper {
this.name = name;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
}

View File

@@ -32,6 +32,13 @@
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-script-javascript</artifactId>
<version>${revision}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@@ -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,12 +66,16 @@ 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 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\");");
Thread.sleep(2000L);
}
@@ -86,6 +85,7 @@ public class ZkClusterWithXmlELSpringbootTest extends BaseTest {
DefaultContext context = response.getFirstContextBean();
Assert.assertTrue(response.isSuccess());
Assert.assertEquals("hello", context.getData("test"));
Assert.assertEquals("hello", context.getData("test1"));
}
}

View File

@@ -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,34 @@ 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 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\");");
}
@Test
public void testZkNodeWithXml() {
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"));
}
@AfterClass