> scriptClass;}
5.3执行的调用流程
5.1.1.请求对应接口
【导购场景下的Groovy脚本引擎实战】@PostMapping("execute")@ApiOperation(value = "http://www.kingceram.com/post/小程序-脚本引擎调用")@ApiMonitor(name = "小程序-脚本引擎调用")public Result> execute(@RequestBody ScriptExecuteRequest request) {StopWatch stopWatch = StopWatch.createStarted();Date requestTime = new Date();Assert.notBlank(request.getScriptCode(), "ScriptCode不可为空");//参数mapScriptExecuteParam scriptExecuteParam = new ScriptExecuteParam();scriptExecuteParam.setParamMap(request.getScriptParam());BaseContextHandler.set(CommonConstants.CONTEXT_SELLER_ID, request.getSellerId());ScriptExecuteContext scriptExecuteContext = new ScriptExecuteContext();scriptExecuteContext.setScriptCode(request.getScriptCode());scriptExecuteContext.setScriptExecuteParam(scriptExecuteParam);//执行脚本scriptExecuteTemplate.execute(scriptExecuteContext);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");return Result.success(result);}
5.1.2.执行脚本语言
public void execute(ScriptExecuteContext context){//加载脚本代码loadScript(context);//设置脚本代码及实现loadScriptFeature(context);loadScriptImplFeature(context);//加载脚本框架对应的脚本语言的实现loadScriptFramework(context);//执行脚本executeScript(context);}
5.1.2.1加载脚本代码
public void loadScript(ScriptExecuteContext context) {try {ScriptCache scriptCache = SCRIPT_CACHE.get(context.getScriptCode());cn.hutool.core.lang.Assert.notNull(scriptCache, "脚本代码不存在");if (ObjectUtil.isNull(context.getVersion())) {// 正式版本代码context.setScriptDTO(scriptCache.getScriptDTO());context.setScriptImplDTO(scriptCache.getScriptImplDTO());} else {// 指定版本代码context.setScriptDTO(scriptCache.getScriptDTO());context.setScriptImplDTO(getScriptImplByScriptIdAndVersion(scriptCache.getScriptDTO().getId(), context.getVersion()));}} catch (Exception e) {log.error("get script error params = {} ", JSON.toJSONString(context), e);cn.hutool.core.lang.Assert.isTrue(false, "执行脚本获取失败");}}
5.1.2.2设置脚本代码及实现
public void loadScriptFeature(ScriptExecuteContext context) {ScriptDTO scriptDTO = context.getScriptDTO();context.setScriptFeatureDTO(scriptDTO.getFeatures());}public void loadScriptImplFeature(ScriptExecuteContext context) {ScriptImplDTO scriptImplDTO = context.getScriptImplDTO();context.setScriptImplFeatureDTO(scriptImplDTO.getFeatures());}
5.1.2.3加载脚本框架对应的脚本语言的实现
public void loadScriptFramework(ScriptExecuteContext context) {ScriptImplDTO scriptImplDTO = context.getScriptImplDTO();Integer scriptFrameworkId = scriptImplDTO.getScriptFrameworkId();ScriptFramework scriptFramework = scriptFrameworkFactory.getInstance(scriptFrameworkId);Assert.that(Objects.nonNull(scriptFramework), "脚本框架不存在");//目前只有Groovy的实现context.setScriptFramework(scriptFramework);}
5.1.2.4使用对应脚本语言执行脚本代码
public void executeScript(ScriptExecuteContext context) throws ExecutionException, InterruptedException, TimeoutException {//设置Groovy脚本的上下文GroovyStandardScriptExecuteContext GroovyContext = new GroovyStandardScriptExecuteContext();//设置上下文的执行脚本computeClassSource(context, GroovyContext);//设置上下文执行脚本的类名computeClassName(context, GroovyContext);//加载执行脚本并转化成类信息compileScriptBinary(context, GroovyContext);//设置系统参数(header)computeSystemParam(context, GroovyContext);//设置请求参数(param)computeScriptParam(context, GroovyContext);//设置依赖属性 feign或servicecomputeScriptDependencyParam(context, GroovyContext);//设置脚本参数 logcomputeScriptFrameworkParam(context, GroovyContext);//绑定作用域参数和变量computeScriptBinding(context, GroovyContext);//执行脚本并返回结果executeScriptBinary(context, GroovyContext);}
- python爬虫设置User
- 如何给shadow DOM下的元素添加样式
- Ubuntu16.04两种方法下载安装VS Code
- Anconda环境下Vscode安装Python
- 玉米除草剂打后几小时不下雨就管用
- 故人西辞黄鹤楼的下一句
- 岳飞手下头号猛将杨再兴经历的两次百人斩
- 佣兵大陆,求佣兵天下艾米诺尔大陆的地图
- 聪明绝顶的李斯为何最终下场不得好*?
- 大同天下