|
@@ -62,38 +62,62 @@ public class KdYdDeliveryServiceImpl extends ServiceImpl<KdYdDeliveryMapper, KdY
|
|
|
String formInstId = UtilMap.getString(map, "formInstId");
|
|
String formInstId = UtilMap.getString(map, "formInstId");
|
|
|
String action = UtilMap.getString(map, "type");
|
|
String action = UtilMap.getString(map, "type");
|
|
|
|
|
|
|
|
|
|
+ // 处理特殊操作类型
|
|
|
// 处理特殊操作类型
|
|
// 处理特殊操作类型
|
|
|
if ("del".equals(action)) {
|
|
if ("del".equals(action)) {
|
|
|
String bm = UtilMap.getString(map, "bm");
|
|
String bm = UtilMap.getString(map, "bm");
|
|
|
|
|
|
|
|
- Map<String, String> headers = new HashMap<>();
|
|
|
|
|
- headers.put("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
|
- Map<String, Object> formParams = new HashMap<>();
|
|
|
|
|
- Map<String, Object> djbh = new HashMap<>();
|
|
|
|
|
- djbh.put("djbh", bm);
|
|
|
|
|
- result.put("data", djbh);
|
|
|
|
|
- String jsonStr = JSON.toJSONString(result);
|
|
|
|
|
- formParams.put("jsons", jsonStr);
|
|
|
|
|
-
|
|
|
|
|
- HttpResponse response = HttpRequest.post(kdHost+"/xk/dyzn/WsBasedyzn.asmx/hgdjydtbdeletefhtzd")
|
|
|
|
|
- .headerMap(headers, true)
|
|
|
|
|
- .form(formParams)
|
|
|
|
|
- .timeout(30000) // 30秒超时
|
|
|
|
|
- .execute();
|
|
|
|
|
-
|
|
|
|
|
- String responseBody = response.body();
|
|
|
|
|
-// System.out.println("responseBody===="+responseBody);
|
|
|
|
|
- log.info("删除返回值responseBody===="+responseBody);
|
|
|
|
|
- // 返回删除操作的格式
|
|
|
|
|
|
|
+ int maxRetries = 3;
|
|
|
|
|
+ boolean success = false;
|
|
|
|
|
+ String lastResponse = "";
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = 0; i < maxRetries; i++) {
|
|
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
|
|
+ headers.put("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
|
+ Map<String, Object> formParams = new HashMap<>();
|
|
|
|
|
+ Map<String, Object> requestBody = new HashMap<>();
|
|
|
|
|
+ Map<String, Object> djbh = new HashMap<>();
|
|
|
|
|
+ djbh.put("djbh", bm);
|
|
|
|
|
+ requestBody.put("data", djbh);
|
|
|
|
|
+ String jsonStr = JSON.toJSONString(requestBody);
|
|
|
|
|
+ formParams.put("jsons", jsonStr);
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = HttpRequest.post(kdHost + "/xk/dyzn/WsBasedyzn.asmx/hgdjydtbdeletefhtzd")
|
|
|
|
|
+ .headerMap(headers, true)
|
|
|
|
|
+ .form(formParams)
|
|
|
|
|
+ .timeout(30000)
|
|
|
|
|
+ .execute();
|
|
|
|
|
+
|
|
|
|
|
+ lastResponse = response.body();
|
|
|
|
|
+ log.info("第{}次删除返回值: {}", i + 1, lastResponse);
|
|
|
|
|
+
|
|
|
|
|
+ if (lastResponse != null && lastResponse.contains("\"code\":200")) {
|
|
|
|
|
+ success = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (i < maxRetries - 1) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ long delay = 2000 + (i * 1000);
|
|
|
|
|
+ log.info("等待 {} 秒后重试", delay / 1000);
|
|
|
|
|
+ Thread.sleep(delay);
|
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
result.put("table", "KD_YD_DELIVERY");
|
|
result.put("table", "KD_YD_DELIVERY");
|
|
|
result.put("action", "del");
|
|
result.put("action", "del");
|
|
|
- result.put("data", new HashMap<String, Object>() {{
|
|
|
|
|
- put("bm", bm); // 删除的主键或标识
|
|
|
|
|
- put("msg", "删除成功");
|
|
|
|
|
- }});
|
|
|
|
|
|
|
+ Map<String, Object> dataMap = new HashMap<>();
|
|
|
|
|
+ dataMap.put("bm", bm);
|
|
|
|
|
+ dataMap.put("msg", success ? "删除成功" : "删除失败,已重试" + maxRetries + "次");
|
|
|
|
|
+ if (!success) {
|
|
|
|
|
+ dataMap.put("error", lastResponse);
|
|
|
|
|
+ }
|
|
|
|
|
+ result.put("data", dataMap);
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
// 查询表单数据
|
|
// 查询表单数据
|
|
|
DDR_New ddrNew = ydClient.queryData(YDParam.builder()
|
|
DDR_New ddrNew = ydClient.queryData(YDParam.builder()
|
|
|
.formInstId(formInstId)
|
|
.formInstId(formInstId)
|
|
@@ -223,24 +247,74 @@ public class KdYdDeliveryServiceImpl extends ServiceImpl<KdYdDeliveryMapper, KdY
|
|
|
Map<String, Object> formParams = new HashMap<>();
|
|
Map<String, Object> formParams = new HashMap<>();
|
|
|
formParams.put("jsons", jsonStr);
|
|
formParams.put("jsons", jsonStr);
|
|
|
log.info("请求参数:" + formParams);
|
|
log.info("请求参数:" + formParams);
|
|
|
-// 发送请求
|
|
|
|
|
- HttpResponse response = null;
|
|
|
|
|
- if ("add".equals(action)) {
|
|
|
|
|
- response = HttpRequest.post(kdHost+"/xk/dyzn/WsBasedyzn.asmx/hgdjydtbcreatefhtzd")
|
|
|
|
|
- .headerMap(headers, true)
|
|
|
|
|
- .form(formParams)
|
|
|
|
|
- .timeout(30000) // 30秒超时
|
|
|
|
|
- .execute();
|
|
|
|
|
- } else {
|
|
|
|
|
- response = HttpRequest.post(kdHost+"/xk/dyzn/WsBasedyzn.asmx/")
|
|
|
|
|
- .headerMap(headers, true)
|
|
|
|
|
- .form(formParams)
|
|
|
|
|
- .timeout(30000) // 30秒超时
|
|
|
|
|
- .execute();
|
|
|
|
|
|
|
+// 发送请求(带重试功能)
|
|
|
|
|
+ String responseBody = null;
|
|
|
|
|
+ int maxRetries = 3;
|
|
|
|
|
+ boolean requestSuccess = false;
|
|
|
|
|
+
|
|
|
|
|
+ for (int retryCount = 0; retryCount < maxRetries; retryCount++) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (retryCount > 0) {
|
|
|
|
|
+ log.info("第{}次重试新增请求...", retryCount);
|
|
|
|
|
+ Thread.sleep(1500 * retryCount);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = null;
|
|
|
|
|
+ if ("add".equals(action)) {
|
|
|
|
|
+ response = HttpRequest.post(kdHost + "/xk/dyzn/WsBasedyzn.asmx/hgdjydtbcreatefhtzd")
|
|
|
|
|
+ .headerMap(headers, true)
|
|
|
|
|
+ .form(formParams)
|
|
|
|
|
+ .timeout(30000)
|
|
|
|
|
+ .execute();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ response = HttpRequest.post(kdHost + "/xk/dyzn/WsBasedyzn.asmx/")
|
|
|
|
|
+ .headerMap(headers, true)
|
|
|
|
|
+ .form(formParams)
|
|
|
|
|
+ .timeout(30000)
|
|
|
|
|
+ .execute();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ responseBody = response.body();
|
|
|
|
|
+ log.info("第{}次请求返回结果:{}", retryCount + 1, responseBody);
|
|
|
|
|
+
|
|
|
|
|
+ if (responseBody != null && !responseBody.isEmpty()) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ JSONArray jsonArray = JSONArray.parseArray(responseBody);
|
|
|
|
|
+ if (jsonArray != null && !jsonArray.isEmpty()) {
|
|
|
|
|
+ JSONObject jsonObject = jsonArray.getJSONObject(0);
|
|
|
|
|
+ Integer code = jsonObject.getInteger("code");
|
|
|
|
|
+
|
|
|
|
|
+ if (code != null && code == 200) {
|
|
|
|
|
+ requestSuccess = true;
|
|
|
|
|
+ log.info("新增发货通知单成功");
|
|
|
|
|
+ break;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.warn("请求返回错误码:{},第{}次尝试失败,将进行重试", code, retryCount + 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception parseException) {
|
|
|
|
|
+ log.error("解析返回结果异常:{}", parseException.getMessage());
|
|
|
|
|
+ if (retryCount == maxRetries - 1) {
|
|
|
|
|
+ throw new RuntimeException("解析返回结果失败", parseException);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.warn("返回响应为空,第{}次尝试失败", retryCount + 1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
|
|
+ log.error("重试被中断", e);
|
|
|
|
|
+ break;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("请求异常,第{}次尝试失败:{}", retryCount + 1, e.getMessage());
|
|
|
|
|
+ if (retryCount == maxRetries - 1) {
|
|
|
|
|
+ throw new RuntimeException("新增请求失败,已重试" + maxRetries + "次", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- String responseBody = response.body();
|
|
|
|
|
- log.info("返回结果responseBody:" + responseBody);
|
|
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
|
|
|
|
|
String msg = JSON.parseArray(responseBody)
|
|
String msg = JSON.parseArray(responseBody)
|
|
@@ -362,6 +436,8 @@ public class KdYdDeliveryServiceImpl extends ServiceImpl<KdYdDeliveryMapper, KdY
|
|
|
// 3. 添加处理后的子表数据
|
|
// 3. 添加处理后的子表数据
|
|
|
updateMap.put("tableField_mejnamfd", newSubTable);
|
|
updateMap.put("tableField_mejnamfd", newSubTable);
|
|
|
String sqrid = sqr.substring(2, sqr.length() - 2);
|
|
String sqrid = sqr.substring(2, sqr.length() - 2);
|
|
|
|
|
+ String xszgid = xszg.substring(2, xszg.length() - 2);
|
|
|
|
|
+ String finalId = (sqrid == null || sqrid.trim().isEmpty()) ? xszgid : sqrid;
|
|
|
System.out.println("sqrid==" + sqrid);
|
|
System.out.println("sqrid==" + sqrid);
|
|
|
// 创建新表单实例
|
|
// 创建新表单实例
|
|
|
try {
|
|
try {
|
|
@@ -369,7 +445,7 @@ public class KdYdDeliveryServiceImpl extends ServiceImpl<KdYdDeliveryMapper, KdY
|
|
|
YDParam.builder()
|
|
YDParam.builder()
|
|
|
.formUuid("FORM-FAE2575E112644ED914CAB4FEC9309F32AVR")
|
|
.formUuid("FORM-FAE2575E112644ED914CAB4FEC9309F32AVR")
|
|
|
.formDataJson(JSON.toJSONString(updateMap))
|
|
.formDataJson(JSON.toJSONString(updateMap))
|
|
|
- .userId(sqrid)
|
|
|
|
|
|
|
+ .userId(finalId)
|
|
|
.build(),
|
|
.build(),
|
|
|
YDConf.FORM_OPERATION.create
|
|
YDConf.FORM_OPERATION.create
|
|
|
);
|
|
);
|
|
@@ -378,7 +454,7 @@ public class KdYdDeliveryServiceImpl extends ServiceImpl<KdYdDeliveryMapper, KdY
|
|
|
log.error("创建新单据失败,dz=" + dz, e);
|
|
log.error("创建新单据失败,dz=" + dz, e);
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- Thread.sleep(2000);
|
|
|
|
|
|
|
+ Thread.sleep(3000);
|
|
|
} catch (InterruptedException e) {
|
|
} catch (InterruptedException e) {
|
|
|
Thread.currentThread().interrupt();
|
|
Thread.currentThread().interrupt();
|
|
|
log.error("线程被中断", e);
|
|
log.error("线程被中断", e);
|
|
@@ -440,98 +516,162 @@ public class KdYdDeliveryServiceImpl extends ServiceImpl<KdYdDeliveryMapper, KdY
|
|
|
|
|
|
|
|
public static BigDecimal toBigDecimal(String str) {
|
|
public static BigDecimal toBigDecimal(String str) {
|
|
|
if (str == null || str.trim().isEmpty()) {
|
|
if (str == null || str.trim().isEmpty()) {
|
|
|
- return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
|
|
+ return BigDecimal.ZERO.setScale(6, RoundingMode.HALF_UP);
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
return new BigDecimal(str.trim())
|
|
return new BigDecimal(str.trim())
|
|
|
- .setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
|
|
+ .setScale(6, RoundingMode.HALF_UP);
|
|
|
} catch (NumberFormatException e) {
|
|
} catch (NumberFormatException e) {
|
|
|
// 返回默认值而不是抛出异常,避免程序中断
|
|
// 返回默认值而不是抛出异常,避免程序中断
|
|
|
- return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
|
|
+ return BigDecimal.ZERO.setScale(6, RoundingMode.HALF_UP);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 重载方法,支持Number类型输入
|
|
* 重载方法,支持Number类型输入
|
|
|
*/
|
|
*/
|
|
|
- public static BigDecimal toBigDecimal(Number number) {
|
|
|
|
|
- if (number == null) {
|
|
|
|
|
- return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
- }
|
|
|
|
|
- return new BigDecimal(number.toString())
|
|
|
|
|
- .setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+// public static BigDecimal toBigDecimal(Number number) {
|
|
|
|
|
+// if (number == null) {
|
|
|
|
|
+// return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
+// }
|
|
|
|
|
+// return new BigDecimal(number.toString())
|
|
|
|
|
+// .setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
+// }
|
|
|
|
|
|
|
|
//自带重试的operateData方法
|
|
//自带重试的operateData方法
|
|
|
private Object retryOperateData(YDParam param, YDConf.FORM_OPERATION formOperation) {
|
|
private Object retryOperateData(YDParam param, YDConf.FORM_OPERATION formOperation) {
|
|
|
int maxAttempts = 3; // 尝试的最大次数
|
|
int maxAttempts = 3; // 尝试的最大次数
|
|
|
int attempt = 0;
|
|
int attempt = 0;
|
|
|
-
|
|
|
|
|
- Object result = null;
|
|
|
|
|
|
|
+ long baseWaitTime = 3000; // 基础等待时间3秒
|
|
|
|
|
|
|
|
while (attempt < maxAttempts) {
|
|
while (attempt < maxAttempts) {
|
|
|
try {
|
|
try {
|
|
|
- result = ydClient.operateData(param, formOperation);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ Object result = ydClient.operateData(param, formOperation);
|
|
|
|
|
+ // 成功调用后,如果有之前失败的重试日志,记录成功
|
|
|
|
|
+ if (attempt > 0) {
|
|
|
|
|
+ log.info("第{}次重试成功", attempt + 1);
|
|
|
|
|
+ }
|
|
|
return result;
|
|
return result;
|
|
|
} catch (McException e) {
|
|
} catch (McException e) {
|
|
|
- log.info("错误信息:{}",e.getMessage());
|
|
|
|
|
- if (e.getMessage().equals("The request has failed due to a temporary failure of the server.")) {
|
|
|
|
|
|
|
+ String errorMsg = e.getMessage();
|
|
|
|
|
+ log.warn("第{}次调用失败,错误信息:{}", attempt + 1, errorMsg);
|
|
|
|
|
+
|
|
|
|
|
+ // 判断是否是可重试的异常
|
|
|
|
|
+ if (isRetryableError(errorMsg)) {
|
|
|
attempt++;
|
|
attempt++;
|
|
|
- // 线程睡眠3秒
|
|
|
|
|
|
|
+ // 如果已达到最大尝试次数,不再重试,直接抛出异常
|
|
|
|
|
+ if (attempt >= maxAttempts) {
|
|
|
|
|
+ log.error("已达最大重试次数{}次,放弃重试", maxAttempts);
|
|
|
|
|
+ throw new RuntimeException("超出最大重试次数, param:" + JSONObject.toJSONString(param) + ", FORM_OPERATION:" + formOperation, e);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 线程睡眠,使用指数退避策略
|
|
|
try {
|
|
try {
|
|
|
- Thread.sleep(5000);
|
|
|
|
|
- log.info("尝试第{}次,param:{},FORM_OPERATION:{},异常信息:{}", attempt, JSONObject.toJSONString(param), formOperation,e.getMessage());
|
|
|
|
|
|
|
+ long waitTime = baseWaitTime * (long) Math.pow(2, attempt - 1);
|
|
|
|
|
+ log.info("等待{}ms后重试第{}次, param:{}, FORM_OPERATION:{}",
|
|
|
|
|
+ waitTime, attempt + 1, JSONObject.toJSONString(param), formOperation);
|
|
|
|
|
+ Thread.sleep(waitTime);
|
|
|
} catch (InterruptedException ie) {
|
|
} catch (InterruptedException ie) {
|
|
|
Thread.currentThread().interrupt(); // 重新设置中断状态
|
|
Thread.currentThread().interrupt(); // 重新设置中断状态
|
|
|
- System.err.println("Sleep interrupted: " + ie.getMessage());
|
|
|
|
|
|
|
+ log.error("重试睡眠被中断", ie);
|
|
|
|
|
+ throw new RuntimeException("重试过程被中断", ie);
|
|
|
}
|
|
}
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 不可重试的异常,直接抛出
|
|
|
|
|
+ log.error("发生不可重试的异常,直接失败: {}", errorMsg);
|
|
|
|
|
+ throw new RuntimeException("宜搭操作失败: " + errorMsg, e);
|
|
|
}
|
|
}
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- log.error("操作失败,param:{},FORM_OPERATION:{},异常信息:{}", JSONObject.toJSONString(param), formOperation,e.getMessage());
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // 非McException异常,直接抛出,不重试
|
|
|
|
|
+ log.error("操作失败,param:{},FORM_OPERATION:{},异常信息:{}",
|
|
|
|
|
+ JSONObject.toJSONString(param), formOperation, e.getMessage());
|
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- throw new RuntimeException("超出最大重试次数,param:"+JSONObject.toJSONString(param)+",FORM_OPERATION:{}"+formOperation);
|
|
|
|
|
|
|
+ // 理论上不会执行到这里,但为了安全保留
|
|
|
|
|
+ throw new RuntimeException("超出最大重试次数, param:" + JSONObject.toJSONString(param) + ", FORM_OPERATION:" + formOperation);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
//自带重试的queryData方法
|
|
//自带重试的queryData方法
|
|
|
private DDR_New retryQueryData(YDParam param, YDConf.FORM_QUERY formQuery) {
|
|
private DDR_New retryQueryData(YDParam param, YDConf.FORM_QUERY formQuery) {
|
|
|
int maxAttempts = 3; // 尝试的最大次数
|
|
int maxAttempts = 3; // 尝试的最大次数
|
|
|
int attempt = 0;
|
|
int attempt = 0;
|
|
|
-
|
|
|
|
|
- DDR_New result = null;
|
|
|
|
|
|
|
+ long baseWaitTime = 3000; // 基础等待时间3秒
|
|
|
|
|
|
|
|
while (attempt < maxAttempts) {
|
|
while (attempt < maxAttempts) {
|
|
|
try {
|
|
try {
|
|
|
- result = ydClient.queryData(param, formQuery);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ DDR_New result = ydClient.queryData(param, formQuery);
|
|
|
|
|
+ // 成功调用后,如果有之前失败的重试日志,记录成功
|
|
|
|
|
+ if (attempt > 0) {
|
|
|
|
|
+ log.info("第{}次重试查询成功", attempt + 1);
|
|
|
|
|
+ }
|
|
|
return result;
|
|
return result;
|
|
|
} catch (McException e) {
|
|
} catch (McException e) {
|
|
|
- log.info("错误信息:{}",e.getMessage());
|
|
|
|
|
- if (e.getMessage().equals("The request has failed due to a temporary failure of the server.")) {
|
|
|
|
|
|
|
+ String errorMsg = e.getMessage();
|
|
|
|
|
+ log.warn("第{}次查询失败,错误信息:{}", attempt + 1, errorMsg);
|
|
|
|
|
+
|
|
|
|
|
+ // 判断是否是可重试的异常
|
|
|
|
|
+ if (isRetryableError(errorMsg)) {
|
|
|
attempt++;
|
|
attempt++;
|
|
|
- // 线程睡眠3秒
|
|
|
|
|
|
|
+ // 如果已达到最大尝试次数,不再重试,直接抛出异常
|
|
|
|
|
+ if (attempt >= maxAttempts) {
|
|
|
|
|
+ log.error("已达最大重试次数{}次,放弃重试查询", maxAttempts);
|
|
|
|
|
+ throw new RuntimeException("超出最大重试次数, param:" + JSONObject.toJSONString(param) + ", FORM_QUERY:" + formQuery, e);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 线程睡眠,使用指数退避策略
|
|
|
try {
|
|
try {
|
|
|
- Thread.sleep(5000);
|
|
|
|
|
- log.info("尝试第{}次,param:{},FORM_QUERY:{},异常信息:{}", attempt, JSONObject.toJSONString(param), formQuery,e.getMessage());
|
|
|
|
|
|
|
+ long waitTime = baseWaitTime * (long) Math.pow(2, attempt - 1);
|
|
|
|
|
+ log.info("等待{}ms后重试第{}次查询, param:{}, FORM_QUERY:{}",
|
|
|
|
|
+ waitTime, attempt + 1, JSONObject.toJSONString(param), formQuery);
|
|
|
|
|
+ Thread.sleep(waitTime);
|
|
|
} catch (InterruptedException ie) {
|
|
} catch (InterruptedException ie) {
|
|
|
Thread.currentThread().interrupt(); // 重新设置中断状态
|
|
Thread.currentThread().interrupt(); // 重新设置中断状态
|
|
|
- System.err.println("Sleep interrupted: " + ie.getMessage());
|
|
|
|
|
|
|
+ log.error("重试查询睡眠被中断", ie);
|
|
|
|
|
+ throw new RuntimeException("重试查询过程被中断", ie);
|
|
|
}
|
|
}
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 不可重试的异常,直接抛出
|
|
|
|
|
+ log.error("发生不可重试的异常,查询直接失败: {}", errorMsg);
|
|
|
|
|
+ throw new RuntimeException("宜搭查询失败: " + errorMsg, e);
|
|
|
}
|
|
}
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- log.error("操作失败,param:{},FORM_QUERY:{},异常信息:{}", JSONObject.toJSONString(param), formQuery,e.getMessage());
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // 非McException异常,直接抛出,不重试
|
|
|
|
|
+ log.error("查询操作失败,param:{},FORM_QUERY:{},异常信息:{}",
|
|
|
|
|
+ JSONObject.toJSONString(param), formQuery, e.getMessage());
|
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- throw new RuntimeException("超出最大重试次数,param:"+JSONObject.toJSONString(param)+",FORM_QUERY:{}"+formQuery);
|
|
|
|
|
|
|
+ // 理论上不会执行到这里,但为了安全保留
|
|
|
|
|
+ throw new RuntimeException("超出最大重试次数, param:" + JSONObject.toJSONString(param) + ", FORM_QUERY:" + formQuery);
|
|
|
|
|
+ }
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 判断是否是可重试的错误
|
|
|
|
|
+ */
|
|
|
|
|
+ private boolean isRetryableError(String errorMsg) {
|
|
|
|
|
+ if (errorMsg == null) return false;
|
|
|
|
|
+
|
|
|
|
|
+ // 可重试的错误关键字列表(扩展了匹配范围)
|
|
|
|
|
+ String[] retryableKeywords = {
|
|
|
|
|
+ "temporary failure",
|
|
|
|
|
+ "未知异常",
|
|
|
|
|
+ "timeout",
|
|
|
|
|
+ "Timeout",
|
|
|
|
|
+ "Connection refused",
|
|
|
|
|
+ "Internal Server",
|
|
|
|
|
+ "服务不可用",
|
|
|
|
|
+ "Service Unavailable",
|
|
|
|
|
+ "Gateway Timeout",
|
|
|
|
|
+ "Bad Gateway"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ for (String keyword : retryableKeywords) {
|
|
|
|
|
+ if (errorMsg.contains(keyword)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|