You've already forked FrameTour-BE
添加锁避免高并发冲突
This commit is contained in:
@ -8,6 +8,7 @@ import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class WxMpUtil {
|
||||
private static final String GET_WXA_CODE_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token=%s";
|
||||
@ -16,8 +17,11 @@ public class WxMpUtil {
|
||||
public static final String GET_USER_PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s";
|
||||
private static final Map<String, String> tokens = new ConcurrentHashMap<>();
|
||||
private static final Map<String, Date> expireTimes = new ConcurrentHashMap<>();
|
||||
private static final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
private static String getAccessToken(String appId, String appSecret) {
|
||||
lock.lock();
|
||||
try {
|
||||
if (expireTimes.containsKey(appId)) {
|
||||
Date expireTime = expireTimes.get(appId);
|
||||
if (expireTime.getTime() < System.currentTimeMillis()) {
|
||||
@ -35,6 +39,9 @@ public class WxMpUtil {
|
||||
expireTimes.put(appId, expireTime);
|
||||
return token;
|
||||
});
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public static void generateWXAQRCode(String appId, String appSecret, String envVersion, String path, String filePath) throws Exception {
|
||||
|
Reference in New Issue
Block a user