🎨 #1820 优化更新getTicket方法,调整锁调用时机避免并发问题

* Fix:调整获取相关票据的锁处理时机

* Fix:更新票据,锁之后,再次检查是否有效,避免并发同时进入多次重置票据

Co-authored-by: weiwei.xing <weiwei.xing@nplusgroup.com>
This commit is contained in:
jn老A
2020-10-25 12:59:52 +08:00
committed by GitHub
parent 449bda4009
commit 15e02d725c
3 changed files with 52 additions and 44 deletions

View File

@ -32,22 +32,25 @@ public class WxMaJsapiServiceImpl implements WxMaJsapiService {
@Override @Override
public String getCardApiTicket(boolean forceRefresh) throws WxErrorException { public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
Lock lock = this.wxMaService.getWxMaConfig().getCardApiTicketLock();
lock.lock();
try {
if (forceRefresh) {
this.wxMaService.getWxMaConfig().expireCardApiTicket();
}
if (this.wxMaService.getWxMaConfig().isCardApiTicketExpired()) { if (forceRefresh) {
String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=wx_card", null); this.wxMaService.getWxMaConfig().expireCardApiTicket();
JsonObject tmpJsonObject = GsonParser.parse(responseContent); }
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); if (this.wxMaService.getWxMaConfig().isCardApiTicketExpired()) {
this.wxMaService.getWxMaConfig().updateCardApiTicket(jsapiTicket, expiresInSeconds); Lock lock = this.wxMaService.getWxMaConfig().getCardApiTicketLock();
lock.lock();
try {
if (this.wxMaService.getWxMaConfig().isCardApiTicketExpired()) {
String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=wx_card", null);
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
this.wxMaService.getWxMaConfig().updateCardApiTicket(jsapiTicket, expiresInSeconds);
}
} finally {
lock.unlock();
} }
} finally {
lock.unlock();
} }
return this.wxMaService.getWxMaConfig().getCardApiTicket(); return this.wxMaService.getWxMaConfig().getCardApiTicket();
} }

View File

@ -151,23 +151,26 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
@Override @Override
public String getTicket(TicketType type, boolean forceRefresh) throws WxErrorException { public String getTicket(TicketType type, boolean forceRefresh) throws WxErrorException {
Lock lock = this.getWxMpConfigStorage().getTicketLock(type);
lock.lock();
try {
if (forceRefresh) {
this.getWxMpConfigStorage().expireTicket(type);
}
if (this.getWxMpConfigStorage().isTicketExpired(type)) { if (forceRefresh) {
String responseContent = execute(SimpleGetRequestExecutor.create(this), this.getWxMpConfigStorage().expireTicket(type);
GET_TICKET_URL.getUrl(this.getWxMpConfigStorage()) + type.getCode(), null); }
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); if (this.getWxMpConfigStorage().isTicketExpired(type)) {
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); Lock lock = this.getWxMpConfigStorage().getTicketLock(type);
this.getWxMpConfigStorage().updateTicket(type, jsapiTicket, expiresInSeconds); lock.lock();
try {
if (this.getWxMpConfigStorage().isTicketExpired(type)) {
String responseContent = execute(SimpleGetRequestExecutor.create(this),
GET_TICKET_URL.getUrl(this.getWxMpConfigStorage()) + type.getCode(), null);
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
this.getWxMpConfigStorage().updateTicket(type, jsapiTicket, expiresInSeconds);
}
} finally {
lock.unlock();
} }
} finally {
lock.unlock();
} }
return this.getWxMpConfigStorage().getTicket(type); return this.getWxMpConfigStorage().getTicket(type);

View File

@ -48,24 +48,26 @@ public class WxMpCardServiceImpl implements WxMpCardService {
@Override @Override
public String getCardApiTicket(boolean forceRefresh) throws WxErrorException { public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
final TicketType type = TicketType.WX_CARD; final TicketType type = TicketType.WX_CARD;
Lock lock = getWxMpService().getWxMpConfigStorage().getTicketLock(type);
lock.lock();
try {
if (forceRefresh) { if (forceRefresh) {
this.getWxMpService().getWxMpConfigStorage().expireTicket(type); this.getWxMpService().getWxMpConfigStorage().expireTicket(type);
} }
if (this.getWxMpService().getWxMpConfigStorage().isTicketExpired(type)) { if (this.getWxMpService().getWxMpConfigStorage().isTicketExpired(type)) {
String responseContent = this.wxMpService.execute(SimpleGetRequestExecutor Lock lock = getWxMpService().getWxMpConfigStorage().getTicketLock(type);
.create(this.getWxMpService().getRequestHttp()), WxMpApiUrl.Card.CARD_GET_TICKET, null); lock.lock();
JsonObject tmpJsonObject = GsonParser.parse(responseContent); try {
String cardApiTicket = tmpJsonObject.get("ticket").getAsString(); if (this.getWxMpService().getWxMpConfigStorage().isTicketExpired(type)) {
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); String responseContent = this.wxMpService.execute(SimpleGetRequestExecutor
this.getWxMpService().getWxMpConfigStorage().updateTicket(type, cardApiTicket, expiresInSeconds); .create(this.getWxMpService().getRequestHttp()), WxMpApiUrl.Card.CARD_GET_TICKET, null);
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
String cardApiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
this.getWxMpService().getWxMpConfigStorage().updateTicket(type, cardApiTicket, expiresInSeconds);
}
} finally {
lock.unlock();
} }
} finally {
lock.unlock();
} }
return this.getWxMpService().getWxMpConfigStorage().getTicket(type); return this.getWxMpService().getWxMpConfigStorage().getTicket(type);
} }