设计一个同步返回的接口

         是否存在一个需要同步返回请求结果的场景?如果存在需要进行相关的设定,请求个数、维持回话时间、请求权限限定

 public ResultBean setPlatformListenInfo(PlatformListenInfoReq info) {
        ResultBean ret = new ResultBean();
        if (info.getId() < 1 || info.getPlatformPort() < 1 ||
                info.getSimulatorId() < 1 || StringUtils.isEmpty(info.getPlatformId())) {
            ret.setRetcode(ErrorCode.PARA_IS_ERROR);
            ret.setText("Invalid Parameter, Please contact manager.");
            return ret;
        }
        if (!StringUtils.isEmpty(info.getMqPort())) {
            info.setMqAddress(info.getMqAddress() + ":" + info.getMqPort());
        }
        // System.err.println("Set platformInfo:" + info);
        SimulatorBean simulatorBean = simulatorMapper.readSimulatorById(info.getSimulatorId());
        long consumeTime = 0L;
        if (simulatorBean != null && simulatorBean.getStatus() == SimulatorConstants.START_SUCCESS_STATUS && simulatorBean.getConfigEnable()) {
            String whiteList = simulatorBean.getWhiteList();
            if (org.apache.commons.lang.StringUtils.isNotEmpty(whiteList)) {
                List<String> ips = Arrays.asList(org.apache.commons.lang.StringUtils.split(whiteList, ",").clone());
                if (!ips.contains(info.getSdkIP())) {
                    ret.setText("Invalid Parameter SdkIP, Please contact manager.");
                    return ret;
                }
            }
            long executeMachineId = simulatorBean.getExecuteMachineId();
            ExecuteMachineBean executeMachineBean = executeMachineMapper.readExecuteMachineById(executeMachineId);
            if (executeMachineBean != null && executeMachineBean.getStatus() == ExectueMachineConstants.ONLINE_STATUS) { // 设备存在且在线下发监听
                platformInfoMessageService.sendSetPlatformInfoMessage(info, executeMachineBean);
                PlatformListenSynchronizedConn conn = new PlatformListenSynchronizedConn();
                conn.setId(info.getId());
                conn.setSimulatorId(info.getSimulatorId());
                String key = String.valueOf(info.getSimulatorId()) + (info.getId());
                ConcurrentHashMap<String, PlatformListenSynchronizedConn> requestMap = platformInfoMessageService.getSimulator2ConnectionStatusMap();
                requestMap.put(key, conn);
                long start = System.currentTimeMillis();
                while (requestMap.containsKey(key) && !requestMap.get(key).getMqACK()) {
                    try {
                        log.info("Waiting platformInfo ACK 500ms.");
                        TimeUnit.MILLISECONDS.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    long end = System.currentTimeMillis();
                    consumeTime = end - start;
                    if (consumeTime > 5000) {
                        log.error("Time is over,not wait yet!");
                        break;
                    }
                }
                if (requestMap.containsKey(key)) {
                    int code = requestMap.get(key).getErrorCode();
                    ret.setRetcode(code);
                    ret.setText(PlatformListenACKErrorEnum.getValueByCode(code) + " 响应时间:" + consumeTime);
                    requestMap.remove(key);
                } else {
                    ret.setRetcode(ErrorCode.ERROR);
                    ret.setText("未知错误!");
                }
                return ret;
            }
        }
        ret.setRetcode(ErrorCode.AUTO_TEST_SET_PLATFORM_INFO_ERROR);
        String responseTime = consumeTime > 0L ? "响应时间: " + consumeTime : "";
        ret.setText("相关设备不存在或者不在线、或者设备自动化对接使能配置为false,下发监听失败!" + responseTime);
        return ret;
    }

上一篇:【优化框架】提供参数动态化替换,正则表达式提取数据优化


下一篇:function