Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化getAccessToken方法,解决并发时重复刷新的问题 #1294

Merged
merged 1 commit into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
Lock lock = config.getAccessTokenLock();
lock.lock();
try {
if (!config.isAccessTokenExpired() && !forceRefresh) {
return config.getAccessToken();
}
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());
try {
HttpGet httpGet = new HttpGet(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
Lock lock = config.getAccessTokenLock();
lock.lock();
try {
if (!config.isAccessTokenExpired() && !forceRefresh) {
return config.getAccessToken();
}
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());

HttpRequest request = HttpRequest.get(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
Lock lock = config.getAccessTokenLock();
lock.lock();
try {
if (!config.isAccessTokenExpired() && !forceRefresh) {
return config.getAccessToken();
}
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());

Request request = new Request.Builder().url(url).get().build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package me.chanjar.weixin.mp.api.impl;

import com.google.common.collect.Sets;
import com.google.inject.Inject;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxNetCheckResult;
import me.chanjar.weixin.common.error.WxErrorException;
Expand All @@ -14,6 +19,7 @@
import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

Expand Down Expand Up @@ -114,4 +120,35 @@ public void testShortUrl() throws WxErrorException {
public void testShortUrl_with_exceptional_url() throws WxErrorException {
this.wxService.shortUrl("http://www.baidu.com/test?redirect_count=1&access_token=123");
}

@Test
public void refreshAccessTokenDuplicatelyTest() throws InterruptedException {
// 测试多线程刷新accessToken时是否重复刷新
wxService.getWxMpConfigStorage().expireAccessToken();
final Set<String> set = Sets.newConcurrentHashSet();
Runnable r = new Runnable() {
@Override
public void run() {
try {
String accessToken = wxService.getAccessToken();
set.add(accessToken);
} catch (WxErrorException e) {
e.printStackTrace();
}
}
};

final int threadNumber = 10;
ExecutorService executorService = Executors.newFixedThreadPool(threadNumber);
for ( int i = 0; i < threadNumber; i++ ) {
executorService.submit(r);
}
executorService.shutdown();
boolean isTerminated = executorService.awaitTermination(15, TimeUnit.SECONDS);
System.out.println("isTerminated: " + isTerminated);
System.out.println("times of refreshing accessToken: " + set.size());

assertEquals(set.size(), 1);

}
}