|
|
@@ -90,6 +90,8 @@ public class HuaTaiCrawlerRequestImpl implements HuaTaiCrawlerRequest {
|
|
|
private static final String BASE_URL = "https://cspxp.pc.ehuatai.com";
|
|
|
private static final int MAX_RETRIES = 3;
|
|
|
private static final long RETRY_DELAY_MS = 3000;
|
|
|
+ /** 指数退避倍率:每次重试间隔 = RETRY_DELAY_MS * (BACKOFF_FACTOR ^ 重试次数) */
|
|
|
+ private static final int BACKOFF_FACTOR = 2;
|
|
|
private static final long SHORT_DELAY_MS = 100;
|
|
|
private static final String RISK_CODE = "1251";
|
|
|
private static final String XBCOM_CODE = "927";
|
|
|
@@ -221,9 +223,13 @@ public class HuaTaiCrawlerRequestImpl implements HuaTaiCrawlerRequest {
|
|
|
quoteResultsVo.setQuoteStatusEnum(InsOrderStatusEnum.QUOTE_FAIL);
|
|
|
// 带重试的获取报价编号、车型型号
|
|
|
Map<String, Object> xbResult = retry(() -> {
|
|
|
- try { return xbQuery(dto.getUsername(), cookie, car); }
|
|
|
+ try {
|
|
|
+ String freshCookie = cache.getCookie();
|
|
|
+ String useCookie = StringUtils.hasText(freshCookie) ? freshCookie : cookie;
|
|
|
+ return xbQuery(dto.getUsername(), useCookie, car);
|
|
|
+ }
|
|
|
catch (Exception e) { refreshLogin(dto); throw new Exception(e); }
|
|
|
- }, MAX_RETRIES, RETRY_DELAY_MS, "预报价获取登录信息");
|
|
|
+ }, MAX_RETRIES, RETRY_DELAY_MS, BACKOFF_FACTOR, "预报价获取登录信息");
|
|
|
|
|
|
String quotationNo = str(xbResult, "quotationNo");
|
|
|
String modelcodexh = StringUtils.isBlank(str(xbResult, "modelcodexh")) ? car.getCarModelNo() : str(xbResult, "modelcodexh");
|
|
|
@@ -1333,13 +1339,16 @@ public class HuaTaiCrawlerRequestImpl implements HuaTaiCrawlerRequest {
|
|
|
@FunctionalInterface
|
|
|
public interface RetryableTask<T> { T execute() throws Exception; }
|
|
|
|
|
|
- private <T> T retry(RetryableTask<T> task, int max, long delay, String name) {
|
|
|
+ private <T> T retry(RetryableTask<T> task, int max, long delay, int backoffFactor, String name) {
|
|
|
for (int i = 0; i < max; i++) {
|
|
|
try { return task.execute(); }
|
|
|
catch (Exception e) {
|
|
|
log.warn("{} 失败,第 {} 次重试", name, i + 1);
|
|
|
if (i + 1 >= max) throw new SystemException(name + " 失败,已重试 " + max + " 次", e.getMessage());
|
|
|
- sleep(delay);
|
|
|
+ // 指数退避:第1次 baseDelay,第2次 baseDelay*factor,第3次 baseDelay*factor^2 ...
|
|
|
+ long waitMs = delay * (long) Math.pow(Math.max(backoffFactor, 1), i);
|
|
|
+ log.info("{} 等待 {} ms 后重试", name, waitMs);
|
|
|
+ sleep(waitMs);
|
|
|
}
|
|
|
}
|
|
|
throw new SystemException(name + " 失败,已达到最大重试次数");
|