自动化立体仓库 - WMS系统
lty
9 天以前 5f0a9c30e3a7db095b0901257214122dde912f12
#推送Gwcs时失败后重试3次
1个文件已修改
75 ■■■■■ 已修改文件
src/main/java/com/zy/asrs/service/impl/OpenServiceImpl.java 75 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/asrs/service/impl/OpenServiceImpl.java
@@ -2085,39 +2085,48 @@
        return wrkDetls.size() > 1 ? 1 : 4; // 一条记录就是一卷
    }
    public boolean pushStaNoToGwcs(Integer palletizingNo, Integer staNo, Integer workNo,String barcode,String message) {
        boolean success = false;
        // 获取请求头
        Map<String, Object> headers = getHeaders();
    public boolean pushStaNoToGwcs(Integer palletizingNo, Integer staNo, Integer workNo, String barcode, String message) {
        int maxRetry = 3;       // 最多重试 3 次
        long retryDelay = 3000; // 重试间隔 3 秒
        boolean finalSuccess = false;
        // 构造请求体
        Map<String, Object> headers = getHeaders();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("workNo", workNo);
        jsonObject.put("staNo", staNo);
        jsonObject.put("sourceStaNo", palletizingNo);
        jsonObject.put("barcode", barcode);
        String body = jsonObject.toJSONString();
        String response = "";
        try {
            response = new HttpHandler.Builder()
                    .setUri(MesConstant.GWCS_IP_PORT)
                    .setPath(MesConstant.GWCS_FPKW_URL)
                    .setHeaders(headers)
                    .setJson(body)
                    .build()
                    .doPost();
            if (!Cools.isEmpty(response)) {
                success = true;
            } else {
                log.error(message + "失败!!!url:{};request:{};response:{}", MesConstant.GWCS_IP_PORT + MesConstant.GWCS_FPKW_URL, body, response);
            }
        } catch (Exception e) {
            log.error(message + "异常:{}", e);
        } finally {
        for (int attempt = 1; attempt <= maxRetry; attempt++) {
            String response = "";
            boolean success = false;
            String attemptMsg = message + "(第 " + attempt + " 次尝试)";
            try {
                // 保存接口日志
                response = new HttpHandler.Builder()
                        .setUri(MesConstant.GWCS_IP_PORT)
                        .setPath(MesConstant.GWCS_FPKW_URL)
                        .setHeaders(headers)
                        .setJson(body)
                        .build()
                        .doPost();
                if (!Cools.isEmpty(response)) {
                    success = true;
                    finalSuccess = true;
                    log.info("{} 成功,响应:{}", attemptMsg, response);
                } else {
                    log.warn("{} 失败,响应为空", attemptMsg);
                }
            } catch (Exception e) {
                log.error("{} 异常:{}", attemptMsg, e.getMessage(), e);
            }
            // 每次都记录日志,不论成功或失败
            try {
                apiLogService.save(
                        message,
                        attemptMsg,
                        MesConstant.GWCS_IP_PORT + MesConstant.GWCS_FPKW_URL,
                        null,
                        "127.0.0.1",
@@ -2126,12 +2135,26 @@
                        success
                );
            } catch (Exception e) {
                log.error("", e);
                log.error("保存接口日志异常:{}", e.getMessage(), e);
            }
            if (success) break; // 成功就不再重试
            // 如果还有下一次重试,则等待
            if (attempt < maxRetry) {
                try {
                    Thread.sleep(retryDelay);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        }
        return success;
        return finalSuccess;
    }
    Map<String, Object> getHeaders(){
        Map<String,Object> headers = new HashMap<>();
        headers.put("digi-type","sync ");