#943 优化公众号、小程序、企业微信模块获取accessToken关于lock的逻辑,缩小锁的范围,提高效率。

This commit is contained in:
Binary Wang
2019-03-03 10:56:56 +08:00
parent 3bf866e7ff
commit 8ef459c964
7 changed files with 183 additions and 201 deletions

View File

@@ -39,9 +39,11 @@ public class WxCpServiceApacheHttpClientImpl extends BaseWxCpServiceImpl<Closeab
@Override @Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException { public String getAccessToken(boolean forceRefresh) throws WxErrorException {
if (this.configStorage.isAccessTokenExpired() || forceRefresh) { if (!this.configStorage.isAccessTokenExpired() && !forceRefresh) {
return this.configStorage.getAccessToken();
}
synchronized (this.globalAccessTokenRefreshLock) { synchronized (this.globalAccessTokenRefreshLock) {
if (this.configStorage.isAccessTokenExpired()) {
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
+ "&corpid=" + this.configStorage.getCorpId() + "&corpid=" + this.configStorage.getCorpId()
+ "&corpsecret=" + this.configStorage.getCorpSecret(); + "&corpsecret=" + this.configStorage.getCorpSecret();
@@ -52,7 +54,7 @@ public class WxCpServiceApacheHttpClientImpl extends BaseWxCpServiceImpl<Closeab
.setProxy(this.httpProxy).build(); .setProxy(this.httpProxy).build();
httpGet.setConfig(config); httpGet.setConfig(config);
} }
String resultContent = null; String resultContent;
try (CloseableHttpClient httpclient = getRequestHttpClient(); try (CloseableHttpClient httpclient = getRequestHttpClient();
CloseableHttpResponse response = httpclient.execute(httpGet)) { CloseableHttpResponse response = httpclient.execute(httpGet)) {
resultContent = new BasicResponseHandler().handleResponse(response); resultContent = new BasicResponseHandler().handleResponse(response);
@@ -63,15 +65,13 @@ public class WxCpServiceApacheHttpClientImpl extends BaseWxCpServiceImpl<Closeab
if (error.getErrorCode() != 0) { if (error.getErrorCode() != 0) {
throw new WxErrorException(error); throw new WxErrorException(error);
} }
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
this.configStorage.updateAccessToken( this.configStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
accessToken.getAccessToken(), accessToken.getExpiresIn());
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
}
}
return this.configStorage.getAccessToken(); return this.configStorage.getAccessToken();
} }

View File

@@ -30,9 +30,11 @@ public class WxCpServiceJoddHttpImpl extends BaseWxCpServiceImpl<HttpConnectionP
@Override @Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException { public String getAccessToken(boolean forceRefresh) throws WxErrorException {
if (this.configStorage.isAccessTokenExpired() || forceRefresh) { if (!this.configStorage.isAccessTokenExpired() && !forceRefresh) {
return this.configStorage.getAccessToken();
}
synchronized (this.globalAccessTokenRefreshLock) { synchronized (this.globalAccessTokenRefreshLock) {
if (this.configStorage.isAccessTokenExpired()) {
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
+ "&corpid=" + this.configStorage.getCorpId() + "&corpid=" + this.configStorage.getCorpId()
+ "&corpsecret=" + this.configStorage.getCorpSecret(); + "&corpsecret=" + this.configStorage.getCorpSecret();
@@ -50,10 +52,7 @@ public class WxCpServiceJoddHttpImpl extends BaseWxCpServiceImpl<HttpConnectionP
throw new WxErrorException(error); throw new WxErrorException(error);
} }
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
this.configStorage.updateAccessToken( this.configStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
accessToken.getAccessToken(), accessToken.getExpiresIn());
}
}
} }
return this.configStorage.getAccessToken(); return this.configStorage.getAccessToken();
} }

View File

@@ -33,10 +33,11 @@ public class WxCpServiceOkHttpImpl extends BaseWxCpServiceImpl<OkHttpClient, OkH
@Override @Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException { public String getAccessToken(boolean forceRefresh) throws WxErrorException {
this.log.debug("WxCpServiceOkHttpImpl is running"); if (!this.configStorage.isAccessTokenExpired() && !forceRefresh) {
if (this.configStorage.isAccessTokenExpired() || forceRefresh) { return this.configStorage.getAccessToken();
}
synchronized (this.globalAccessTokenRefreshLock) { synchronized (this.globalAccessTokenRefreshLock) {
if (this.configStorage.isAccessTokenExpired()) {
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
+ "&corpid=" + this.configStorage.getCorpId() + "&corpid=" + this.configStorage.getCorpId()
+ "&corpsecret=" + this.configStorage.getCorpSecret(); + "&corpsecret=" + this.configStorage.getCorpSecret();
@@ -60,8 +61,6 @@ public class WxCpServiceOkHttpImpl extends BaseWxCpServiceImpl<OkHttpClient, OkH
this.configStorage.updateAccessToken(accessToken.getAccessToken(), this.configStorage.updateAccessToken(accessToken.getAccessToken(),
accessToken.getExpiresIn()); accessToken.getExpiresIn());
} }
}
}
return this.configStorage.getAccessToken(); return this.configStorage.getAccessToken();
} }

View File

