From b8a9795cf5a67711ec77c56798e296a9b3a1fa4f Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Thu, 15 Dec 2016 15:23:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8E=A5=E5=8F=A3=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E9=87=8D=E8=AF=95=E4=BB=A3=E7=A0=81=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E6=97=A0=E6=95=88=E7=AD=89=E5=BE=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/cp/api/WxCpServiceImpl.java | 12 ++++++++--- .../weixin/cp/api/WxCpBusyRetryTest.java | 14 ++++++------- .../weixin/mp/api/impl/WxMpServiceImpl.java | 11 ++++++++-- .../weixin/mp/api/WxMpBusyRetryTest.java | 20 +++++++++---------- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java index d44bbf5f..734593d4 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java +++ b/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("微信服务端异常,超出重试次数"); } diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpBusyRetryTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpBusyRetryTest.java index b3f96b1b..e1b005bd 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpBusyRetryTest.java +++ b/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 executeInternal( RequestExecutor executor, String uri, E data) throws WxErrorException { + this.log.info("Executed"); WxError error = new WxError(); error.setErrorCode(-1); throw new WxErrorException(error); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java index 3c602325..62b363a2 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java +++ b/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("微信服务端异常,超出重试次数"); } diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpBusyRetryTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpBusyRetryTest.java index a302cc2f..8d46eba7 100644 --- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpBusyRetryTest.java +++ b/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 executeInternal( RequestExecutor 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)