Junjie
14 小时以前 fa37288ba72c61578163950eca8d7e72a5e151a3
#入库任务优化
1个文件已修改
51 ■■■■■ 已修改文件
src/main/java/com/zy/core/utils/WmsOperateUtils.java 51 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/utils/WmsOperateUtils.java
@@ -68,7 +68,12 @@
    }
    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;
@@ -90,10 +95,11 @@
        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;
@@ -107,11 +113,14 @@
            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)
@@ -119,8 +128,11 @@
                    .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",
@@ -143,7 +155,15 @@
            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;
    }
@@ -344,4 +364,31 @@
        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";
    }
}