@@ -1,31 +1,6 @@
package cn.binarywang.wx.miniapp.api.impl; package cn.binarywang.wx.miniapp.api.impl;
import java.io.IOException; import cn.binarywang.wx.miniapp.api.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import cn.binarywang.wx.miniapp.api.WxMaAnalysisService;
import cn.binarywang.wx.miniapp.api.WxMaCodeService;
import cn.binarywang.wx.miniapp.api.WxMaJsapiService;
import cn.binarywang.wx.miniapp.api.WxMaMediaService;
import cn.binarywang.wx.miniapp.api.WxMaMsgService;
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaRunService;
import cn.binarywang.wx.miniapp.api.WxMaSecCheckService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaSettingService;
import cn.binarywang.wx.miniapp.api.WxMaShareService;
import cn.binarywang.wx.miniapp.api.WxMaTemplateService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.config.WxMaConfig; import cn.binarywang.wx.miniapp.config.WxMaConfig;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
@@ -38,13 +13,21 @@ import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.DataUtils; import me.chanjar.weixin.common.util.DataUtils;
import me.chanjar.weixin.common.util.crypto.SHA1; import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.*;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import static cn.binarywang.wx.miniapp.constant.WxMaConstants.ErrorCode.*; import static cn.binarywang.wx.miniapp.constant.WxMaConstants.ErrorCode.*;
@@ -118,11 +101,13 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
@Override @Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException { public String getAccessToken(boolean forceRefresh) throws WxErrorException {
Lock lock = this.getWxMaConfig().getAccessTokenLock(); if (!this.getWxMaConfig().isAccessTokenExpired() && !forceRefresh) {
try { return this.getWxMaConfig().getAccessToken();
lock.lock(); }
if (this.getWxMaConfig().isAccessTokenExpired() || forceRefresh) { Lock lock = this.getWxMaConfig().getAccessTokenLock();
lock.lock();
try {
String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(), String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(),
this.getWxMaConfig().getSecret()); this.getWxMaConfig().getSecret());
try { try {
@@ -138,20 +123,19 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
throw new WxErrorException(error); throw new WxErrorException(error);
} }
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
this.getWxMaConfig().updateAccessToken(accessToken.getAccessToken(), this.getWxMaConfig().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
accessToken.getExpiresIn());
return this.getWxMaConfig().getAccessToken();
} finally { } finally {
httpGet.releaseConnection(); httpGet.releaseConnection();
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}
} finally { } finally {
lock.unlock(); lock.unlock();
} }
return this.getWxMaConfig().getAccessToken();
} }
@Override @Override

View File

@@ -63,10 +63,13 @@ public class WxMpServiceHttpClientImpl extends BaseWxMpServiceImpl<CloseableHttp
@Override @Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException { public String getAccessToken(boolean forceRefresh) throws WxErrorException {
if (!this.getWxMpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
return this.getWxMpConfigStorage().getAccessToken();
}
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
try {
lock.lock(); lock.lock();
if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) { try {
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL, String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret()); this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
try { try {
@@ -82,18 +85,16 @@ public class WxMpServiceHttpClientImpl extends BaseWxMpServiceImpl<CloseableHttp
throw new WxErrorException(error); throw new WxErrorException(error);
} }
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
accessToken.getExpiresIn()); return this.getWxMpConfigStorage().getAccessToken();
} finally { } finally {
httpGet.releaseConnection(); httpGet.releaseConnection();
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}
} finally { } finally {
lock.unlock(); lock.unlock();
} }
return this.getWxMpConfigStorage().getAccessToken();
} }
} }

View File

@@ -48,11 +48,13 @@ public class WxMpServiceJoddHttpImpl extends BaseWxMpServiceImpl<HttpConnectionP
@Override @Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException { public String getAccessToken(boolean forceRefresh) throws WxErrorException {
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); if (!this.getWxMpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
try { return this.getWxMpConfigStorage().getAccessToken();
lock.lock(); }
if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) { Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
lock.lock();
try {
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL, String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret()); this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
@@ -71,13 +73,12 @@ public class WxMpServiceJoddHttpImpl extends BaseWxMpServiceImpl<HttpConnectionP
throw new WxErrorException(error); throw new WxErrorException(error);
} }
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
accessToken.getExpiresIn());
} return this.getWxMpConfigStorage().getAccessToken();
} finally { } finally {
lock.unlock(); lock.unlock();
} }
return this.getWxMpConfigStorage().getAccessToken();
} }
} }

View File

@@ -36,12 +36,13 @@ public class WxMpServiceOkHttpImpl extends BaseWxMpServiceImpl<OkHttpClient, OkH
@Override @Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException { public String getAccessToken(boolean forceRefresh) throws WxErrorException {
this.log.debug("WxMpServiceOkHttpImpl is running"); if (!this.getWxMpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); return this.getWxMpConfigStorage().getAccessToken();
try { }
lock.lock();
if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) { Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
lock.lock();
try {
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL, String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret()); this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
@@ -53,21 +54,18 @@ public class WxMpServiceOkHttpImpl extends BaseWxMpServiceImpl<OkHttpClient, OkH
throw new WxErrorException(error); throw new WxErrorException(error);
} }
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
accessToken.getExpiresIn());
} return this.getWxMpConfigStorage().getAccessToken();
} catch (IOException e) { } catch (IOException e) {
this.log.error(e.getMessage(), e); throw new RuntimeException(e);
} finally { } finally {
lock.unlock(); lock.unlock();
} }
return this.getWxMpConfigStorage().getAccessToken();
} }
@Override @Override
public void initHttp() { public void initHttp() {
this.log.debug("WxMpServiceOkHttpImpl initHttp");
//设置代理 //设置代理
if (wxMpConfigStorage.getHttpProxyHost() != null && wxMpConfigStorage.getHttpProxyPort() > 0) { if (wxMpConfigStorage.getHttpProxyHost() != null && wxMpConfigStorage.getHttpProxyPort() > 0) {
httpProxy = OkHttpProxyInfo.httpProxy(wxMpConfigStorage.getHttpProxyHost(), httpProxy = OkHttpProxyInfo.httpProxy(wxMpConfigStorage.getHttpProxyHost(),