指令数据采集(六)--指令的执行
执行也是需要进行循环递归执行,类似一个树的形状,详见代码片段。TL1Connection 为执行的连接接口,List<Instruction> 为需要执行的指令集
1 /** 2 * 执行指令集 3 * 4 * @param iConnect 5 * @param instructions 6 */ 7 @Override 8 public void executeInstruction(TL1Connection iConnect, List<Instruction> instructions) { 9 for(Instruction instruction:instructions){ 10 logInfo = instruction.getInstName() + ",执行的指令:" + instruction.getInstNo() + "开始执行!"; 11 eventLogService.insertEventLogNew(instruction.getWoId(), logInfo,EventLog.NO_EXCEPTION, "", ""); 12 13 this.executeInstruction(iConnect,instruction); 14 15 logInfo = instruction.getInstName() + ",执行的指令:" + instruction.getInstNo() + "执行完毕!"; 16 eventLogService.insertEventLogNew(instruction.getWoId(), logInfo,EventLog.NO_EXCEPTION, "", ""); 17 } 18 }
执行当前的一条指令,已经循环执行子指令
1 /** 2 * 执行一个指令 包括子指令 3 * @param iConnect 4 * @param instructions 5 */ 6 @Override 7 public void executeInstruction(TL1Connection iConnect, Instruction instructions) { 8 logger.debug("<><><执行命令><><><>" + instructions.getInstNo()); 9 logger.debug("<><><执行命令入参><><><>" + instructions.getParamsInput()); 10 //这边需要优化的地方有两点 11 //1.根据执行的结果拼装一个 Insert 语句 这边需要一次网络访问 12 //2.远程服务器执行也需要一次网络访问 13 //两次网络访问的时间是叠加的 14 //执行之前判断条件 当前的指令是否要执行 15 try { 16 setInstructionByCondition(instructions); 17 List<Map<String, String>> executeInst = sendInstruction(iConnect, instructions); 18 //在下面的方法中 处理insert语句 19 //处理成对应的参数的入参形式 提供给下一条命令使用 20 packageExecuteReusltCurrentIns(instructions, executeInst); 21 //正常执行时候的日志 22 EventLog eventLog = new EventLog(0, 0, instructions.getWoId(), 0, 23 instructions.getInputInstruction()==null?"执行的指令为:空":"执行的指令:"+instructions.getInputInstruction().toString(), 0, 24 instructions.getParamsInput()==null?"输入的参数为:空":instructions.getParamsInput().toString(), "",""); 25 //进行日志记录 26 eventLogService.insertEventLog(eventLog); 27 }catch (Exception e) { 28 //日志记录 执行异常的时候的日志 29 EventLog eventLog = new EventLog(0, 0, instructions.getWoId(),0, 30 instructions.getInputInstruction()==null?"执行的指令为:空":"执行的指令为: "+instructions.getInputInstruction().toString()+ 31 "抛出的异常信息"+e.getMessage(), 0, 32 instructions.getParamsInput()==null?"输入的参数为:空":"输入的参数为: "+instructions.getParamsInput().toString(), "",""); 33 //进行日志记录 34 eventLogService.insertEventLog(eventLog); 35 logger.error(e.getMessage()); 36 } 37 38 /** 39 List<List<Param>> paramListOutput = pakageExecuteResult(instructions, executeInst); 40 logger.debug("<><><指令关联的参数><><><>" + paramListOutput.toString()); 41 42 //需要复制的指令 43 List<Instruction> instructionListRelation = null; 44 if(instructions.getChildren()!=null&&instructions.getChildren().size()>0) { 45 instructionListRelation = (List<Instruction>) ((ArrayList<Instruction>) instructions.getChildren()).clone(); 46 } 47 if (instructionListRelation != null && !instructionListRelation.isEmpty()) { 48 //上条命令执行的出参,作为当前这条命令的出参 49 //Instruction instructionChild = instructionListRelation.get(0); 50 //根据返回的参数的多少 动态创建子命令 51 //上一条命令的出参 作为下一条命令的入参 52 //instruction.setParamsInput(paramListOutput); 53 //先清空关联的指令 54 if(instructions.getChildren()!=null) { 55 instructions.getChildren().removeAll(instructions.getChildren()); 56 } 57 //复制创建子命令 然后开始重置 58 cloneChildInstruction(instructionListRelation,executeInst.size(), 59 paramListOutput,instructions.getChildren(), 60 instructions.getListsOutputResult()); 61 logger.debug("<><><克隆的子指令 根据返回的结果数 ><" + (executeInst.size()) + "><><>" + instructionListRelation.toString()); 62 } 63 **/ 64 //递归执行子命令 65 if(instructions.getChildren()!=null&&!instructions.getChildren().isEmpty()) { 66 for (Instruction execIns : instructions.getChildren()) { 67 logInfo = execIns.getInstName() + ",执行的指令:" + execIns.getInstNo() + "开始执行!"; 68 eventLogService.insertEventLogNew(execIns.getWoId(), logInfo,EventLog.NO_EXCEPTION, "", ""); 69 70 executeInstruction(iConnect, execIns); 71 72 logInfo = execIns.getInstName() + ",执行的指令:" + execIns.getInstNo() + "执行完毕!"; 73 eventLogService.insertEventLogNew(execIns.getWoId(), logInfo,EventLog.NO_EXCEPTION, "", ""); 74 } 75 } 76 }
进行条件判断,他的子指令是否需要进行执行
1 /** 2 * 指令使用条件控制 3 * @param currInstruction 当前的指令 4 */ 5 public void setInstructionByCondition(Instruction currInstruction){ 6 //处理条件 7 GroupCondition groupCondition = iGroupConditionServiceImpl.packageGroupCondition(currInstruction); 8 GroupConditionExector groupConditionExector = new GroupConditionExector(currInstruction.getParamOutputParent()); 9 //处理条件 判断为null的情况以及判断父指令没有返回值的情况 10 if(groupCondition!=null&&currInstruction.getParamOutputParent()!=null&& 11 !currInstruction.getParamOutputParent().isEmpty()) { 12 boolean flag = groupConditionExector.execute(groupCondition); 13 currInstruction.setIsRun(flag); 14 } 15 }
连接器,执行的结果
1 /** 2 * 返回的指令结果 3 * 4 * @param instruction 5 * @return 6 */ 7 public List<Map<String, String>> sendInstruction(TL1Connection iConnect, Instruction instruction) { 8 List<Map<String, String>> instructionMap = new ArrayList<Map<String, String>>(); 9 try { 10 if(instruction.isRun()) { 11 //判断当前的指令是否要执行 12 instructionMap = iConnect.sendCommand(instruction); 13 } 14 }finally { 15 TL1ConnectionPool tl1ConnectionPool = TL1ConnectionPool.getInstance(); 16 InterfaceObject interfaceObject = iConnect.getObject(); 17 try { 18 tl1ConnectionPool.closeConnection(iConnect,interfaceObject); 19 } catch (Exception e) { 20 e.printStackTrace(); 21 } 22 } 23 24 // } finally 25 // { 26 // TL1ConnectionPool tl1ConnectionPool = TL1ConnectionPool.getInstance(); 27 // InterfaceObject interfaceObject = iConnect.getObject(); 28 // try { 29 // tl1ConnectionPool.closeConnection(iConnect,interfaceObject); 30 // } catch (Exception e) { 31 // e.printStackTrace(); 32 // } 33 // } 34 return instructionMap; 35 }
将当前指令的返回的N条结果对应到克隆创建的N条指令的入参当中去
1 /** 2 * 该方法用于处理指令的返回结果 3 * @param instruction 4 * @param executeInst 5 */ 6 public void packageExecuteReusltCurrentIns(Instruction instruction, List<Map<String, String>> executeInst){ 7 logger.debug("<><><telnet之后返回的结果><><><>" + executeInst.toString()); 8 List<List<Param>> listsParam = new ArrayList<List<Param>>(); 9 //这边是每条指令的返回值的key 但返回的是多行的数据 这边只能够对应的是一条数据 10 //所以返回数据集就不在这边设置了 11 List<Param> paramListOutputValue = instruction.getParamsOutput(); 12 String [] sqlArray = new String[executeInst.size()]; 13 int sqlArrayCount = 0; 14 //先清空所有的sql 15 //InstructionExeSql.stringSqlStack.clear(); 16 //处理当前的输出值 17 for (Map<String, String> mapOutput : executeInst) { 18 List<Param> listsRow = new ArrayList<Param>(); 19 for (Param param : paramListOutputValue) { 20 ParamKey paramKey = param.getParamKey(); 21 //新建一个 22 Param paramNew = new Param(); 23 paramNew.setParamKey(paramKey); 24 if (StringUtils.isNotBlank(mapOutput.get(paramKey.getKeyDesc()))) { 25 ParamValue paramValue = new ParamValue(); 26 paramValue.setValue(mapOutput.get(paramKey.getKeyDesc())); 27 //赋值给当前的对象 新建的对象 28 paramNew.setParamValue(paramValue); 29 } 30 listsRow.add(paramNew); 31 } 32 //加载全局的设置 33 if(this.wholeSituation!=null&&!this.wholeSituation.isEmpty()){ 34 listsRow.addAll(this.wholeSituation); 35 } 36 //搞一个sql 装载命令 37 String sql = this.getSqlString(listsRow); 38 sqlArray[sqlArrayCount++]=sql; 39 //InstructionExeSql.stringSqlStack.add(sql); 40 logger.debug("<><><获得的sql><><><>" + sql); 41 listsParam.add(listsRow); 42 } 43 //执行sql 44 //InstructionExeSql.stringSqlStack.size(); 45 sqlService.exectorSqlBatch(sqlArray); 46 logger.debug("<><><处理出参><><><>" + paramListOutputValue.toString()); 47 logger.debug("<><><执行之后的出参值><><><>" + listsParam.toString()); 48 //设置出参的结果 49 instruction.setListsOutputResult(listsParam); 50 //克隆指令 51 cloneChildInstrucion(instruction); 52 }
指令克隆
1 public void cloneChildInstrucion(Instruction instructionParent) { 2 //复制之后的个数 3 List<Instruction> instructionListChild = new ArrayList<Instruction>(); 4 List<List<Param>> listListParamResult = instructionParent.getListsOutputResult(); 5 //当前子指令的个数 6 List<Instruction> instructionListChildCurrent = instructionParent.getChildren(); 7 //需要拷贝的个数 默认为0个 8 int cloneChildCount = 0; 9 if (listListParamResult != null) { 10 cloneChildCount = listListParamResult.size(); 11 } 12 try { 13 //含有子指令的时候进行拷贝处理 14 if(instructionListChildCurrent!=null) { 15 for (Instruction instruction : instructionListChildCurrent) { 16 for (int cloneChildStart = 0; cloneChildStart < cloneChildCount; cloneChildStart++) { 17 Instruction instructionChild = instruction.clone(); 18 //设置父指令的结果 分配到每个指令当中 19 instructionChild.setParamOutputParent(listListParamResult.get(cloneChildStart)); 20 instructionListChild.add(instructionChild); 21 } 22 } 23 //重新设置子指令 24 if (!instructionListChild.isEmpty()) { 25 for(Instruction instChid:instructionListChild){ 26 //调整参数 出入参数设置 27 List<Param> paramList = dealInstructionInput(instChid.getParamOutputParent(),instChid.getInstructionRelaParamList()); 28 instChid.setParamsInput(paramList); 29 } 30 instructionParent.setChildren(instructionListChild); 31 } 32 } 33 } catch (CloneNotSupportedException e) { 34 e.printStackTrace(); 35 } 36 }