Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dc89396

Browse filesBrowse files
kennywgxbinarywang
authored andcommitted
🎨 binarywang#1294 优化getAccessToken方法,解决并发时重复刷新的问题
1 parent 2faac86 commit dc89396
Copy full SHA for dc89396

File tree

Expand file treeCollapse file tree

4 files changed

+46
-0
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+46
-0
lines changed

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

Copy file name to clipboardExpand all lines: weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceHttpClientImpl.java
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
7474
Lock lock = config.getAccessTokenLock();
7575
lock.lock();
7676
try {
77+
if (!config.isAccessTokenExpired() && !forceRefresh) {
78+
return config.getAccessToken();
79+
}
7780
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());
7881
try {
7982
HttpGet httpGet = new HttpGet(url);

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

Copy file name to clipboardExpand all lines: weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceJoddHttpImpl.java
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
5959
Lock lock = config.getAccessTokenLock();
6060
lock.lock();
6161
try {
62+
if (!config.isAccessTokenExpired() && !forceRefresh) {
63+
return config.getAccessToken();
64+
}
6265
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());
6366

6467
HttpRequest request = HttpRequest.get(url);

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

Copy file name to clipboardExpand all lines: weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceOkHttpImpl.java
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
4848
Lock lock = config.getAccessTokenLock();
4949
lock.lock();
5050
try {
51+
if (!config.isAccessTokenExpired() && !forceRefresh) {
52+
return config.getAccessToken();
53+
}
5154
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());
5255

5356
Request request = new Request.Builder().url(url).get().build();

‎weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImplTest.java

Copy file name to clipboardExpand all lines: weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImplTest.java
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3+
import com.google.common.collect.Sets;
34
import com.google.inject.Inject;
5+
import java.util.Set;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
import java.util.concurrent.TimeUnit;
49
import me.chanjar.weixin.common.api.WxConsts;
510
import me.chanjar.weixin.common.bean.WxNetCheckResult;
611
import me.chanjar.weixin.common.error.WxErrorException;
@@ -14,6 +19,7 @@
1419
import java.util.Arrays;
1520

1621
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.testng.Assert.assertEquals;
1723
import static org.testng.Assert.assertFalse;
1824
import static org.testng.Assert.assertTrue;
1925

@@ -114,4 +120,35 @@ public void testShortUrl() throws WxErrorException {
114120
public void testShortUrl_with_exceptional_url() throws WxErrorException {
115121
this.wxService.shortUrl("http://www.baidu.com/test?redirect_count=1&access_token=123");
116122
}
123+
124+
@Test
125+
public void refreshAccessTokenDuplicatelyTest() throws InterruptedException {
126+
// 测试多线程刷新accessToken时是否重复刷新
127+
wxService.getWxMpConfigStorage().expireAccessToken();
128+
final Set<String> set = Sets.newConcurrentHashSet();
129+
Runnable r = new Runnable() {
130+
@Override
131+
public void run() {
132+
try {
133+
String accessToken = wxService.getAccessToken();
134+
set.add(accessToken);
135+
} catch (WxErrorException e) {
136+
e.printStackTrace();
137+
}
138+
}
139+
};
140+
141+
final int threadNumber = 10;
142+
ExecutorService executorService = Executors.newFixedThreadPool(threadNumber);
143+
for ( int i = 0; i < threadNumber; i++ ) {
144+
executorService.submit(r);
145+
}
146+
executorService.shutdown();
147+
boolean isTerminated = executorService.awaitTermination(15, TimeUnit.SECONDS);
148+
System.out.println("isTerminated: " + isTerminated);
149+
System.out.println("times of refreshing accessToken: " + set.size());
150+
151+
assertEquals(set.size(), 1);
152+
153+
}
117154
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.