mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-06-10 03:07:32 +08:00
enhancement 新增graaljs脚本插件
This commit is contained in:
27
liteflow-script-plugin/liteflow-script-graaljs/pom.xml
Normal file
27
liteflow-script-plugin/liteflow-script-graaljs/pom.xml
Normal file
@@ -0,0 +1,27 @@
|
||||
<?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-script-plugin</artifactId>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>liteflow-script-graaljs</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.graalvm.js</groupId>
|
||||
<artifactId>js</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.yomahub.liteflow.script.graaljs;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.script.ScriptBeanManager;
|
||||
import com.yomahub.liteflow.script.ScriptExecuteWrap;
|
||||
import com.yomahub.liteflow.script.ScriptExecutor;
|
||||
import com.yomahub.liteflow.script.exception.ScriptLoadException;
|
||||
import com.yomahub.liteflow.slot.DataBus;
|
||||
import com.yomahub.liteflow.slot.Slot;
|
||||
import com.yomahub.liteflow.util.CopyOnWriteHashMap;
|
||||
import org.graalvm.polyglot.Context;
|
||||
import org.graalvm.polyglot.Value;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* GraalVM JavaScript脚本语言的执行器实现
|
||||
* @author zendwang
|
||||
* @since 2.9.4
|
||||
*/
|
||||
public class GraalJavaScriptExecutor implements ScriptExecutor {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
private final Map<String, String> scriptMap = new CopyOnWriteHashMap<>();
|
||||
|
||||
@Override
|
||||
public ScriptExecutor init() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(String nodeId, String script) {
|
||||
try{
|
||||
String wrapScript = StrUtil.format("function process(){{}} process();",script);
|
||||
scriptMap.put(nodeId, wrapScript);
|
||||
}catch (Exception e){
|
||||
String errorMsg = StrUtil.format("script loading error for node[{}], error msg:{}", nodeId, e.getMessage());
|
||||
throw new ScriptLoadException(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object execute(ScriptExecuteWrap wrap) throws Exception{
|
||||
try{
|
||||
if (!scriptMap.containsKey(wrap.getNodeId())){
|
||||
String errorMsg = StrUtil.format("script for node[{}] is not loaded", wrap.getNodeId());
|
||||
throw new ScriptLoadException(errorMsg);
|
||||
}
|
||||
|
||||
Context context = Context.newBuilder().allowAllAccess(true).build();
|
||||
Value bindings = context.getBindings("js");
|
||||
//往脚本语言绑定表里循环增加绑定上下文的key
|
||||
//key的规则为自定义上下文的simpleName
|
||||
//比如你的自定义上下文为AbcContext,那么key就为:abcContext
|
||||
//这里不统一放一个map的原因是考虑到有些用户会调用上下文里的方法,而不是参数,所以脚本语言的绑定表里也是放多个上下文
|
||||
DataBus.getContextBeanList(wrap.getSlotIndex()).forEach(o -> {
|
||||
String key = StrUtil.lowerFirst(o.getClass().getSimpleName());
|
||||
bindings.putMember(key, o);
|
||||
});
|
||||
|
||||
//把wrap对象转换成元数据map
|
||||
Map<String, Object> metaMap = BeanUtil.beanToMap(wrap);
|
||||
|
||||
//在元数据里放入主Chain的流程参数
|
||||
Slot slot = DataBus.getSlot(wrap.getSlotIndex());
|
||||
metaMap.put("requestData", slot.getRequestData());
|
||||
|
||||
//如果有隐式流程,则放入隐式流程的流程参数
|
||||
Object subRequestData = slot.getChainReqData(wrap.getCurrChainName());
|
||||
if (ObjectUtil.isNotNull(subRequestData)){
|
||||
metaMap.put("subRequestData", subRequestData);
|
||||
}
|
||||
|
||||
//往脚本上下文里放入元数据
|
||||
bindings.putMember("_meta", metaMap);
|
||||
|
||||
//放入用户自己定义的bean
|
||||
ScriptBeanManager.getScriptBeanMap().entrySet().stream().forEach( e ->{
|
||||
bindings.putMember(e.getKey(), e.getValue());
|
||||
});
|
||||
Value value = context.eval("js", scriptMap.get(wrap.getNodeId()));
|
||||
if (value.isBoolean()) {
|
||||
return value.asBoolean();
|
||||
} else if (value.isNumber()) {
|
||||
return value.asInt();
|
||||
} else if (value.isString()) {
|
||||
return value.asString();
|
||||
}
|
||||
return value;
|
||||
}catch (Exception e){
|
||||
log.error(e.getMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache() {
|
||||
scriptMap.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
# JavaScript的实现
|
||||
com.yomahub.liteflow.script.graaljs.GraalJavaScriptExecutor
|
||||
@@ -1,2 +1,2 @@
|
||||
# JavaScript的实现
|
||||
com.yomahub.liteflow.script.javascript.JavaScriptExecutor
|
||||
com.yomahub.liteflow.script.graaljs.GraalJavaScriptExecutor
|
||||
@@ -18,6 +18,7 @@
|
||||
<module>liteflow-script-qlexpress</module>
|
||||
<module>liteflow-script-groovy</module>
|
||||
<module>liteflow-script-javascript</module>
|
||||
<module>liteflow-script-graaljs</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
@@ -10,7 +10,7 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>liteflow-testcase-el-script-graalvm-springboot</artifactId>
|
||||
<artifactId>liteflow-testcase-el-script-graaljs-springboot</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -20,7 +20,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-script-javascript</artifactId>
|
||||
<artifactId>liteflow-script-graaljs</artifactId>
|
||||
<version>${revision}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
@@ -29,16 +29,7 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.graalvm.js</groupId>
|
||||
<artifactId>js-scriptengine</artifactId>
|
||||
<version>20.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.graalvm.js</groupId>
|
||||
<artifactId>js</artifactId>
|
||||
<version>20.2.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.yomahub.liteflow.test.script.javascript.common;
|
||||
package com.yomahub.liteflow.test.script.graaljs.common;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
@@ -17,16 +17,16 @@ import javax.annotation.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* 测试springboot下的groovy脚本组件,基于xml配置
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.0
|
||||
* 测试springboot下的graaljs脚本组件,基于xml配置
|
||||
* @author zendwang
|
||||
* @since 2.9.4
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource(value = "classpath:/common/application.properties")
|
||||
@SpringBootTest(classes = LiteflowXmlScriptJsCommonELTest.class)
|
||||
@SpringBootTest(classes = LiteflowXmlScriptCommonELTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.javascript.common.cmp"})
|
||||
public class LiteflowXmlScriptJsCommonELTest extends BaseTest {
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.graaljs.common.cmp"})
|
||||
public class LiteflowXmlScriptCommonELTest extends BaseTest {
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
@@ -37,6 +37,6 @@ public class LiteflowXmlScriptJsCommonELTest extends BaseTest {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
|
||||
DefaultContext context = response.getFirstContextBean();
|
||||
Assert.assertTrue(response.isSuccess());
|
||||
Assert.assertEquals(Double.valueOf(11), context.getData("s1"));
|
||||
Assert.assertEquals(Integer.valueOf(11), context.getData("s1"));
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.graalvm.common.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.common.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.graalvm.common.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.common.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.graalvm.common.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.common.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.yomahub.liteflow.test.script.javascript.ifelse;
|
||||
package com.yomahub.liteflow.test.script.graaljs.ifelse;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
@@ -18,7 +18,7 @@ import javax.annotation.Resource;
|
||||
@TestPropertySource(value = "classpath:/ifelse/application.properties")
|
||||
@SpringBootTest(classes = LiteFlowXmlScriptIfelseJsELTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.javascript.ifelse.cmp"})
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.graaljs.ifelse.cmp"})
|
||||
public class LiteFlowXmlScriptIfelseJsELTest extends BaseTest {
|
||||
|
||||
@Resource
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.ifelse.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.ifelse.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.common.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.ifelse.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.common.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.ifelse.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.graalvm.common.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.ifelse.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.yomahub.liteflow.test.script.javascript.loop;
|
||||
package com.yomahub.liteflow.test.script.graaljs.loop;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
@@ -18,7 +18,7 @@ import javax.annotation.Resource;
|
||||
@TestPropertySource(value = "classpath:/loop/application.properties")
|
||||
@SpringBootTest(classes = LiteFlowXmlScriptLoopJsELTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.javascript.loop.cmp"})
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.graaljs.loop.cmp"})
|
||||
public class LiteFlowXmlScriptLoopJsELTest extends BaseTest {
|
||||
|
||||
@Resource
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.loop.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.loop.cmp;
|
||||
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.loop.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.loop.cmp;
|
||||
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.loop.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.loop.cmp;
|
||||
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.loop.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.loop.cmp;
|
||||
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import com.yomahub.liteflow.slot.DefaultContext;
|
||||
@@ -1,11 +1,10 @@
|
||||
package com.yomahub.liteflow.test.script.javascript.refresh;
|
||||
package com.yomahub.liteflow.test.script.graaljs.refresh;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.enums.FlowParserTypeEnum;
|
||||
import com.yomahub.liteflow.flow.FlowBus;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
import com.yomahub.liteflow.slot.DefaultContext;
|
||||
import com.yomahub.liteflow.test.BaseTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@@ -20,15 +19,15 @@ import javax.annotation.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* 测试springboot下的groovy脚本组件,基于xml配置
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.0
|
||||
* 测试springboot下的graaljs脚本组件,基于xml配置
|
||||
* @author zendwang
|
||||
* @since 2.9.4
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource(value = "classpath:/refresh/application.properties")
|
||||
@SpringBootTest(classes = LiteflowXmlScriptJsRefreshELTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.javascript.refresh.cmp"})
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.graaljs.refresh.cmp"})
|
||||
public class LiteflowXmlScriptJsRefreshELTest extends BaseTest {
|
||||
|
||||
@Resource
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.common.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.refresh.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.ifelse.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.refresh.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.ifelse.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.refresh.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.common.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.refresh.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.yomahub.liteflow.test.script.javascript.scriptbean;
|
||||
package com.yomahub.liteflow.test.script.graaljs.scriptbean;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
@@ -19,7 +19,7 @@ import javax.annotation.Resource;
|
||||
@TestPropertySource(value = "classpath:/scriptbean/application.properties")
|
||||
@SpringBootTest(classes = LiteFlowScriptScriptbeanJsELTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.javascript.scriptbean.cmp","com.yomahub.liteflow.test.script.javascript.scriptbean.bean"})
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.graaljs.scriptbean.cmp", "com.yomahub.liteflow.test.script.graaljs.scriptbean.bean"})
|
||||
public class LiteFlowScriptScriptbeanJsELTest extends BaseTest {
|
||||
|
||||
@Resource
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.yomahub.liteflow.test.script.javascript.scriptbean.bean;
|
||||
package com.yomahub.liteflow.test.script.graaljs.scriptbean.bean;
|
||||
|
||||
import com.yomahub.liteflow.script.ScriptBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.yomahub.liteflow.test.script.javascript.scriptbean.bean;
|
||||
package com.yomahub.liteflow.test.script.graaljs.scriptbean.bean;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.scriptbean.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.scriptbean.cmp;
|
||||
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.scriptbean.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.scriptbean.cmp;
|
||||
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.scriptbean.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.scriptbean.cmp;
|
||||
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.yomahub.liteflow.test.script.javascript.sw;
|
||||
package com.yomahub.liteflow.test.script.graaljs.sw;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
@@ -16,15 +16,15 @@ import javax.annotation.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* 测试springboot下的groovy脚本组件,基于xml配置
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.0
|
||||
* 测试springboot下的graaljs脚本组件,基于xml配置
|
||||
* @author zendwang
|
||||
* @since 2.9.4
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource(value = "classpath:/sw/application.properties")
|
||||
@SpringBootTest(classes = LiteflowXmlScriptJsSwitchELTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.javascript.sw.cmp"})
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.graaljs.sw.cmp"})
|
||||
public class LiteflowXmlScriptJsSwitchELTest extends BaseTest {
|
||||
|
||||
@Resource
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.sw.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.sw.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.sw.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.sw.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.sw.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.sw.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -5,7 +5,7 @@
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.sw.cmp;
|
||||
package com.yomahub.liteflow.test.script.graaljs.sw.cmp;
|
||||
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -1,75 +0,0 @@
|
||||
package com.yomahub.liteflow.test.script.graalvm.common;
|
||||
|
||||
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.graalvm.polyglot.Context;
|
||||
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.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
|
||||
/**
|
||||
* 测试springboot下的groovy脚本组件,基于xml配置
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.0
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource(value = "classpath:/common/application.properties")
|
||||
@SpringBootTest(classes = LiteflowXmlScriptJsCommonELTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.script.graalvm.common.cmp"})
|
||||
public class LiteflowXmlScriptJsCommonELTest extends BaseTest {
|
||||
|
||||
// @Resource
|
||||
// private FlowExecutor flowExecutor;
|
||||
//
|
||||
// //测试普通脚本节点
|
||||
// @Test
|
||||
// public void testCommon1() {
|
||||
// LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
|
||||
// DefaultContext context = response.getFirstContextBean();
|
||||
// Assert.assertTrue(response.isSuccess());
|
||||
// Assert.assertEquals(Double.valueOf(11), context.getData("s1"));
|
||||
// }
|
||||
|
||||
/**
|
||||
* polyglot 模式
|
||||
*/
|
||||
@Test
|
||||
public void method1() {
|
||||
System.out.println("Hello Java!");
|
||||
try (Context context = Context.newBuilder().allowAllAccess(true).build()) {
|
||||
context.eval("js", "print('Hello JavaScript!');");
|
||||
context.eval("js", "let user = {name:\"dalong\",age:333}; print(JSON.stringify(user))");
|
||||
java.math.BigDecimal v = context.eval("js",
|
||||
"var BigDecimal = Java.type('java.math.BigDecimal');" +
|
||||
"BigDecimal.valueOf(10).pow(20)")
|
||||
.asHostObject();
|
||||
System.out.println(v.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 一种是基于ScriptEngineManager模式
|
||||
* @throws ScriptException
|
||||
* @throws NoSuchMethodException
|
||||
*/
|
||||
@Test
|
||||
public void method2() throws ScriptException, NoSuchMethodException {
|
||||
// 注意此处可以直接使用js,因为js-scriptengine 的spi 注册的时候会自动处理了内置的nashorn
|
||||
ScriptEngine eng = new ScriptEngineManager().getEngineByName("js");
|
||||
eng.eval("let user = {name:\"dalong\",age:333}; print(JSON.stringify(user))");
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/**
|
||||
* <p>Title: liteflow</p>
|
||||
* <p>Description: 轻量级的组件式流程框架</p>
|
||||
* @author Bryan.Zhang
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.ifelse.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!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
/**
|
||||
* <p>Title: liteflow</p>
|
||||
* <p>Description: 轻量级的组件式流程框架</p>
|
||||
* @author Bryan.Zhang
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.refresh.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!");
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
/**
|
||||
* <p>Title: liteflow</p>
|
||||
* <p>Description: 轻量级的组件式流程框架</p>
|
||||
* @author Bryan.Zhang
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.refresh.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!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
/**
|
||||
* <p>Title: liteflow</p>
|
||||
* <p>Description: 轻量级的组件式流程框架</p>
|
||||
* @author Bryan.Zhang
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.refresh.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!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/**
|
||||
* <p>Title: liteflow</p>
|
||||
* <p>Description: 轻量级的组件式流程框架</p>
|
||||
* @author Bryan.Zhang
|
||||
* @email weenyc31@163.com
|
||||
* @Date 2020/4/1
|
||||
*/
|
||||
package com.yomahub.liteflow.test.script.javascript.refresh.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!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,7 +23,7 @@
|
||||
<module>liteflow-testcase-el-script-groovy-springboot</module>
|
||||
<module>liteflow-testcase-el-script-qlexpress-springboot</module>
|
||||
<module>liteflow-testcase-el-script-javascript-springboot</module>
|
||||
<module>liteflow-testcase-el-script-graalvm-springboot</module>
|
||||
<module>liteflow-testcase-el-script-graaljs-springboot</module>
|
||||
<module>liteflow-testcase-el-zk-springboot</module>
|
||||
<module>liteflow-testcase-el-sql-springboot</module>
|
||||
<module>liteflow-testcase-el-nacos-springboot</module>
|
||||
|
||||
6
pom.xml
6
pom.xml
@@ -60,6 +60,7 @@
|
||||
<nacos.version>1.4.4</nacos.version>
|
||||
<qlexpress.version>3.3.0</qlexpress.version>
|
||||
<groovy.version>3.0.8</groovy.version>
|
||||
<graalvm.version>20.2.0</graalvm.version>
|
||||
<bytebuddy.version>1.11.13</bytebuddy.version>
|
||||
<aspectjweaver.version>1.8.13</aspectjweaver.version>
|
||||
<logback-classic.version>1.2.3</logback-classic.version>
|
||||
@@ -213,6 +214,11 @@
|
||||
<artifactId>byte-buddy</artifactId>
|
||||
<version>${bytebuddy.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.graalvm.js</groupId>
|
||||
<artifactId>js</artifactId>
|
||||
<version>${graalvm.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-beanutils</groupId>
|
||||
<artifactId>commons-beanutils</artifactId>
|
||||
|
||||
Reference in New Issue
Block a user