Kaynağa Gözat

修复接口请求重试代码,避免无效等待

Binary Wang 8 yıl önce
ebeveyn
işleme
b8a9795cf5

+ 9 - 3
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java

@@ -540,6 +540,12 @@ public class WxCpServiceImpl implements WxCpService {
       try {
         return executeInternal(executor, uri, data);
       } catch (WxErrorException e) {
+        if (retryTimes + 1 > this.maxRetryTimes) {
+          this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
+          //最后一次重试失败后,直接抛出异常,不再等待
+          throw new RuntimeException("微信服务端异常,超出重试次数");
+        }
+
         WxError error = e.getError();
         /*
          * -1 系统繁忙, 1000ms后重试
@@ -547,8 +553,7 @@ public class WxCpServiceImpl implements WxCpService {
         if (error.getErrorCode() == -1) {
           int sleepMillis = this.retrySleepMillis * (1 << retryTimes);
           try {
-            this.log.debug("微信系统繁忙,{}ms 后重试(第{}次)", sleepMillis,
-              retryTimes + 1);
+            this.log.debug("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
             Thread.sleep(sleepMillis);
           } catch (InterruptedException e1) {
             throw new RuntimeException(e1);
@@ -557,8 +562,9 @@ public class WxCpServiceImpl implements WxCpService {
           throw e;
         }
       }
-    } while (++retryTimes < this.maxRetryTimes);
+    } while (retryTimes++ < this.maxRetryTimes);
 
+    this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
     throw new RuntimeException("微信服务端异常,超出重试次数");
   }
 

+ 7 - 7
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpBusyRetryTest.java

@@ -1,17 +1,16 @@
 package me.chanjar.weixin.cp.api;
 
+import me.chanjar.weixin.common.bean.result.WxError;
+import me.chanjar.weixin.common.exception.WxErrorException;
+import me.chanjar.weixin.common.util.http.RequestExecutor;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 
-import org.testng.annotations.DataProvider;
-import org.testng.annotations.Test;
-
-import me.chanjar.weixin.common.bean.result.WxError;
-import me.chanjar.weixin.common.exception.WxErrorException;
-import me.chanjar.weixin.common.util.http.RequestExecutor;
-
 @Test
 public class WxCpBusyRetryTest {
 
@@ -23,6 +22,7 @@ public class WxCpBusyRetryTest {
       protected synchronized <T, E> T executeInternal(
           RequestExecutor<T, E> executor, String uri, E data)
           throws WxErrorException {
+        this.log.info("Executed");
         WxError error = new WxError();
         error.setErrorCode(-1);
         throw new WxErrorException(error);

+ 9 - 2
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java

@@ -369,12 +369,18 @@ public class WxMpServiceImpl implements WxMpService {
         this.log.debug("\n[URL]:  {}\n[PARAMS]: {}\n[RESPONSE]: {}",uri, data, result);
         return result;
       } catch (WxErrorException e) {
+        if (retryTimes + 1 > this.maxRetryTimes) {
+          this.log.warn("重试达到最大次数【{}】", maxRetryTimes);
+          //最后一次重试失败后,直接抛出异常,不再等待
+          throw new RuntimeException("微信服务端异常,超出重试次数");
+        }
+
         WxError error = e.getError();
         // -1 系统繁忙, 1000ms后重试
         if (error.getErrorCode() == -1) {
           int sleepMillis = this.retrySleepMillis * (1 << retryTimes);
           try {
-            this.log.debug("微信系统繁忙,{}ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
+            this.log.warn("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
             Thread.sleep(sleepMillis);
           } catch (InterruptedException e1) {
             throw new RuntimeException(e1);
@@ -383,8 +389,9 @@ public class WxMpServiceImpl implements WxMpService {
           throw e;
         }
       }
-    } while (++retryTimes < this.maxRetryTimes);
+    } while (retryTimes++ < this.maxRetryTimes);
 
+    this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
     throw new RuntimeException("微信服务端异常,超出重试次数");
   }
 

+ 9 - 11
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpBusyRetryTest.java

@@ -1,17 +1,16 @@
 package me.chanjar.weixin.mp.api;
 
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-
-import org.testng.annotations.DataProvider;
-import org.testng.annotations.Test;
-
 import me.chanjar.weixin.common.bean.result.WxError;
 import me.chanjar.weixin.common.exception.WxErrorException;
 import me.chanjar.weixin.common.util.http.RequestExecutor;
 import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
 
 @Test
 public class WxMpBusyRetryTest {
@@ -24,6 +23,7 @@ public class WxMpBusyRetryTest {
       protected synchronized <T, E> T executeInternal(
           RequestExecutor<T, E> executor, String uri, E data)
           throws WxErrorException {
+        this.log.info("Executed");
         WxError error = new WxError();
         error.setErrorCode(-1);
         throw new WxErrorException(error);
@@ -32,9 +32,7 @@ public class WxMpBusyRetryTest {
 
     service.setMaxRetryTimes(3);
     service.setRetrySleepMillis(500);
-    return new Object[][] {
-        new Object[] { service }
-    };
+    return new Object[][] { { service } };
   }
 
   @Test(dataProvider = "getService", expectedExceptions = RuntimeException.class)