mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 20:22:07 +08:00
feature #I44FT8 支持脚本语言的组件,并支持动态刷新脚本(版本特性)
This commit is contained in:
@@ -13,6 +13,12 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-script-common</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.yomahub.liteflow.core;
|
||||
|
||||
import com.yomahub.liteflow.script.ScriptFactory;
|
||||
|
||||
/**
|
||||
* 脚本组件类
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.5.11
|
||||
*/
|
||||
public class ScriptNodeComponent extends NodeComponent{
|
||||
|
||||
@Override
|
||||
public void process() throws Exception {
|
||||
ScriptFactory.loadInstance().getScriptExecutor().execute(getNodeId(), getSlotIndex());
|
||||
}
|
||||
|
||||
public void loadScript(String script) {
|
||||
ScriptFactory.loadInstance().getScriptExecutor().load(getNodeId(), script);
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import java.util.Map;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import com.yomahub.liteflow.core.ScriptNodeComponent;
|
||||
import com.yomahub.liteflow.entity.data.DataBus;
|
||||
import com.yomahub.liteflow.entity.data.Slot;
|
||||
import com.yomahub.liteflow.enums.ExecuteTypeEnum;
|
||||
|
||||
@@ -12,6 +12,7 @@ import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import com.yomahub.liteflow.core.ScriptNodeComponent;
|
||||
import com.yomahub.liteflow.entity.flow.Chain;
|
||||
import com.yomahub.liteflow.entity.flow.Node;
|
||||
import com.yomahub.liteflow.enums.FlowParserTypeEnum;
|
||||
@@ -23,6 +24,7 @@ import com.yomahub.liteflow.util.SpringAware;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -72,14 +74,19 @@ public class FlowBus {
|
||||
public static void addNode(String nodeId, String name, String cmpClazzStr) throws Exception {
|
||||
if (containNode(nodeId)) return;
|
||||
Class<NodeComponent> cmpClazz = (Class<NodeComponent>) Class.forName(cmpClazzStr);
|
||||
addNode(nodeId, name ,cmpClazz);
|
||||
addNode(nodeId, name ,cmpClazz, null);
|
||||
}
|
||||
|
||||
public static void addScriptNode(String nodeId, String name, String script){
|
||||
if (containNode(nodeId)) return;
|
||||
addNode(nodeId, name , ScriptNodeComponent.class, script);
|
||||
}
|
||||
|
||||
public static void addNode(String nodeId, Class<? extends NodeComponent> cmpClazz){
|
||||
addNode(nodeId, null, cmpClazz);
|
||||
addNode(nodeId, null, cmpClazz, null);
|
||||
}
|
||||
|
||||
public static void addNode(String nodeId, String name, Class<? extends NodeComponent> cmpClazz) {
|
||||
public static void addNode(String nodeId, String name, Class<? extends NodeComponent> cmpClazz, String script) {
|
||||
if (containNode(nodeId)) return;
|
||||
try {
|
||||
//以node方式配置,本质上是为了适配无spring的环境,如果有spring环境,其实不用这么配置
|
||||
@@ -92,6 +99,11 @@ public class FlowBus {
|
||||
cmpInstance.setNodeId(nodeId);
|
||||
cmpInstance.setName(name);
|
||||
cmpInstance.setSelf(cmpInstance);
|
||||
|
||||
//如果是脚本节点,则还要加载script脚本
|
||||
if (cmpClazz.equals(ScriptNodeComponent.class) && StrUtil.isNotBlank(script)){
|
||||
((ScriptNodeComponent)cmpInstance).loadScript(script);
|
||||
}
|
||||
nodeMap.put(nodeId, new Node(cmpInstance));
|
||||
} catch (Exception e) {
|
||||
String error = StrUtil.format("component[{}] register error", cmpClazz.getName());
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.yomahub.liteflow.parser;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.common.LocalDefaultFlowConstant;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -52,23 +53,34 @@ public abstract class XmlFlowParser extends FlowParser {
|
||||
//xml形式的主要解析过程
|
||||
public void parseDocument(List<Document> documentList) throws Exception {
|
||||
try {
|
||||
//先进行Spring上下文中的节点的判断
|
||||
for (Entry<String, NodeComponent> componentEntry : ComponentScanner.nodeComponentMap.entrySet()) {
|
||||
if (!FlowBus.containNode(componentEntry.getKey())) {
|
||||
FlowBus.addNode(componentEntry.getKey(), new Node(componentEntry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
for (Document document : documentList) {
|
||||
Element rootElement = document.getRootElement();
|
||||
//判断是以spring方式注册节点,还是以xml方式注册
|
||||
if (ComponentScanner.nodeComponentMap.isEmpty()) {
|
||||
// 解析node节点
|
||||
List<Element> nodeList = rootElement.element("nodes").elements("node");
|
||||
String id, name, clazz;
|
||||
Element nodesElement = rootElement.element("nodes");
|
||||
// 当存在<nodes>节点定义时,解析node节点
|
||||
if (ObjectUtil.isNotNull(nodesElement)){
|
||||
List<Element> nodeList = nodesElement.elements("node");
|
||||
String id, name, clazz, script;
|
||||
for (Element e : nodeList) {
|
||||
id = e.attributeValue("id");
|
||||
name = e.attributeValue("name");
|
||||
clazz = e.attributeValue("class");
|
||||
FlowBus.addNode(id, name, clazz);
|
||||
}
|
||||
} else {
|
||||
for (Entry<String, NodeComponent> componentEntry : ComponentScanner.nodeComponentMap.entrySet()) {
|
||||
if (!FlowBus.containNode(componentEntry.getKey())) {
|
||||
FlowBus.addNode(componentEntry.getKey(), new Node(componentEntry.getValue()));
|
||||
|
||||
if (StrUtil.isNotBlank(clazz)){
|
||||
if (!FlowBus.containNode(id)){
|
||||
FlowBus.addNode(id, name, clazz);
|
||||
}
|
||||
}else{
|
||||
if (!FlowBus.containNode(id)){
|
||||
script = e.getTextTrim();
|
||||
FlowBus.addScriptNode(id, name, script);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
26
liteflow-script-common/pom.xml
Normal file
26
liteflow-script-common/pom.xml
Normal file
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>liteflow</artifactId>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<version>2.5.11</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>liteflow-script-common</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.yomahub.liteflow.script;
|
||||
|
||||
/**
|
||||
* 脚本执行器
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.5.11
|
||||
*/
|
||||
public interface ScriptExecutor {
|
||||
|
||||
ScriptExecutor init();
|
||||
|
||||
void load(String nodeId, String script);
|
||||
|
||||
void execute(String nodeId, int slotIndex);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.yomahub.liteflow.script;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
public class ScriptFactory {
|
||||
|
||||
private static ScriptFactory scriptFactory;
|
||||
|
||||
private static ScriptExecutor scriptExecutor;
|
||||
|
||||
public static ScriptFactory loadInstance(){
|
||||
if (ObjectUtil.isNull(scriptFactory)){
|
||||
scriptFactory = new ScriptFactory();
|
||||
}
|
||||
return scriptFactory;
|
||||
}
|
||||
|
||||
public ScriptExecutor getScriptExecutor(){
|
||||
if (ObjectUtil.isNull(scriptExecutor)){
|
||||
ServiceLoader<ScriptExecutor> loader = ServiceLoader.load(ScriptExecutor.class);
|
||||
if (loader.iterator().hasNext()){
|
||||
scriptExecutor = loader.iterator().next().init();
|
||||
return scriptExecutor;
|
||||
}
|
||||
}
|
||||
return scriptExecutor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
package com.yomahub.liteflow.script.exception;
|
||||
|
||||
/**
|
||||
* 脚本运行异常
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.5.11
|
||||
*/
|
||||
public class ScriptExecuteException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 异常信息 */
|
||||
private String message;
|
||||
|
||||
public ScriptExecuteException(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
package com.yomahub.liteflow.script.exception;
|
||||
|
||||
/**
|
||||
* 脚本加载异常
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.5.11
|
||||
*/
|
||||
public class ScriptLoadException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 异常信息 */
|
||||
private String message;
|
||||
|
||||
public ScriptLoadException(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
31
liteflow-script-qlexpress/pom.xml
Normal file
31
liteflow-script-qlexpress/pom.xml
Normal file
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>liteflow</artifactId>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<version>2.5.11</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>liteflow-script-qlexpress</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-script-common</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>QLExpress</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.yomahub.liteflow.script.qlexpress;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.ql.util.express.DefaultContext;
|
||||
import com.ql.util.express.ExpressRunner;
|
||||
import com.yomahub.liteflow.entity.data.DataBus;
|
||||
import com.yomahub.liteflow.entity.data.Slot;
|
||||
import com.yomahub.liteflow.script.ScriptExecutor;
|
||||
import com.yomahub.liteflow.script.exception.ScriptExecuteException;
|
||||
import com.yomahub.liteflow.script.exception.ScriptLoadException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class QLExpressScriptExecutor implements ScriptExecutor {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
private ExpressRunner expressRunner;
|
||||
|
||||
@Override
|
||||
public ScriptExecutor init() {
|
||||
expressRunner = new ExpressRunner();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(String nodeId, String script) {
|
||||
try{
|
||||
expressRunner.loadMutilExpress(nodeId, script);
|
||||
}catch (Exception e){
|
||||
String errorMsg = StrUtil.format("script loading error for node[{}]", nodeId);
|
||||
throw new ScriptLoadException(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String nodeId, int slotIndex) {
|
||||
List<String> errorList = new ArrayList<>();
|
||||
try{
|
||||
Slot slot = DataBus.getSlot(slotIndex);
|
||||
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
|
||||
context.put("slot", slot);
|
||||
expressRunner.executeByExpressName(nodeId, context, errorList, true, false, null);
|
||||
}catch (Exception e){
|
||||
for (String scriptErrorMsg : errorList){
|
||||
log.error("\n{}", scriptErrorMsg);
|
||||
}
|
||||
String errorMsg = StrUtil.format("script execute error for node[{}]", nodeId);
|
||||
throw new ScriptExecuteException(errorMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
15
pom.xml
15
pom.xml
@@ -56,6 +56,8 @@
|
||||
<transmittable-thread-local.version>2.12.1</transmittable-thread-local.version>
|
||||
<curator-test.version>5.1.0</curator-test.version>
|
||||
<zkclient.version>0.10</zkclient.version>
|
||||
<qlexpress.version>3.2.0</qlexpress.version>
|
||||
<groovy.version>3.0.8</groovy.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
@@ -150,6 +152,17 @@
|
||||
<artifactId>zkclient</artifactId>
|
||||
<version>${zkclient.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>QLExpress</artifactId>
|
||||
<version>${qlexpress.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
@@ -229,6 +242,8 @@
|
||||
<modules>
|
||||
<module>liteflow-core</module>
|
||||
<module>liteflow-spring-boot-starter</module>
|
||||
<module>liteflow-script-qlexpress</module>
|
||||
<module>liteflow-script-common</module>
|
||||
</modules>
|
||||
|
||||
<distributionManagement>
|
||||
|
||||
Reference in New Issue
Block a user