opt:优化

This commit is contained in:
2026-06-01 11:17:05 +08:00
parent 552d48b758
commit fd4618788c

View File

@@ -62,6 +62,11 @@ public class TwoNDCSocketConnectionAutoRun extends AbstractAutoRunnable {
boolean requireSucessTake = false; boolean requireSucessTake = false;
boolean requireSucessPut = false; boolean requireSucessPut = false;
// 核心配置最大重试次数改为10次
private static final int MAX_RETRY_TIMES = 10;
// 重试间隔保持原有5秒
private static final long RETRY_SLEEP_MS = 5000L;
@Autowired @Autowired
ISysParamService paramService; ISysParamService paramService;
@@ -391,23 +396,44 @@ public class TwoNDCSocketConnectionAutoRun extends AbstractAutoRunnable {
public static void write(byte[] b) { public static void write(byte[] b) {
try { int retryCount = 0; // 已重试次数0表示首次尝试
log.info("下发agv数据:" + Bytes2HexString(b)); boolean isSuccess = false; // 写入是否成功
System.out.println("下发agv数据:" + Bytes2HexString(b));
dos.write(b); // 循环重试:未达到最大次数 且 未成功时继续
dos.flush(); while (retryCount <= MAX_RETRY_TIMES && !isSuccess) {
} catch (Exception e) {
// TODO Auto-generated catch block
try { try {
Thread.sleep(1000); String hexData = Bytes2HexString(b);
log.info("再次下发agv数据:" + Bytes2HexString(b)); // 日志区分首次和重试
System.out.println("再次下发agv数据:" + Bytes2HexString(b)); if (retryCount == 0) {
log.info("下发agv数据:" + hexData);
System.out.println("下发agv数据:" + hexData);
} else {
log.info("第{}次重试下发agv数据:{}", retryCount, hexData);
System.out.println("" + retryCount + "次重试下发agv数据:" + hexData);
}
// 核心写入逻辑
dos.write(b); dos.write(b);
dos.flush(); dos.flush();
} catch (Exception e1) { isSuccess = true; // 写入成功,终止循环
e1.printStackTrace(); } catch (Exception e) {
} retryCount++; // 重试次数+1
// 未到最大重试次数:休眠后继续
if (retryCount <= MAX_RETRY_TIMES) {
log.warn("第{}次下发失败,{}秒后重试,异常:", retryCount, RETRY_SLEEP_MS / 1000, e);
try {
Thread.sleep(RETRY_SLEEP_MS);
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
// 达到最大重试次数:记录最终失败日志
log.error("达到最大重试次数({}次)下发AGV数据失败", MAX_RETRY_TIMES, e);
e.printStackTrace();
}
}
} }
} }
} }