詳解Java執行groovy腳本的兩種方式
記錄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(); }}
運行結果:
例如把腳本內容定義為這樣:
執行結果就是這樣了:
實例化腳本對象并執行
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.groovyclass 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(){// 初始化BindingsBindings 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(); }}
到此這篇關于Java執行groovy腳本的兩種方式的文章就介紹到這了,更多相關Java執行groovy腳本內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. Django中的AutoField字段使用2. Django ORM實現按天獲取數據去重求和例子3. 解決docker與vmware的沖突問題4. IntelliJ Idea 2020.1 正式發布,官方支持中文(必看)5. IntelliJ IDEA設置自動提示功能快捷鍵的方法6. asp.net core應用docke部署到centos7的全過程7. Java 3D的動畫展示(Part1-使用JMF)8. Python基于jieba, wordcloud庫生成中文詞云9. 如何在vue3.0+中使用tinymce及實現多圖上傳文件上傳公式編輯功能10. 刪除docker里建立容器的操作方法
