@(JAVA总结)java
在网上查了资料发现,java有三种方式调用groovy脚本。可是真正在实际的服务器环境中,嵌入groovy脚本每每须要知足下面的条件:缓存
只有知足了上面的这些要求,才能安心的将其嵌入到现有的Java后台服务中。
下面就来具体探讨下具体实现的步骤。服务器
其实,GroovyScriptEngine类就已经提供了上面所说的功能。
主要使用GroovyScriptEngine.loadScriptByName来读取脚本,loadScriptByName方法内部提供了缓存功能,在读取groovy脚本的时候,会优先从缓存中读取,若是缓存中没有的话,才去读取脚本,以下:
测试
在后面的测试后,会用到下面的java类和groovy脚本。this
该类用于测试传递Java对象到Groovy脚本中code
public class Person { public String name; public String address; public Integer age; public Person(String name, String addr, Integer age){ this.name = name; this.address = addr; this.age = age; } public String toString(){ return String.format("[Person: name:%s, address:%s, age:%s]", name,address, age); } }
下面脚本中的两个方法用于测试方法的无参调用和带参调用orm
def helloWithoutParam(){ println "start to call helloWithoutParam!" return "success, helloWithoutParam"; } def helloWithParam(person, id){ println "start to call helloWithParam, param{person:" + person + ", id:" + id + "}"; return "success, helloWithParam"; }
public static void testGroovy2(){ try { Class scriptClass = groovyScriptEngine.loadScriptByName("hello2.groovy"); GroovyObject scriptInstance = (GroovyObject)scriptClass.newInstance(); Object ret = scriptInstance.invokeMethod("helloWithoutParam", null); System.out.println("testGroovy2:" + ret); } catch (Exception e) { e.printStackTrace(); System.out.println("Exception e="+e.toString()); } }
执行结果:
start to call helloWithoutParam!
testGroovy2: success, helloWithoutParam对象
@SuppressWarnings({ "rawtypes" }) public static void testGroovy3(){ try { Person person = new Person("wchi", "nanjing", 30); Class scriptClass = groovyScriptEngine.loadScriptByName("hello2.groovy"); GroovyObject scriptInstance = (GroovyObject)scriptClass.newInstance(); Object ret = scriptInstance.invokeMethod("helloWithParam", new Object[]{person,"lxi"}); System.out.println("testGroovy3:" + ret); } catch (Exception e) { e.printStackTrace(); System.out.println("Exception e="+e.toString()); } }
返回结果:
start to call helloWithParam, param{person:[Person: name:wchi, address:nanjing, age:30], id:lxi}
testGroovy3: success, helloWithParamip
能够将上面的代码封装成公用类,这样就方便不少,以下:字符串
public class GroovyCommonUtil { private static final Logger log = LoggerFactory.getLogger(GroovyCommonUtil.class); //该变量用于指明groovy脚本所在的父目录 static String root[]=new String[]{"bin/groovy/"}; static GroovyScriptEngine groovyScriptEngine; static{ try { groovyScriptEngine=new GroovyScriptEngine(root); } catch (IOException e) { e.printStackTrace(); } } /** * 用于调用指定Groovy脚本中的指定方法 * @param scriptName 脚本名称 * @param methodName 方法名称 * @param params 方法参数 * @return */ @SuppressWarnings({ "rawtypes"}) public Object invokeMethod(String scriptName, String methodName, Object... params) throws Exception{ Object ret = null; Class scriptClass = null; GroovyObject scriptInstance = null; try { scriptClass = groovyScriptEngine.loadScriptByName(scriptName); scriptInstance = (GroovyObject)scriptClass.newInstance(); } catch (ResourceException | ScriptException | InstantiationException | IllegalAccessException e1) { log.warn("加载脚本["+scriptName+"]出现异常", e1); throw new Exception("加载脚本"+scriptName+"失败"); } try { ret = (String)scriptInstance.invokeMethod(methodName, params); } catch (IllegalArgumentException e) { log.warn("执行方法" + methodName + "参数出现异常, 参数为" + params, e); throw new Exception("调用方法[" + methodName + "]失败,因参数不合法"); } catch(Exception e){ log.warn("执行方法" + methodName + "出现异常", e); throw new Exception("调用方法[" + methodName + "]失败"); } return ret; }
使用上面的公用类,改写的测试代码以下:
/** * 测试没有参数的方法调用 */ public static void testGroovyWithoutParam(){ String result = (String)GroovyCommonUtil.invokeMethod("hello2.groovy", "helloWithoutParam"); System.out.println("testGroovy4: " + result + "\n"); } /** * 测试携带参数的方法调用 */ public static void testGroovyWithParam(){ Person person = new Person("wchi", "nanjing", 30); String result = (String)GroovyCommonUtil.invokeMethod("hello2.groovy", "helloWithParam", person, "testGroovy4"); System.out.println("testGroovy4: " + result + "\n"); }