mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
feat: use Groovy Engine replace Nashorn Engine.
This commit is contained in:
parent
88b92e2994
commit
7539faffff
@ -267,6 +267,19 @@
|
||||
<version>${swagger.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-jsr223 -->
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-jsr223</artifactId>
|
||||
<version>3.0.10</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-json -->
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-json</artifactId>
|
||||
<version>3.0.10</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -14,9 +14,9 @@ import javax.script.ScriptEngineManager;
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class JavaScriptEvaluator implements Evaluator {
|
||||
public class GroovyEvaluator implements Evaluator {
|
||||
|
||||
private static final ScriptEngine ENGINE = new ScriptEngineManager().getEngineByName("nashorn");
|
||||
private static final ScriptEngine ENGINE = new ScriptEngineManager().getEngineByName("groovy");
|
||||
|
||||
|
||||
@Override
|
@ -10,7 +10,7 @@ import tech.powerjob.common.enums.WorkflowNodeType;
|
||||
import tech.powerjob.common.exception.PowerJobException;
|
||||
import tech.powerjob.common.model.PEWorkflowDAG;
|
||||
import tech.powerjob.common.serialize.JsonUtils;
|
||||
import tech.powerjob.server.core.evaluator.JavaScriptEvaluator;
|
||||
import tech.powerjob.server.core.evaluator.GroovyEvaluator;
|
||||
import tech.powerjob.server.core.workflow.algorithm.WorkflowDAG;
|
||||
import tech.powerjob.server.core.workflow.algorithm.WorkflowDAGUtils;
|
||||
import tech.powerjob.server.core.workflow.hanlder.ControlNodeHandler;
|
||||
@ -26,7 +26,7 @@ import java.util.*;
|
||||
@Component
|
||||
public class DecisionNodeHandler implements ControlNodeHandler {
|
||||
|
||||
private final JavaScriptEvaluator javaScriptEvaluator = new JavaScriptEvaluator();
|
||||
private final GroovyEvaluator groovyEvaluator = new GroovyEvaluator();
|
||||
|
||||
/**
|
||||
* 处理判断节点
|
||||
@ -45,7 +45,7 @@ public class DecisionNodeHandler implements ControlNodeHandler {
|
||||
});
|
||||
Object result;
|
||||
try {
|
||||
result = javaScriptEvaluator.evaluate(script, wfContext);
|
||||
result = groovyEvaluator.evaluate(script, wfContext);
|
||||
} catch (Exception e) {
|
||||
log.error("[Workflow-{}|{}]failed to evaluate decision node,nodeId:{}", wfInstanceInfo.getWorkflowId(), wfInstanceInfo.getWfInstanceId(), node.getNodeId(), e);
|
||||
throw new PowerJobException("can't evaluate decision node!");
|
||||
|
@ -11,11 +11,11 @@ import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @author Echo009
|
||||
* @since 2021/12/10
|
||||
* @since 2022/04/13
|
||||
*/
|
||||
public class JavaScriptEvaluatorTest {
|
||||
public class GroovyEvaluatorTest {
|
||||
|
||||
private final JavaScriptEvaluator javaScriptEvaluator = new JavaScriptEvaluator();
|
||||
private final GroovyEvaluator groovyEvaluator = new GroovyEvaluator();
|
||||
|
||||
private final HashMap<String, String> SIMPLE_CONTEXT = new HashMap<>();
|
||||
|
||||
@ -44,41 +44,36 @@ public class JavaScriptEvaluatorTest {
|
||||
|
||||
@Test
|
||||
public void testSimpleEval1() {
|
||||
Object res = javaScriptEvaluator.evaluate("var x = false; x;", null);
|
||||
Object res = groovyEvaluator.evaluate("var x = false; x;", null);
|
||||
Assert.assertEquals(false, res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleEval2() {
|
||||
Object res = javaScriptEvaluator.evaluate("var person = {name:'echo',tag:'y'}; person.name;", null);
|
||||
Assert.assertEquals("echo", res);
|
||||
// inject simple context
|
||||
Object res = groovyEvaluator.evaluate("var res = context.k3; res;", SIMPLE_CONTEXT);
|
||||
Boolean s = JsonUtils.parseObjectUnsafe(res.toString(), Boolean.class);
|
||||
Assert.assertEquals(false, s);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSimpleEval3() {
|
||||
// inject simple context
|
||||
Object res = javaScriptEvaluator.evaluate("var res = context.k3; res;", SIMPLE_CONTEXT);
|
||||
Boolean s = JsonUtils.parseObjectUnsafe(res.toString(), Boolean.class);
|
||||
Assert.assertEquals(false, s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleEval4() {
|
||||
Object res = javaScriptEvaluator.evaluate("var res = JSON.parse(context.k3); res == false;", SIMPLE_CONTEXT);
|
||||
Object res = groovyEvaluator.evaluate("var res = new groovy.json.JsonSlurper().parseText(context.k3); res == false;", SIMPLE_CONTEXT);
|
||||
Assert.assertEquals(true, res);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testComplexEval1() {
|
||||
// array
|
||||
Object res = javaScriptEvaluator.evaluate("var res = JSON.parse(context.array); res[0] == 1;", COMPLEX_CONTEXT);
|
||||
Object res = groovyEvaluator.evaluate("var res = new groovy.json.JsonSlurper().parseText(context.array) ; res[0] == 1;", COMPLEX_CONTEXT);
|
||||
Assert.assertEquals(true, res);
|
||||
// map
|
||||
res = javaScriptEvaluator.evaluate("var map = JSON.parse(context.map); var e1 = map.e1; e1.value ",COMPLEX_CONTEXT);
|
||||
res = groovyEvaluator.evaluate("var map = new groovy.json.JsonSlurper().parseText(context.map); var e1 = map.e1; e1.value ",COMPLEX_CONTEXT);
|
||||
Assert.assertEquals(1,res);
|
||||
// object
|
||||
res = javaScriptEvaluator.evaluate("var e3 = JSON.parse(context.obj); var e1 = e3.sub.sub; e1.value ",COMPLEX_CONTEXT);
|
||||
res = groovyEvaluator.evaluate("var e3 = new groovy.json.JsonSlurper().parseText(context.obj); var e1 = e3.sub.sub; e1.value ",COMPLEX_CONTEXT);
|
||||
Assert.assertEquals(1,res);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user