登录/注册
占位
占位
浏览量
占位
粉丝
占位
关注
Java执行groovy脚本的两种方式
2021-06-24 17:26:38 2021-06-24
135
0

记录Java执行groovy脚本的两种方式,简单粗暴:

一种是通过脚本引擎ScriptEngine提供的eval(String)方法执行脚本内容;一种是执行groovy脚本;

二者都通过Invocable来传递参数并获取执行结果;

Invocable:脚本引擎的解释器接口,提供invokeFunction和invokeMethod两种传递参数并获取执行结果的方法,Java JDK API文档解释如下:

invokeFunction:

invokeMethod:

以下为案例:

引入依赖

<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.2.74</version>
</dependency>

定义脚本内容并执行

public void testByFunction(){
// 初始化Bindings
Bindings bindings = engine.createBindings();
// 绑定参数
bindings.put("date", new Date());
final String name = "groovy";
// 定义groovy脚本中执行方法的名称
final String scriptName = "execute";
// 定义groovy脚本内容
final String scriptContent = "def " + scriptName +"(name){" +
" println(\"now dateTime is: ${date.getTime()}\");" +
" println(\"my name is $name\");" +
" return date.getTime() > 0;" +
"}";
try {
// 执行脚本
engine.eval(scriptContent, bindings);
// 获取执行结果
Invocable invocable = (Invocable) engine;
Boolean flag = (Boolean) invocable.invokeFunction(scriptName, name);
System.out.println("---------------------------------------");
System.out.println("result is: " + flag);
} catch (ScriptException | NoSuchMethodException e) {
e.printStackTrace();
}
}

运行结果:

  • invokeFunction方法的第一个参数为脚本的函数名称,把scriptName拎出来通过创建String对象再赋值进去,方便你看懂函数名称到底是哪个;
  • scriptContent中${date.getTime()}与$name的意思一样,grovvy中的字符串可以识别${}和$占位符;
  • bindings绑定参数与invokeFunction方法的第二个参数的区别是,前者是脚本内全局的,而后者是定义在函数内的;

例如把脚本内容定义为这样:

执行结果就是这样了:

实例化脚本对象并执行

public void testByMethod(){
try {
// 初始化groovy脚本对象
final TestGroovy testGroovy = new TestGroovy();
// 定义groovy脚本中执行方法的名称
final String scriptName = "execute";
// 定义参数
final Date arg_1 = new Date();
final String arg_2 = "groovy";
// 执行脚本并获取结果
Invocable invocable = (Invocable) engine;
Boolean flag = (Boolean) invocable.invokeMethod(testGroovy, scriptName, arg_1, arg_2);
System.out.println("---------------------------------------");
System.out.println("result is: " + flag);
} catch (ScriptException |NoSuchMethodException e) {
e.printStackTrace();
}
}

TestGroovy.groovy脚本内容:

package com.dandelion.groovy
class TestGroovy {
static def execute(Date date, String name){
println("now dateTime is: ${date.getTime()}");
println("my name is $name");
return date.getTime() < 0;
}
}

运行结果:

  • invokeMethod方法的第一个参数是脚本对象,第二个参数是脚本中的函数名称,之后为绑定的参数;

源码:

package com.dandelion.test;
import com.dandelion.groovy.TestGroovy;
import javax.script.*;
import java.util.Date;
/**
* ================================
* 测试groovy脚本的执行方式
* @Author Him
* @Date 2021-04-21
* @Time 01:12
* ================================
*/
public class TestScriptEngine {
// 查找并创建指定脚本引擎
private ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
public void testByFunction(){
// 初始化Bindings
Bindings bindings = engine.createBindings();
// 绑定参数
bindings.put("date", new Date());
// 定义groovy脚本中执行方法的名称
final String scriptName = "execute";
// 定义groovy脚本内容
final String scriptContent = "def " + scriptName +"(){" +
" println(\"now dateTime is: ${date.getTime()}\");" +
" return date.getTime() > 0;" +
"}";
try {
// 执行脚本
engine.eval(scriptContent, bindings);
// 获取执行结果
Invocable invocable = (Invocable) engine;
Boolean flag = (Boolean) invocable.invokeFunction(scriptName);
System.out.println("---------------------------------------");
System.out.println("result is: " + flag);
} catch (ScriptException | NoSuchMethodException e) {
e.printStackTrace();
}
}
public void testByMethod(){
try {
// 初始化groovy脚本对象
final TestGroovy testGroovy = new TestGroovy();
// 定义groovy脚本中执行方法的名称
final String scriptName = "execute";
// 定义参数
final Date arg_1 = new Date();
final String arg_2 = "groovy";
// 执行脚本并获取结果
Invocable invocable = (Invocable) engine;
Boolean flag = (Boolean) invocable.invokeMethod(testGroovy, scriptName, arg_1, arg_2);
System.out.println("---------------------------------------");
System.out.println("result is: " + flag);
} catch (ScriptException |NoSuchMethodException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TestScriptEngine engine = new TestScriptEngine();
engine.testByFunction();
}
}

不正之处,请多多指教~

原文: https://www.cnblogs.com/dandelion200/p/14683609.html

暂无评论