Java執行groovy腳本的兩種方式


記錄Java執行groovy腳本的兩種方式,簡單粗暴:

一種是通過腳本引擎ScriptEngine提供的eval(String)方法執行腳本內容;一種是執行groovy腳本;

二者都通過Invocable來傳遞參數並獲取執行結果;

Invocable:腳本引擎的解釋器接口,提供invokeFunctioninvokeMethod兩種傳遞參數並獲取執行結果的方法,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(); } } 

不正之處,請多多指教~

 
分類:  案例groovy
好文要頂  關注我  收藏該文   
0
0
 
 
 
« 上一篇:  springboot項目配置logback日志系統
posted @  2021-04-21 02:40  蒲公英不是夢  閱讀(92)  評論(0)  編輯  收藏

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM