| | |
| | | } |
| | | |
| | | public String applyInTask(InTaskApplyRequest request) { |
| | | long startMs = System.currentTimeMillis(); |
| | | Map<String, Long> stepCostMap = new LinkedHashMap<>(); |
| | | |
| | | long redisGetStartNs = System.nanoTime(); |
| | | Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key); |
| | | addStepCost(stepCostMap, "redisUtil.get", elapsedMsFromNano(redisGetStartNs)); |
| | | if (systemConfigMapObj == null) { |
| | | News.error("系统Config缓存失效"); |
| | | return null; |
| | |
| | | Map<String, Object> requestParam = new LinkedHashMap<>(); |
| | | String response = null; |
| | | int result = 0; |
| | | long startMs = System.currentTimeMillis(); |
| | | try { |
| | | long stationQueryStartNs = System.nanoTime(); |
| | | BasStation basStation = basStationService |
| | | .getOne(new QueryWrapper<BasStation>().eq("station_id", request.getSourceStaNo())); |
| | | addStepCost(stepCostMap, "basStationService.getOne", elapsedMsFromNano(stationQueryStartNs)); |
| | | if (basStation == null) { |
| | | News.error("站点{}不存在", request.getSourceStaNo()); |
| | | return null; |
| | |
| | | requestParam.put("barcode", request.getBarcode()); |
| | | requestParam.put("sourceStaNo", stationNo); |
| | | requestParam.put("locType1", request.getLocType1() == null ? 1 : request.getLocType1()); |
| | | long getRowStartNs = System.nanoTime(); |
| | | requestParam.put("row", Utils.getInTaskEnableRowWithCache(request.getSourceStaNo())); |
| | | addStepCost(stepCostMap, "Utils.getInTaskEnableRowWithCache", elapsedMsFromNano(getRowStartNs)); |
| | | if (request.getExtraParams() != null && !request.getExtraParams().isEmpty()) { |
| | | requestParam.putAll(request.getExtraParams()); |
| | | } |
| | | |
| | | long httpPostStartNs = System.nanoTime(); |
| | | response = new HttpHandler.Builder() |
| | | .setUri(wmsUrl) |
| | | .setPath(wmsSystemInUrl) |
| | |
| | | .setTimeout(APPLY_IN_TASK_TIMEOUT_SECONDS, TimeUnit.SECONDS) |
| | | .build() |
| | | .doPost(); |
| | | addStepCost(stepCostMap, "HttpHandler.doPost", elapsedMsFromNano(httpPostStartNs)); |
| | | if (!Cools.isEmpty(response)) { |
| | | long parseResponseStartNs = System.nanoTime(); |
| | | JSONObject jsonObject = JSON.parseObject(response); |
| | | addStepCost(stepCostMap, "JSON.parseObject", elapsedMsFromNano(parseResponseStartNs)); |
| | | if (jsonObject.getInteger("code") == 200) { |
| | | result = 1; |
| | | News.info("请求WMS入库接口成功!!!url:{};request:{};response:{};cost={}ms", |
| | |
| | | httpRequestLog.setResponse(response); |
| | | httpRequestLog.setCreateTime(new Date()); |
| | | httpRequestLog.setResult(result); |
| | | httpRequestLogService.save(httpRequestLog); |
| | | long saveRequestLogStartNs = System.nanoTime(); |
| | | try { |
| | | httpRequestLogService.save(httpRequestLog); |
| | | } finally { |
| | | addStepCost(stepCostMap, "httpRequestLogService.save", elapsedMsFromNano(saveRequestLogStartNs)); |
| | | News.info("WMS入库申请耗时统计:url:{};barcode:{};sourceStaNo:{};stepCosts:{};slowestStep:{};totalCost={}ms", |
| | | wmsUrl + wmsSystemInUrl, request.getBarcode(), request.getSourceStaNo(), |
| | | formatStepCosts(stepCostMap), getSlowestStep(stepCostMap), elapsedMs(startMs)); |
| | | } |
| | | } |
| | | return response; |
| | | } |
| | |
| | | return System.currentTimeMillis() - startMs; |
| | | } |
| | | |
| | | private long elapsedMsFromNano(long startNs) { |
| | | return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs); |
| | | } |
| | | |
| | | private void addStepCost(Map<String, Long> stepCostMap, String stepName, long costMs) { |
| | | stepCostMap.put(stepName, costMs); |
| | | } |
| | | |
| | | private String formatStepCosts(Map<String, Long> stepCostMap) { |
| | | return JSON.toJSONString(stepCostMap); |
| | | } |
| | | |
| | | private String getSlowestStep(Map<String, Long> stepCostMap) { |
| | | String slowestStepName = "-"; |
| | | long maxCostMs = -1L; |
| | | for (Map.Entry<String, Long> entry : stepCostMap.entrySet()) { |
| | | if (entry.getValue() != null && entry.getValue() > maxCostMs) { |
| | | slowestStepName = entry.getKey(); |
| | | maxCostMs = entry.getValue(); |
| | | } |
| | | } |
| | | if (maxCostMs < 0) { |
| | | return "-"; |
| | | } |
| | | return slowestStepName + "=" + maxCostMs + "ms"; |
| | | } |
| | | |
| | | } |