您好我使用Groovy
引擎来评估某个脚本。问题是执行时间有点大,所以我想将我的脚本分成两个脚本。第一个是静态的,可以在@PostConstruct
方法内编译,第二个是变量,它取决于用户选择的一些参数。这就是我想要做的事情:
Bindings bindings;
CompiledScript scriptC;
String script1="Static Script";
String script2="Variable Script";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("groovy");
scriptC = ((Compilable) engine).compile(script1);
//Execute some instruction to generate 'script2'
scriptC += ((Compilable) engine).compile(script2);//Please note the += operator
或
Bindings bindings;
CompiledScript scriptC;
String script1="Static Script";
String script2="Variable Script";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("groovy");
scriptC = ((Compilable) engine).compile(script1);
//Execute some instruction to generate 'script2'
scriptC.append(((Compilable) engine).compile(script2));//Please note the append function
这两个代码无法确定,我用它们只是为了说明我试图做的事情。是否可以将两个脚本合并为一个脚本?
答案 0 :(得分:1)
你不能像那两个CompiledScript
对象那样组合。如果涉及到Groovy - 当Groovy脚本被编译时,它会创建一个扩展groovy.lang.Script
的类,里面有几个方法。在这种情况下,相互添加两个脚本意味着合并两个Java类。
考虑将分离中的两个脚本作为替代方案进行评估。看一下下面的例子:
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
public class ScriptTest {
public static void main(String[] args) throws ScriptException {
final ScriptEngineManager manager = new ScriptEngineManager();
final ScriptEngine engine = manager.getEngineByName("Groovy");
final Bindings bindings = new SimpleBindings();
bindings.put("a", 3);
final String script1 = "println 'Running first script...'; def c = 2; def d = c + a; return d";
final CompiledScript compiledScript1 = ((Compilable) engine).compile(script1);
//Execute some instruction to generate 'script2'
final String script2 = "println 'Running second script...';";
final CompiledScript compiledScript2 = ((Compilable) engine).compile(script2);
Integer returnedValue = (Integer) compiledScript1.eval(bindings);
System.out.println("Returned value from the first script: " + returnedValue);
if (returnedValue > 1) {
compiledScript2.eval(bindings);
}
}
}
创建CompiledScript
对象是一回事。第二件事是对这些脚本的评估。如果您始终立即评估script1
和script2
,那么您只需要:
compiledScript1.eval(bindings);
compiledScript2.eval(bindings);
但是script1
可能会返回一些值,根据该值,您可以决定是否应评估script2
。我们假设script1
返回Integer
,只有在返回的值大于1时才会评估script2
:
Integer returnedValue = (Integer) compiledScript1.eval(bindings);
if (returnedValue > 1) {
compiledScript2.eval(bindings);
}
或者,您可以在运行script1
后基于绑定值。让我们说script1
修改绑定a
和b
并将其设置为true
(假设它们保持boolean
类型):
compiledScript1.eval(bindings);
if (bindings.get("a") && bindings.get("b")) {
compiledScript2.eval(bindings);
}
script1
script2
中定义的函数
让我们说script1
定义如下:
import groovy.json.JsonOutput
def json(Map map) {
return new JsonOutput().toJson(map)
}
println json([test: 1])
如果您必须在json(map)
内调用script2
功能 - 您可以执行此操作。看一下这个例子:
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
public class ScriptTest {
public static void main(String[] args) throws ScriptException {
final ScriptEngineManager manager = new ScriptEngineManager();
final ScriptEngine engine = manager.getEngineByName("Groovy");
final Bindings bindings = new SimpleBindings();
bindings.put("a", 3);
final String script1 = "import groovy.json.JsonOutput\n" +
"\n" +
"def json(Map map) {\n" +
" return new JsonOutput().toJson(map)\n" +
"}\n" +
"\n" +
"println json([test: 1])";
final CompiledScript compiledScript1 = ((Compilable) engine).compile(script1);
//Execute some instruction to generate 'script2'
final String script2 = "println 'Running second script...'; println json([test: 2]);";
final CompiledScript compiledScript2 = ((Compilable) engine).compile(script2);
compiledScript1.eval(bindings);
compiledScript2.eval(bindings);
}
}
script2
定义为:
println 'Running second script...';
println json([test: 2]);
当你运行它时,你会在控制台中看到:
{"test":1}
Running second script...
{"test":2}
前两行来自script1
,而最后一行来自script2
。请记住,CompiledScript
都拥有对同一引擎的引用,因此您可以引用先前评估过的脚本中定义的函数。