mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-15 04:22:09 +08:00
feature #I7V6VB https://gitee.com/dromara/liteFlow/issues/I7V6VB
This commit is contained in:
@@ -43,6 +43,7 @@ public interface ScriptComponent {
|
||||
wrap.setCmpData(cmp.getCmpData(Map.class));
|
||||
wrap.setLoopIndex(cmp.getLoopIndex());
|
||||
wrap.setLoopObject(cmp.getCurrLoopObj());
|
||||
wrap.setCmp(cmp);
|
||||
return wrap;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.yomahub.liteflow.script;
|
||||
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
|
||||
/**
|
||||
* script执行前的包装元参数
|
||||
*
|
||||
@@ -8,19 +10,21 @@ package com.yomahub.liteflow.script;
|
||||
*/
|
||||
public class ScriptExecuteWrap {
|
||||
|
||||
private int slotIndex;
|
||||
public int slotIndex;
|
||||
|
||||
private String currChainId;
|
||||
public String currChainId;
|
||||
|
||||
private String nodeId;
|
||||
public String nodeId;
|
||||
|
||||
private String tag;
|
||||
public String tag;
|
||||
|
||||
private Object cmpData;
|
||||
public Object cmpData;
|
||||
|
||||
private Integer loopIndex;
|
||||
public Integer loopIndex;
|
||||
|
||||
private Object loopObject;
|
||||
public Object loopObject;
|
||||
|
||||
public NodeComponent cmp;
|
||||
|
||||
public int getSlotIndex() {
|
||||
return slotIndex;
|
||||
@@ -93,4 +97,12 @@ public class ScriptExecuteWrap {
|
||||
public void setLoopObject(Object loopObject) {
|
||||
this.loopObject = loopObject;
|
||||
}
|
||||
|
||||
public NodeComponent getCmp() {
|
||||
return cmp;
|
||||
}
|
||||
|
||||
public void setCmp(NodeComponent cmp) {
|
||||
this.cmp = cmp;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
type (script|if_script|switch_script|while_script|for_script|break_script) #IMPLIED
|
||||
class CDATA #IMPLIED
|
||||
file CDATA #IMPLIED
|
||||
language (qlexpress|groovy|js|python|lua|aviator) #IMPLIED
|
||||
language (qlexpress|groovy|js|python|lua|aviator|java) #IMPLIED
|
||||
>
|
||||
<!ATTLIST chain
|
||||
id CDATA #IMPLIED
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.janino</groupId>
|
||||
|
||||
@@ -1,38 +1,51 @@
|
||||
package com.yomahub.liteflow.script.java;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.enums.ScriptTypeEnum;
|
||||
import com.yomahub.liteflow.script.ScriptExecuteWrap;
|
||||
import com.yomahub.liteflow.script.ScriptExecutor;
|
||||
import com.yomahub.liteflow.script.exception.ScriptLoadException;
|
||||
import com.yomahub.liteflow.util.CopyOnWriteHashMap;
|
||||
import org.codehaus.commons.compiler.CompilerFactoryFactory;
|
||||
import org.codehaus.commons.compiler.IScriptEvaluator;
|
||||
import java.util.Map;
|
||||
|
||||
public class JavaExecutor extends ScriptExecutor {
|
||||
|
||||
private final Map<String, IScriptEvaluator> compiledScriptMap = new CopyOnWriteHashMap<>();
|
||||
|
||||
@Override
|
||||
public void load(String nodeId, String script) {
|
||||
// 创建Janino脚本Evaluator
|
||||
/*IScriptEvaluator se = CompilerFactoryFactory.getDefaultCompilerFactory().newScriptEvaluator();
|
||||
// 返回值类型指定为Object以支持不同脚本
|
||||
se.setReturnType(Object.class);
|
||||
// 指定Janino脚本里的变量名及类型,为通用起见,只设置一个Object类型的变量
|
||||
se.setParameters(new String[] { JANINO_SCRIPT_PARAMETER_NAME }, new Class[] { Object.class });
|
||||
// 编译
|
||||
se.cook(script);
|
||||
// 缓存编译过的Evaluator
|
||||
compiledScriptMap.put(nodeId, se);*/
|
||||
try{
|
||||
IScriptEvaluator se = CompilerFactoryFactory.getDefaultCompilerFactory(this.getClass().getClassLoader()).newScriptEvaluator();
|
||||
se.setReturnType(Object.class);
|
||||
se.setParameters(new String[] {"_meta"}, new Class[] {ScriptExecuteWrap.class});
|
||||
se.cook(script);
|
||||
compiledScriptMap.put(nodeId, se);
|
||||
}catch (Exception e){
|
||||
String errorMsg = StrUtil.format("script loading error for node[{}],error msg:{}", nodeId, e.getMessage());
|
||||
throw new ScriptLoadException(errorMsg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object executeScript(ScriptExecuteWrap wrap) throws Exception {
|
||||
return null;
|
||||
if (!compiledScriptMap.containsKey(wrap.getNodeId())) {
|
||||
String errorMsg = StrUtil.format("script for node[{}] is not loaded", wrap.getNodeId());
|
||||
throw new ScriptLoadException(errorMsg);
|
||||
}
|
||||
IScriptEvaluator se = compiledScriptMap.get(wrap.getNodeId());
|
||||
return se.evaluate(wrap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache() {
|
||||
|
||||
compiledScriptMap.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptTypeEnum scriptType() {
|
||||
return null;
|
||||
return ScriptTypeEnum.JAVA;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# Java的实现
|
||||
com.yomahub.liteflow.script.java.JavaExecutor
|
||||
@@ -0,0 +1,33 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>liteflow-testcase-el</artifactId>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>liteflow-testcase-el-script-java-springboot</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-spring-boot-starter</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-script-java</artifactId>
|
||||
<version>${revision}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.yomahub.liteflow.test;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowInitHook;
|
||||
import com.yomahub.liteflow.flow.FlowBus;
|
||||
import com.yomahub.liteflow.property.LiteflowConfigGetter;
|
||||
import com.yomahub.liteflow.spi.holder.SpiFactoryCleaner;
|
||||
import com.yomahub.liteflow.spring.ComponentScanner;
|
||||
import com.yomahub.liteflow.thread.ExecutorHelper;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
|
||||
public class BaseTest {
|
||||
|
||||
@AfterAll
|
||||
public static void cleanScanCache() {
|
||||
ComponentScanner.cleanCache();
|
||||
FlowBus.cleanCache();
|
||||
ExecutorHelper.loadInstance().clearExecutorServiceMap();
|
||||
SpiFactoryCleaner.clean();
|
||||
LiteflowConfigGetter.clean();
|
||||
FlowInitHook.cleanHook();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.yomahub.liteflow.test.script.java.common;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
import com.yomahub.liteflow.slot.DefaultContext;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
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.junit.jupiter.SpringExtension;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@TestPropertySource(value = "classpath:/common/application.properties")
|
||||
@SpringBootTest(classes = ScriptJavaCommonELTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({ "com.yomahub.liteflow.test.script.java.common.cmp" })
|
||||
public class ScriptJavaCommonELTest {
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
// 测试普通脚本节点
|
||||
@Test
|
||||
public void testCommon1() {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
|
||||
DefaultContext context = response.getFirstContextBean();
|
||||
Assertions.assertTrue(response.isSuccess());
|
||||
Assertions.assertEquals(6, (int)context.getData("s1"));
|
||||
Assertions.assertEquals("hello,jack", context.getData("hi"));
|
||||
}
|
||||
}
|
||||
@@ -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.script.java.common.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
|
||||
@LiteflowComponent("a")
|
||||
public class ACmp extends NodeComponent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
System.out.println("ACmp executed!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.script.java.common.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
|
||||
@LiteflowComponent("b")
|
||||
public class BCmp extends NodeComponent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
System.out.println("BCmp executed!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.script.java.common.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
|
||||
@LiteflowComponent("c")
|
||||
public class CCmp extends NodeComponent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
System.out.println("CCmp executed!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* <p>Title: liteflow</p>
|
||||
* <p>Description: 轻量级的组件式流程框架</p>
|
||||
* @author Bryan.Zhang
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.java.common.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import com.yomahub.liteflow.slot.DefaultContext;
|
||||
|
||||
@LiteflowComponent("d")
|
||||
public class DCmp extends NodeComponent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
DefaultContext context = this.getFirstContextBean();
|
||||
context.setData("count", 198);
|
||||
System.out.println("DCmp executed!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.yomahub.liteflow.test.script.java.common.cmp;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class TestDomain {
|
||||
|
||||
public String sayHello(String name){
|
||||
return "hello," + name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
liteflow.rule-source=common/flow.xml
|
||||
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE flow PUBLIC "liteflow" "liteflow.dtd">
|
||||
<flow>
|
||||
<nodes>
|
||||
<node id="s1" name="普通脚本1" type="script" language="java">
|
||||
<![CDATA[
|
||||
import com.yomahub.liteflow.slot.DefaultContext;
|
||||
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
||||
import com.yomahub.liteflow.test.script.java.common.cmp.TestDomain;
|
||||
|
||||
int v1 = 2;
|
||||
int v2 = 3;
|
||||
System.out.println(_meta.cmp);
|
||||
DefaultContext ctx = (DefaultContext) _meta.cmp.getFirstContextBean();
|
||||
ctx.setData("s1", v1 * v2);
|
||||
|
||||
TestDomain domain = (TestDomain)ContextAwareHolder.loadContextAware().getBean(TestDomain.class);
|
||||
|
||||
String str = domain.sayHello("jack");
|
||||
ctx.setData("hi", str);
|
||||
return null;
|
||||
]]>
|
||||
</node>
|
||||
</nodes>
|
||||
|
||||
<chain name="chain1">
|
||||
THEN(a, b, c, s1);
|
||||
</chain>
|
||||
</flow>
|
||||
@@ -36,6 +36,7 @@
|
||||
<module>liteflow-testcase-el-script-multi-language-springboot</module>
|
||||
<module>liteflow-testcase-el-script-aviator-springboot</module>
|
||||
<module>liteflow-testcase-el-sql-springboot-dynamic</module>
|
||||
<module>liteflow-testcase-el-script-java-springboot</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
||||
Reference in New Issue
Block a user