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 requireSucessPut = false;
// 核心配置最大重试次数改为10次
private static final int MAX_RETRY_TIMES = 10;
// 重试间隔保持原有5秒
private static final long RETRY_SLEEP_MS = 5000L;
@Autowired
ISysParamService paramService;
@@ -391,23 +396,44 @@ public class TwoNDCSocketConnectionAutoRun extends AbstractAutoRunnable {
public static void write(byte[] b) {
try {
log.info("下发agv数据:" + Bytes2HexString(b));
System.out.println("下发agv数据:" + Bytes2HexString(b));
dos.write(b);
dos.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
int retryCount = 0; // 已重试次数0表示首次尝试
boolean isSuccess = false; // 写入是否成功
// 循环重试:未达到最大次数 且 未成功时继续
while (retryCount <= MAX_RETRY_TIMES && !isSuccess) {
try {
Thread.sleep(1000);
log.info("再次下发agv数据:" + Bytes2HexString(b));
System.out.println("再次下发agv数据:" + Bytes2HexString(b));
String hexData = 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.flush();
} catch (Exception e1) {
e1.printStackTrace();
}
isSuccess = true; // 写入成功,终止循环
} 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();
}
}
}
}
}