Java執(zhí)行groovy腳本的兩種方式
點擊上方藍色字體,選擇“標星公眾號”
優(yōu)質文章,第一時間送達
作者 | 蒲公英不是夢
來源 | urlify.cn/222iMv
記錄Java執(zhí)行groovy腳本的兩種方式,簡單粗暴:
一種是通過腳本引擎ScriptEngine提供的eval(String)方法執(zhí)行腳本內容;一種是執(zhí)行groovy腳本;
二者都通過Invocable來傳遞參數并獲取執(zhí)行結果;
Invocable:腳本引擎的解釋器接口,提供invokeFunction和invokeMethod兩種傳遞參數并獲取執(zhí)行結果的方法,Java JDK API文檔解釋如下:
invokeFunction:
invokeMethod:
以下為案例:
引入依賴
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.2.74</version>
</dependency>
定義腳本內容并執(zhí)行
public void testByFunction(){
// 初始化Bindings
Bindings bindings = engine.createBindings();
// 綁定參數
bindings.put("date", new Date());
final String name = "groovy";
// 定義groovy腳本中執(zhí)行方法的名稱
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 {
// 執(zhí)行腳本
engine.eval(scriptContent, bindings);
// 獲取執(zhí)行結果
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拎出來通過創(chuàng)建String對象再賦值進去,方便你看懂函數名稱到底是哪個;scriptContent中${date.getTime()}與$name的意思一樣,grovvy中的字符串可以識別${}和$占位符;bindings綁定參數與invokeFunction方法的第二個參數的區(qū)別是,前者是腳本內全局的,而后者是定義在函數內的;
例如把腳本內容定義為這樣:
執(zhí)行結果就是這樣了:
實例化腳本對象并執(zhí)行
public void testByMethod(){
try {
// 初始化groovy腳本對象
final TestGroovy testGroovy = new TestGroovy();
// 定義groovy腳本中執(zhí)行方法的名稱
final String scriptName = "execute";
// 定義參數
final Date arg_1 = new Date();
final String arg_2 = "groovy";
// 執(zhí)行腳本并獲取結果
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腳本的執(zhí)行方式
* @Author Him
* @Date 2021-04-21
* @Time 01:12
* ================================
*/
public class TestScriptEngine {
// 查找并創(chuàng)建指定腳本引擎
private ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
public void testByFunction(){
// 初始化Bindings
Bindings bindings = engine.createBindings();
// 綁定參數
bindings.put("date", new Date());
// 定義groovy腳本中執(zhí)行方法的名稱
final String scriptName = "execute";
// 定義groovy腳本內容
final String scriptContent = "def " + scriptName +"(){" +
" println(\"now dateTime is: ${date.getTime()}\");" +
" return date.getTime() > 0;" +
"}";
try {
// 執(zhí)行腳本
engine.eval(scriptContent, bindings);
// 獲取執(zhí)行結果
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腳本中執(zhí)行方法的名稱
final String scriptName = "execute";
// 定義參數
final Date arg_1 = new Date();
final String arg_2 = "groovy";
// 執(zhí)行腳本并獲取結果
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();
}
}
粉絲福利:Java從入門到入土學習路線圖
??????

??長按上方微信二維碼 2 秒
感謝點贊支持下哈 
評論
圖片
表情
