diff --git a/acs/nladmin-system/pom.xml b/acs/nladmin-system/pom.xml index 4e7942c59..aa9a18954 100644 --- a/acs/nladmin-system/pom.xml +++ b/acs/nladmin-system/pom.xml @@ -415,6 +415,11 @@ UserAgentUtils 1.21 + + + org.springframework.retry + spring-retry + diff --git a/acs/nladmin-system/src/main/java/org/nl/AppRun.java b/acs/nladmin-system/src/main/java/org/nl/AppRun.java index 2fa3edd13..cac91ec93 100644 --- a/acs/nladmin-system/src/main/java/org/nl/AppRun.java +++ b/acs/nladmin-system/src/main/java/org/nl/AppRun.java @@ -12,6 +12,7 @@ import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.retry.annotation.EnableRetry; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.web.bind.annotation.GetMapping; @@ -35,6 +36,7 @@ import org.springframework.web.bind.annotation.RestController; @EnableJpaAuditing(auditorAwareRef = "auditorAware") @EnableMethodCache(basePackages = "org.nl") @EnableCreateCacheAnnotation +@EnableRetry public class AppRun { public static void main(String[] args) { diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/oven_manipulator/OvenGantryManipulatorDeviceDriver.java b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/oven_manipulator/OvenGantryManipulatorDeviceDriver.java index ef881798a..e9b680cb0 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/oven_manipulator/OvenGantryManipulatorDeviceDriver.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/oven_manipulator/OvenGantryManipulatorDeviceDriver.java @@ -123,6 +123,9 @@ public class OvenGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver i String device_code; + //当前指令 + Instruction inst = null; + //0 无任务执行 1更新指令状态 2下发电气信号 3允许取货 允许放货 5放货完成 int now_steps_type = 0; String notCreateTaskMessage = ""; @@ -130,6 +133,11 @@ public class OvenGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver i String feedMessage = ""; + List getDeviceCodeList = null; + + List putDeviceCodeList = null; + + @Override public Device getDevice() { return this.device; @@ -181,6 +189,7 @@ public class OvenGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver i feedMessage = ""; notCreateInstMessage = ""; notCreateTaskMessage = ""; + inst = null; message = null; } logServer.deviceItemValue(this.device_code, "mode", String.valueOf(mode)); @@ -266,6 +275,9 @@ public class OvenGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver i } if (task != 0) { remark = remark + "当前上报任务号(task)应该为0,"; + if(ObjectUtil.isNotEmpty(this.inst)){ + this.inst = null; + } } if (requireSucess) { remark = remark + "请右击该图标,将请求任务复位标记(requireSucess)改为否。"; @@ -313,9 +325,14 @@ public class OvenGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver i } else { this.instruction_require_time = date; //抓取工位 - List getDeviceCodeList = this.getExtraDeviceCodes("get_device_code"); + if(ObjectUtil.isEmpty(getDeviceCodeList)){ + getDeviceCodeList = this.getExtraDeviceCodes("get_device_code"); + } //放货工位 - List putDeviceCodeList = this.getExtraDeviceCodes("put_device_code"); + if(ObjectUtil.isEmpty(putDeviceCodeList)){ + putDeviceCodeList = this.getExtraDeviceCodes("put_device_code"); + } + TaskDto task = null; for (int i = 0; i < getDeviceCodeList.size(); i++) { String startDeviceCode = getDeviceCodeList.get(i); @@ -387,6 +404,9 @@ public class OvenGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver i } String start_addr = startDevice.getExtraValue().get("address").toString(); String next_addr = nextDevice.getExtraValue().get("address").toString(); + logServer.deviceExecuteLog(device_code, "", "", "当前设备:" + device_code + ",下发指令:" + + instruction.getInstruction_code() + ",指令起点:" + instruction.getStart_device_code() + + ",指令终点:" + instruction.getNext_device_code()); this.writing("to_onset", start_addr); this.writing("to_target", next_addr); this.writing("to_task", instruction.getInstruction_code()); @@ -524,6 +544,9 @@ public class OvenGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver i } String start_addr = startDevice.getExtraValue().get("address").toString(); String next_addr = nextDevice.getExtraValue().get("address").toString(); + logServer.deviceExecuteLog(device_code, "", "", "当前设备:" + device_code + ",下发指令:" + + instdto.getInstruction_code() + ",指令起点:" + instdto.getStart_device_code() + + ",指令终点:" + instdto.getNext_device_code()); this.writing("to_onset", start_addr); this.writing("to_target", next_addr); this.writing("to_task", instdto.getInstruction_code()); @@ -782,7 +805,7 @@ public class OvenGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver i //更改任务状态 if (task > 0) { //inst_message - Instruction inst1 = instructionService.findByCodeFromCache(String.valueOf(task)); + Instruction inst1 = checkInst(); if (inst1 != null) { if (StrUtil.equals(inst1.getInstruction_status(), "0")) { inst1.setInstruction_status("1"); @@ -794,7 +817,7 @@ public class OvenGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver i //申请取货 if (mode == 3 && action == 1 && move == 0 && task > 0) { - Instruction inst2 = instructionService.findByCodeFromCache(String.valueOf(task)); + Instruction inst2 = checkInst(); if (ObjectUtil.isNotEmpty(inst2)) { String start_device_code = inst2.getStart_device_code(); Device device = deviceAppService.findDeviceByCode(start_device_code); @@ -863,7 +886,7 @@ public class OvenGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver i //取货完成关闭烘箱门 if (mode == 3 && action == 2 && move == 1 && task > 0) { - Instruction inst2 = instructionService.findByCodeFromCache(String.valueOf(task)); + Instruction inst2 = checkInst(); if (ObjectUtil.isNotEmpty(inst2)) { String start_device_code = inst2.getStart_device_code(); Device device = deviceAppService.findDeviceByCode(start_device_code); @@ -899,7 +922,7 @@ public class OvenGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver i //申请放货 if (mode == 3 && action == 3 && move == 1 && task > 0) { - Instruction instructionDto = instructionService.findByCodeFromCache(String.valueOf(task)); + Instruction instructionDto = checkInst(); String next_device_code = instructionDto.getNext_device_code(); Device nextDevice = deviceAppService.findDeviceByCode(next_device_code); HongXiangConveyorDeviceDriver hongXiangConveyorDeviceDriver; @@ -969,7 +992,7 @@ public class OvenGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver i //放货完成 if (mode == 3 && action == 4 && move == 0 && task > 0) { - Instruction inst2 = instructionService.findByCodeFromCache(String.valueOf(task)); + Instruction inst2 = checkInst(); if (inst2 != null) { if (StrUtil.equals(inst2.getInstruction_status(), "1")) { @@ -1050,4 +1073,21 @@ public class OvenGantryManipulatorDeviceDriver extends AbstractOpcDeviceDriver i } } + public Instruction checkInst(){ + if(ObjectUtil.isNotEmpty(this.inst)){ + if(this.task>0){ + if(this.inst.getInstruction_code().equals(String.valueOf(this.task))){ + return this.inst; + } else { + inst = instructionService.findByCodeFromCache(String.valueOf(task)); + return inst; + } + } + } else { + inst = instructionService.findByCodeFromCache(String.valueOf(task)); + return inst; + } + return null; + } + } diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/siemens_conveyor/SiemensConveyorDeviceDriver.java b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/siemens_conveyor/SiemensConveyorDeviceDriver.java index 19584c781..2885a683a 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/siemens_conveyor/SiemensConveyorDeviceDriver.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/siemens_conveyor/SiemensConveyorDeviceDriver.java @@ -235,6 +235,7 @@ public class SiemensConveyorDeviceDriver extends AbstractOpcDeviceDriver impleme message = null; if (move == 0) { inst_message = null; + inst = null; this.hand_barcode = null; this.clearWrite(); } @@ -528,7 +529,7 @@ public class SiemensConveyorDeviceDriver extends AbstractOpcDeviceDriver impleme jo.put("isError", this.getIserror()); jo.put("message", this.getMessage()); jo.put("hand_barcode", hand_barcode); - jo.put("barcode", this.getMove() == 0 ? null : instructionService.findByCodeFromCache(String.valueOf(task)) == null ? vehicle_code : instructionService.findByCodeFromCache(String.valueOf(task)).getVehicle_code()); + jo.put("barcode", this.getMove() == 0 ? null : checkInst() == null ? vehicle_code : checkInst().getVehicle_code()); jo.put("is_click", true); jo.put("requireSucess", requireSucess); jo.put("driver_type", "siemens_conveyor"); @@ -846,7 +847,7 @@ public class SiemensConveyorDeviceDriver extends AbstractOpcDeviceDriver impleme vehicle_code = hand_barcode; } else { if (task > 0) { - Instruction instruction = instructionService.findByCodeFromCache(String.valueOf(task)); + Instruction instruction = checkInst(); if (ObjectUtil.isEmpty(instruction)) { message = "申请捆扎电气设备任务号:" + task + "未找到对应指令"; logServer.deviceExecuteLog(device_code, "", "", "申请捆扎电气设备任务号:" + task + "未找到对应指令"); @@ -1111,7 +1112,7 @@ public class SiemensConveyorDeviceDriver extends AbstractOpcDeviceDriver impleme } else { this.instruction_update_time = date; - inst = instructionService.findByCodeFromCache(String.valueOf(task)); + inst = checkInst(); if (inst != null) { inst_message = "当前指令号:" + inst.getInstruction_code() + " " + inst.getStart_point_code() + "->" + inst.getNext_point_code() + " 载具号:" + inst.getVehicle_code(); vehicle_code = inst.getVehicle_code(); @@ -1139,4 +1140,21 @@ public class SiemensConveyorDeviceDriver extends AbstractOpcDeviceDriver impleme } } + + public Instruction checkInst(){ + if(ObjectUtil.isNotEmpty(this.inst)){ + if(this.task>0){ + if(this.inst.getInstruction_code().equals(String.valueOf(this.task))){ + return this.inst; + } else { + inst = instructionService.findByCodeFromCache(String.valueOf(task)); + return inst; + } + } + } else { + inst = instructionService.findByCodeFromCache(String.valueOf(task)); + return inst; + } + return null; + } } diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/ext/wms/RetryableUtil.java b/acs/nladmin-system/src/main/java/org/nl/acs/ext/wms/RetryableUtil.java new file mode 100644 index 000000000..73ce23348 --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/ext/wms/RetryableUtil.java @@ -0,0 +1,70 @@ +package org.nl.acs.ext.wms; + +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.nl.acs.ext.wms.service.AcsToWmsService; +import org.nl.modules.common.exception.BadRequestException; +import org.nl.modules.common.utils.RedisUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.retry.annotation.Backoff; +import org.springframework.retry.annotation.Recover; +import org.springframework.retry.annotation.Retryable; +import org.springframework.stereotype.Component; + +/** + * @author: geng by + * @createDate: 2023/7/6 + */ +@Component +@Slf4j +public class RetryableUtil { + private static ThreadLocal retryTimes = new ThreadLocal<>(); + @Autowired + private RedisUtils redisUtils; + @Autowired + private AcsToWmsService acsToWmsService; + + /** + * 只针对对接系统连接拒绝、连接超时进行重试机制 + * 如果系统连接成功 但是对方返回失败不进行重试 + * + * @param url + * @param param + */ + @Retryable(maxAttempts = 5, backoff = @Backoff(delay = 15000L, multiplier = 2)) + public void retryable(String url, String param, String token) { + HttpResponse httpResponse = null; + String respMessage = null; + try { + httpResponse = + HttpRequest + .post(url) + .header("Authorization", token).body(String.valueOf(param)) + .body(param) + .timeout(5000) + .execute(); + } catch (Exception e) { + respMessage = e.getMessage(); + } + if (retryTimes.get() == null) { + retryTimes.set(1); + } else { + retryTimes.set(retryTimes.get() + 1); + } + if (httpResponse == null) { + log.error("接口进行第{}次重试,请求路径:{},请求参数:{},响应参数:{}", retryTimes.get(), url, JSONObject.parse(param), respMessage); + throw new BadRequestException(url + "_" + param + "_" + retryTimes.get()); + } + retryTimes.remove(); + log.info("接口重试成功,请求路径:{},请求参数:{},重试次数:{}", url, JSONObject.parse(param), retryTimes.get()); + } + + @Recover + public void recover(RuntimeException e) { + retryTimes.remove(); + String[] excMessage = e.getMessage().split("_"); + log.error("请求路径:{},请求参数:{},已达到最大重试次数:{},停止重试!", excMessage[0], excMessage[1], excMessage[2]); + } +} diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/ext/wms/service/impl/AcsToWmsServiceImpl.java b/acs/nladmin-system/src/main/java/org/nl/acs/ext/wms/service/impl/AcsToWmsServiceImpl.java index 71e96405c..67d7dc93a 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/ext/wms/service/impl/AcsToWmsServiceImpl.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/ext/wms/service/impl/AcsToWmsServiceImpl.java @@ -14,6 +14,7 @@ import org.nl.acs.AcsConfig; import org.nl.acs.device.address.service.AddressService; import org.nl.acs.device.address.service.dto.AddressDto; import org.nl.acs.device.service.DeviceService; +import org.nl.acs.ext.wms.RetryableUtil; import org.nl.acs.ext.wms.data.*; import org.nl.acs.ext.wms.service.AcsToWmsService; import org.nl.acs.log.service.DeviceExecuteLogService; @@ -24,6 +25,8 @@ import org.slf4j.MDC; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.concurrent.CompletableFuture; + @Service @RequiredArgsConstructor @Slf4j @@ -43,6 +46,9 @@ public class AcsToWmsServiceImpl implements AcsToWmsService { @Autowired DeviceExecuteLogService logServer; + @Autowired + private RetryableUtil retryableUtil; + /*@Value("${acsTowms.token}")*/ public String token; @@ -120,6 +126,9 @@ public class AcsToWmsServiceImpl implements AcsToWmsService { //网络不通 // //System.out.println(msg); log.info("feedbackTaskStatusToWms-----输出参数{}", msg); + CompletableFuture.runAsync(() -> { + retryableUtil.retryable(wmsurl + methods_url, JSON.toJSONString(data), token); + }); }