Skip to content

Commit b8a9795

Browse files
committed
修复接口请求重试代码,避免无效等待
1 parent c0a3259 commit b8a9795

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,15 +540,20 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
540540
try {
541541
return executeInternal(executor, uri, data);
542542
} catch (WxErrorException e) {
543+
if (retryTimes + 1 > this.maxRetryTimes) {
544+
this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
545+
//最后一次重试失败后,直接抛出异常,不再等待
546+
throw new RuntimeException("微信服务端异常,超出重试次数");
547+
}
548+
543549
WxError error = e.getError();
544550
/*
545551
* -1 系统繁忙, 1000ms后重试
546552
*/
547553
if (error.getErrorCode() == -1) {
548554
int sleepMillis = this.retrySleepMillis * (1 << retryTimes);
549555
try {
550-
this.log.debug("微信系统繁忙,{}ms 后重试(第{}次)", sleepMillis,
551-
retryTimes + 1);
556+
this.log.debug("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
552557
Thread.sleep(sleepMillis);
553558
} catch (InterruptedException e1) {
554559
throw new RuntimeException(e1);
@@ -557,8 +562,9 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
557562
throw e;
558563
}
559564
}
560-
} while (++retryTimes < this.maxRetryTimes);
565+
} while (retryTimes++ < this.maxRetryTimes);
561566

567+
this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
562568
throw new RuntimeException("微信服务端异常,超出重试次数");
563569
}
564570

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package me.chanjar.weixin.cp.api;
22

3+
import me.chanjar.weixin.common.bean.result.WxError;
4+
import me.chanjar.weixin.common.exception.WxErrorException;
5+
import me.chanjar.weixin.common.util.http.RequestExecutor;
6+
import org.testng.annotations.DataProvider;
7+
import org.testng.annotations.Test;
8+
39
import java.util.concurrent.ExecutionException;
410
import java.util.concurrent.ExecutorService;
511
import java.util.concurrent.Executors;
612
import java.util.concurrent.Future;
713

8-
import org.testng.annotations.DataProvider;
9-
import org.testng.annotations.Test;
10-
11-
import me.chanjar.weixin.common.bean.result.WxError;
12-
import me.chanjar.weixin.common.exception.WxErrorException;
13-
import me.chanjar.weixin.common.util.http.RequestExecutor;
14-
1514
@Test
1615
public class WxCpBusyRetryTest {
1716

@@ -23,6 +22,7 @@ public Object[][] getService() {
2322
protected synchronized <T, E> T executeInternal(
2423
RequestExecutor<T, E> executor, String uri, E data)
2524
throws WxErrorException {
25+
this.log.info("Executed");
2626
WxError error = new WxError();
2727
error.setErrorCode(-1);
2828
throw new WxErrorException(error);

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,18 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
369369
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}",uri, data, result);
370370
return result;
371371
} catch (WxErrorException e) {
372+
if (retryTimes + 1 > this.maxRetryTimes) {
373+
this.log.warn("重试达到最大次数【{}】", maxRetryTimes);
374+
//最后一次重试失败后,直接抛出异常,不再等待
375+
throw new RuntimeException("微信服务端异常,超出重试次数");
376+
}
377+
372378
WxError error = e.getError();
373379
// -1 系统繁忙, 1000ms后重试
374380
if (error.getErrorCode() == -1) {
375381
int sleepMillis = this.retrySleepMillis * (1 << retryTimes);
376382
try {
377-
this.log.debug("微信系统繁忙,{}ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
383+
this.log.warn("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
378384
Thread.sleep(sleepMillis);
379385
} catch (InterruptedException e1) {
380386
throw new RuntimeException(e1);
@@ -383,8 +389,9 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
383389
throw e;
384390
}
385391
}
386-
} while (++retryTimes < this.maxRetryTimes);
392+
} while (retryTimes++ < this.maxRetryTimes);
387393

394+
this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
388395
throw new RuntimeException("微信服务端异常,超出重试次数");
389396
}
390397

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package me.chanjar.weixin.mp.api;
22

3-
import java.util.concurrent.ExecutionException;
4-
import java.util.concurrent.ExecutorService;
5-
import java.util.concurrent.Executors;
6-
import java.util.concurrent.Future;
7-
8-
import org.testng.annotations.DataProvider;
9-
import org.testng.annotations.Test;
10-
113
import me.chanjar.weixin.common.bean.result.WxError;
124
import me.chanjar.weixin.common.exception.WxErrorException;
135
import me.chanjar.weixin.common.util.http.RequestExecutor;
146
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
7+
import org.testng.annotations.DataProvider;
8+
import org.testng.annotations.Test;
9+
10+
import java.util.concurrent.ExecutionException;
11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.Executors;
13+
import java.util.concurrent.Future;
1514

1615
@Test
1716
public class WxMpBusyRetryTest {
@@ -24,6 +23,7 @@ public Object[][] getService() {
2423
protected synchronized <T, E> T executeInternal(
2524
RequestExecutor<T, E> executor, String uri, E data)
2625
throws WxErrorException {
26+
this.log.info("Executed");
2727
WxError error = new WxError();
2828
error.setErrorCode(-1);
2929
throw new WxErrorException(error);
@@ -32,9 +32,7 @@ protected synchronized <T, E> T executeInternal(
3232

3333
service.setMaxRetryTimes(3);
3434
service.setRetrySleepMillis(500);
35-
return new Object[][] {
36-
new Object[] { service }
37-
};
35+
return new Object[][] { { service } };
3836
}
3937

4038
@Test(dataProvider = "getService", expectedExceptions = RuntimeException.class)

0 commit comments

Comments
 (0)