mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-09-20 18:48:13 +08:00
#943 优化公众号、小程序、企业微信模块获取accessToken关于lock的逻辑,缩小锁的范围,提高效率。
This commit is contained in:
@@ -39,37 +39,37 @@ 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) {
|
||||||
synchronized (this.globalAccessTokenRefreshLock) {
|
return this.configStorage.getAccessToken();
|
||||||
if (this.configStorage.isAccessTokenExpired()) {
|
}
|
||||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
|
|
||||||
+ "&corpid=" + this.configStorage.getCorpId()
|
synchronized (this.globalAccessTokenRefreshLock) {
|
||||||
+ "&corpsecret=" + this.configStorage.getCorpSecret();
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
|
||||||
try {
|
+ "&corpid=" + this.configStorage.getCorpId()
|
||||||
HttpGet httpGet = new HttpGet(url);
|
+ "&corpsecret=" + this.configStorage.getCorpSecret();
|
||||||
if (this.httpProxy != null) {
|
try {
|
||||||
RequestConfig config = RequestConfig.custom()
|
HttpGet httpGet = new HttpGet(url);
|
||||||
.setProxy(this.httpProxy).build();
|
if (this.httpProxy != null) {
|
||||||
httpGet.setConfig(config);
|
RequestConfig config = RequestConfig.custom()
|
||||||
}
|
.setProxy(this.httpProxy).build();
|
||||||
String resultContent = null;
|
httpGet.setConfig(config);
|
||||||
try (CloseableHttpClient httpclient = getRequestHttpClient();
|
|
||||||
CloseableHttpResponse response = httpclient.execute(httpGet)) {
|
|
||||||
resultContent = new BasicResponseHandler().handleResponse(response);
|
|
||||||
} finally {
|
|
||||||
httpGet.releaseConnection();
|
|
||||||
}
|
|
||||||
WxError error = WxError.fromJson(resultContent, WxType.CP);
|
|
||||||
if (error.getErrorCode() != 0) {
|
|
||||||
throw new WxErrorException(error);
|
|
||||||
}
|
|
||||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
|
||||||
this.configStorage.updateAccessToken(
|
|
||||||
accessToken.getAccessToken(), accessToken.getExpiresIn());
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
String resultContent;
|
||||||
|
try (CloseableHttpClient httpclient = getRequestHttpClient();
|
||||||
|
CloseableHttpResponse response = httpclient.execute(httpGet)) {
|
||||||
|
resultContent = new BasicResponseHandler().handleResponse(response);
|
||||||
|
} finally {
|
||||||
|
httpGet.releaseConnection();
|
||||||
|
}
|
||||||
|
WxError error = WxError.fromJson(resultContent, WxType.CP);
|
||||||
|
if (error.getErrorCode() != 0) {
|
||||||
|
throw new WxErrorException(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
||||||
|
this.configStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this.configStorage.getAccessToken();
|
return this.configStorage.getAccessToken();
|
||||||
|
@@ -30,30 +30,29 @@ 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) {
|
||||||
synchronized (this.globalAccessTokenRefreshLock) {
|
return this.configStorage.getAccessToken();
|
||||||
if (this.configStorage.isAccessTokenExpired()) {
|
}
|
||||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
|
|
||||||
+ "&corpid=" + this.configStorage.getCorpId()
|
|
||||||
+ "&corpsecret=" + this.configStorage.getCorpSecret();
|
|
||||||
|
|
||||||
HttpRequest request = HttpRequest.get(url);
|
synchronized (this.globalAccessTokenRefreshLock) {
|
||||||
if (this.httpProxy != null) {
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
|
||||||
httpClient.useProxy(this.httpProxy);
|
+ "&corpid=" + this.configStorage.getCorpId()
|
||||||
}
|
+ "&corpsecret=" + this.configStorage.getCorpSecret();
|
||||||
request.withConnectionProvider(httpClient);
|
|
||||||
HttpResponse response = request.send();
|
|
||||||
|
|
||||||
String resultContent = response.bodyText();
|
HttpRequest request = HttpRequest.get(url);
|
||||||
WxError error = WxError.fromJson(resultContent, WxType.CP);
|
if (this.httpProxy != null) {
|
||||||
if (error.getErrorCode() != 0) {
|
httpClient.useProxy(this.httpProxy);
|
||||||
throw new WxErrorException(error);
|
|
||||||
}
|
|
||||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
|
||||||
this.configStorage.updateAccessToken(
|
|
||||||
accessToken.getAccessToken(), accessToken.getExpiresIn());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
request.withConnectionProvider(httpClient);
|
||||||
|
HttpResponse response = request.send();
|
||||||
|
|
||||||
|
String resultContent = response.bodyText();
|
||||||
|
WxError error = WxError.fromJson(resultContent, WxType.CP);
|
||||||
|
if (error.getErrorCode() != 0) {
|
||||||
|
throw new WxErrorException(error);
|
||||||
|
}
|
||||||
|
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
||||||
|
this.configStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||||
}
|
}
|
||||||
return this.configStorage.getAccessToken();
|
return this.configStorage.getAccessToken();
|
||||||
}
|
}
|
||||||
|
@@ -33,34 +33,33 @@ 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) {
|
}
|
||||||
if (this.configStorage.isAccessTokenExpired()) {
|
|
||||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
|
|
||||||
+ "&corpid=" + this.configStorage.getCorpId()
|
|
||||||
+ "&corpsecret=" + this.configStorage.getCorpSecret();
|
|
||||||
//得到httpClient
|
|
||||||
OkHttpClient client = getRequestHttpClient();
|
|
||||||
//请求的request
|
|
||||||
Request request = new Request.Builder().url(url).get().build();
|
|
||||||
String resultContent = null;
|
|
||||||
try {
|
|
||||||
Response response = client.newCall(request).execute();
|
|
||||||
resultContent = response.body().string();
|
|
||||||
} catch (IOException e) {
|
|
||||||
this.log.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
|
|
||||||
WxError error = WxError.fromJson(resultContent, WxType.CP);
|
synchronized (this.globalAccessTokenRefreshLock) {
|
||||||
if (error.getErrorCode() != 0) {
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
|
||||||
throw new WxErrorException(error);
|
+ "&corpid=" + this.configStorage.getCorpId()
|
||||||
}
|
+ "&corpsecret=" + this.configStorage.getCorpSecret();
|
||||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
//得到httpClient
|
||||||
this.configStorage.updateAccessToken(accessToken.getAccessToken(),
|
OkHttpClient client = getRequestHttpClient();
|
||||||
accessToken.getExpiresIn());
|
//请求的request
|
||||||
|
Request request = new Request.Builder().url(url).get().build();
|
||||||
|
String resultContent = null;
|
||||||
|
try {
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
resultContent = response.body().string();
|
||||||
|
} catch (IOException e) {
|
||||||
|
this.log.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
WxError error = WxError.fromJson(resultContent, WxType.CP);
|
||||||
|
if (error.getErrorCode() != 0) {
|
||||||
|
throw new WxErrorException(error);
|
||||||
|
}
|
||||||
|
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
||||||
|
this.configStorage.updateAccessToken(accessToken.getAccessToken(),
|
||||||
|
accessToken.getExpiresIn());
|
||||||
}
|
}
|
||||||
return this.configStorage.getAccessToken();
|
return this.configStorage.getAccessToken();
|
||||||
}
|
}
|
||||||
|
@@ -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,40 +101,41 @@ 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();
|
||||||
String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(),
|
lock.lock();
|
||||||
this.getWxMaConfig().getSecret());
|
try {
|
||||||
try {
|
String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(),
|
||||||
HttpGet httpGet = new HttpGet(url);
|
this.getWxMaConfig().getSecret());
|
||||||
if (this.getRequestHttpProxy() != null) {
|
try {
|
||||||
RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
|
HttpGet httpGet = new HttpGet(url);
|
||||||
httpGet.setConfig(config);
|
if (this.getRequestHttpProxy() != null) {
|
||||||
}
|
RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
|
||||||
try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) {
|
httpGet.setConfig(config);
|
||||||
String resultContent = new BasicResponseHandler().handleResponse(response);
|
|
||||||
WxError error = WxError.fromJson(resultContent);
|
|
||||||
if (error.getErrorCode() != 0) {
|
|
||||||
throw new WxErrorException(error);
|
|
||||||
}
|
|
||||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
|
||||||
this.getWxMaConfig().updateAccessToken(accessToken.getAccessToken(),
|
|
||||||
accessToken.getExpiresIn());
|
|
||||||
} finally {
|
|
||||||
httpGet.releaseConnection();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
|
try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) {
|
||||||
|
String resultContent = new BasicResponseHandler().handleResponse(response);
|
||||||
|
WxError error = WxError.fromJson(resultContent);
|
||||||
|
if (error.getErrorCode() != 0) {
|
||||||
|
throw new WxErrorException(error);
|
||||||
|
}
|
||||||
|
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
||||||
|
this.getWxMaConfig().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||||
|
|
||||||
|
return this.getWxMaConfig().getAccessToken();
|
||||||
|
} finally {
|
||||||
|
httpGet.releaseConnection();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.getWxMaConfig().getAccessToken();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -63,37 +63,38 @@ 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();
|
||||||
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
lock.lock();
|
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
|
||||||
if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) {
|
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
||||||
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
|
try {
|
||||||
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
HttpGet httpGet = new HttpGet(url);
|
||||||
try {
|
if (this.getRequestHttpProxy() != null) {
|
||||||
HttpGet httpGet = new HttpGet(url);
|
RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
|
||||||
if (this.getRequestHttpProxy() != null) {
|
httpGet.setConfig(config);
|
||||||
RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
|
|
||||||
httpGet.setConfig(config);
|
|
||||||
}
|
|
||||||
try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) {
|
|
||||||
String resultContent = new BasicResponseHandler().handleResponse(response);
|
|
||||||
WxError error = WxError.fromJson(resultContent, WxType.MP);
|
|
||||||
if (error.getErrorCode() != 0) {
|
|
||||||
throw new WxErrorException(error);
|
|
||||||
}
|
|
||||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
|
||||||
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(),
|
|
||||||
accessToken.getExpiresIn());
|
|
||||||
} finally {
|
|
||||||
httpGet.releaseConnection();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
|
try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) {
|
||||||
|
String resultContent = new BasicResponseHandler().handleResponse(response);
|
||||||
|
WxError error = WxError.fromJson(resultContent, WxType.MP);
|
||||||
|
if (error.getErrorCode() != 0) {
|
||||||
|
throw new WxErrorException(error);
|
||||||
|
}
|
||||||
|
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
||||||
|
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||||
|
return this.getWxMpConfigStorage().getAccessToken();
|
||||||
|
} finally {
|
||||||
|
httpGet.releaseConnection();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
return this.getWxMpConfigStorage().getAccessToken();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -48,36 +48,37 @@ public class WxMpServiceJoddHttpImpl extends BaseWxMpServiceImpl<HttpConnectionP
|
|||||||
|
|
||||||
@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();
|
||||||
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
lock.lock();
|
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
|
||||||
|
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
||||||
|
|
||||||
if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) {
|
HttpRequest request = HttpRequest.get(url);
|
||||||
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
|
|
||||||
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
|
||||||
|
|
||||||
HttpRequest request = HttpRequest.get(url);
|
if (this.getRequestHttpProxy() != null) {
|
||||||
|
SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider();
|
||||||
|
provider.useProxy(getRequestHttpProxy());
|
||||||
|
|
||||||
if (this.getRequestHttpProxy() != null) {
|
request.withConnectionProvider(provider);
|
||||||
SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider();
|
|
||||||
provider.useProxy(getRequestHttpProxy());
|
|
||||||
|
|
||||||
request.withConnectionProvider(provider);
|
|
||||||
}
|
|
||||||
HttpResponse response = request.send();
|
|
||||||
String resultContent = response.bodyText();
|
|
||||||
WxError error = WxError.fromJson(resultContent, WxType.MP);
|
|
||||||
if (error.getErrorCode() != 0) {
|
|
||||||
throw new WxErrorException(error);
|
|
||||||
}
|
|
||||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
|
||||||
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(),
|
|
||||||
accessToken.getExpiresIn());
|
|
||||||
}
|
}
|
||||||
|
HttpResponse response = request.send();
|
||||||
|
String resultContent = response.bodyText();
|
||||||
|
WxError error = WxError.fromJson(resultContent, WxType.MP);
|
||||||
|
if (error.getErrorCode() != 0) {
|
||||||
|
throw new WxErrorException(error);
|
||||||
|
}
|
||||||
|
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
||||||
|
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||||
|
|
||||||
|
return this.getWxMpConfigStorage().getAccessToken();
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
return this.getWxMpConfigStorage().getAccessToken();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -36,38 +36,36 @@ 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) {
|
||||||
|
return this.getWxMpConfigStorage().getAccessToken();
|
||||||
|
}
|
||||||
|
|
||||||
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
|
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
|
||||||
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
lock.lock();
|
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
|
||||||
|
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
||||||
|
|
||||||
if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) {
|
Request request = new Request.Builder().url(url).get().build();
|
||||||
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
|
Response response = getRequestHttpClient().newCall(request).execute();
|
||||||
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
String resultContent = response.body().string();
|
||||||
|
WxError error = WxError.fromJson(resultContent, WxType.MP);
|
||||||
Request request = new Request.Builder().url(url).get().build();
|
if (error.getErrorCode() != 0) {
|
||||||
Response response = getRequestHttpClient().newCall(request).execute();
|
throw new WxErrorException(error);
|
||||||
String resultContent = response.body().string();
|
|
||||||
WxError error = WxError.fromJson(resultContent, WxType.MP);
|
|
||||||
if (error.getErrorCode() != 0) {
|
|
||||||
throw new WxErrorException(error);
|
|
||||||
}
|
|
||||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
|
||||||
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(),
|
|
||||||
accessToken.getExpiresIn());
|
|
||||||
}
|
}
|
||||||
|
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
||||||
|
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), 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(),
|
||||||
|
Reference in New Issue
Block a user