private void executeScriptBinary(ScriptExecuteContext context, GroovyStandardScriptExecuteContext GroovyContext) {Runnable scriptRunTask = instanceScriptRunTask(context, GroovyContext);scriptRunTask.run();}private Runnable instanceScriptRunTask(ScriptExecuteContext context, GroovyStandardScriptExecuteContext GroovyContext) {return () -> {try {//获取转化后的Groovy文件Class> scriptClass = GroovyContext.getScriptClass();//获取脚本作用域的参数和变量Binding scriptBinding = GroovyContext.getScriptBinding();//获取Groovy的构建类Constructor> constructor = scriptClass.getConstructor(Binding.class);//初始化Groovy类实例Object scriptClassInstance = constructor.newInstance(scriptBinding);//获取Groovy中的run方法Method run = scriptClass.getDeclaredMethod("run");//执行GroovyObject result = run.invoke(scriptClassInstance);//设置返回结果ScriptExecuteResult scriptExecuteResult = new ScriptExecuteResult(result);context.setScriptExecuteResult(scriptExecuteResult);} catch (Exception e) {if (e instanceof InvocationTargetException && ((InvocationTargetException) e).getTargetException() instanceof IllegalArgumentException) {throw new BaseException(((InvocationTargetException) e).getTargetException().getMessage());} else {log.error(LogUtil.exceptionMessage("scriptRunTaskException", e));throw new BaseException("脚本执行时发生异常");}} finally {log.info(LogUtil.message("GroovyStandardScriptSandBoxContext", GroovyContext.getGroovyStandardScriptSandBoxContext()));}};}
5.1.3封装返回结果
Object result = Optional.ofNullable(scriptExecuteContext.getScriptExecuteResult()).map(ScriptExecuteResult::getResult).orElse(null);SlsEntity slsEntity = SlsEntity.builder().put("scriptCode", request.getScriptCode()).put("businessStatus", "SUCCESS").put("requestTime", DateUtil.formatDateTime(requestTime)).put("consumingTime", String.valueOf(stopWatch.getTime(TimeUnit.MILLISECONDS)));SLog.info(slsEntity,"{} | {} ",request.getScriptCode(), "SUCCESS"
6.总结
- python爬虫设置User
- 如何给shadow DOM下的元素添加样式
- Ubuntu16.04两种方法下载安装VS Code
- Anconda环境下Vscode安装Python
- 玉米除草剂打后几小时不下雨就管用
- 故人西辞黄鹤楼的下一句
- 岳飞手下头号猛将杨再兴经历的两次百人斩
- 佣兵大陆,求佣兵天下艾米诺尔大陆的地图
- 聪明绝顶的李斯为何最终下场不得好*?
- 大同天下