1、加载和卸载(每次都新建一个GroovyClassLoader 实例,然后使用新建的classloader去加载)
try { GroovyClassLoader groovyClassLoader = new GroovyClassLoader(); Class<?> clazz = groovyClassLoader.parseClass(orchestrationUnitDesc); GroovyObject unit = (GroovyObject) clazz.newInstance(); result.setSuccess(true); result.setResult(unit); return result; } catch (InstantiationException e) { result.setSuccess(false); result.setErrorMsg("groovy:InstantiationException:" + e.getMessage()); DataExchangeLogger.errorLog("", "", "groovy:InstantiationException", e); } catch (IllegalAccessException e) { result.setSuccess(false); result.setErrorMsg("groovy:IllegalAccessException:" + e.getMessage()); DataExchangeLogger.errorLog("", "", "groovy:IllegalAccessException", e); } catch (Exception e) { result.setSuccess(false); result.setErrorMsg("groovy:Exception:" + e.getMessage()); DataExchangeLogger.errorLog("", "", "groovy:Exception", e); }
2、执行
上面得到一个GroovyObject对象,执行方法就是调用GroovyObject的invokeMethod方法
// > 获取编排单元 GroovyObject unit = (GroovyObject) ServiceOrchestrationConfigManager.getOrchestrationUnit(message.getMsgType()); if (unit == null) { result.setErrorCode(OpenErrorCode.S07.name()); result.setErrorMsg("orchestrationScript is null."); result.setSuccess(false); return result; } // > 调用服务编排 try { unit.invokeMethod("setDoc", CustomXmlUtil.parseText2Doc(message.getRequestString())); result = (SendResult) unit.invokeMethod("execute", message); if (result == null) { result = new SendResult(); result.setErrorCode(OpenErrorCode.S07.name()); result.setErrorMsg("下发编排脚本执行,返回为空"); result.setSuccess(false); } } catch (DocumentException e) { result.setErrorCode(OpenErrorCode.S07.name()); result.setErrorMsg("请求报文转docment对象异常" + e.getMessage()); result.setSuccess(false); DataExchangeLogger.errorLog(message.getBizId(), message.getMsgType(), "call service orchestration parseText2Doc error.", e); return result; } catch (Exception e) { result.setErrorCode(OpenErrorCode.S07.name()); result.setErrorMsg("下发编排脚本执行异常" + e.getMessage()); result.setSuccess(false); DataExchangeLogger.errorLog(message.getBizId(), message.getMsgType(), "call service orchestration excute script error. ", e); }