mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-30 10:07:06 +08:00
🎨 #1820 优化更新getTicket方法,调整锁调用时机避免并发问题
* Fix:调整获取相关票据的锁处理时机 * Fix:更新票据,锁之后,再次检查是否有效,避免并发同时进入多次重置票据 Co-authored-by: weiwei.xing <weiwei.xing@nplusgroup.com>
This commit is contained in:
@ -32,13 +32,15 @@ public class WxMaJsapiServiceImpl implements WxMaJsapiService {
|
||||
|
||||
@Override
|
||||
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()) {
|
||||
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);
|
||||
@ -49,6 +51,7 @@ public class WxMaJsapiServiceImpl implements WxMaJsapiService {
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
return this.wxMaService.getWxMaConfig().getCardApiTicket();
|
||||
}
|
||||
|
||||
|
||||
@ -151,13 +151,15 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
|
||||
@Override
|
||||
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)) {
|
||||
Lock lock = this.getWxMpConfigStorage().getTicketLock(type);
|
||||
lock.lock();
|
||||
try {
|
||||
if (this.getWxMpConfigStorage().isTicketExpired(type)) {
|
||||
String responseContent = execute(SimpleGetRequestExecutor.create(this),
|
||||
GET_TICKET_URL.getUrl(this.getWxMpConfigStorage()) + type.getCode(), null);
|
||||
@ -169,6 +171,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
return this.getWxMpConfigStorage().getTicket(type);
|
||||
}
|
||||
|
||||
@ -48,14 +48,15 @@ public class WxMpCardServiceImpl implements WxMpCardService {
|
||||
@Override
|
||||
public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
|
||||
final TicketType type = TicketType.WX_CARD;
|
||||
Lock lock = getWxMpService().getWxMpConfigStorage().getTicketLock(type);
|
||||
lock.lock();
|
||||
try {
|
||||
|
||||
if (forceRefresh) {
|
||||
this.getWxMpService().getWxMpConfigStorage().expireTicket(type);
|
||||
}
|
||||
|
||||
if (this.getWxMpService().getWxMpConfigStorage().isTicketExpired(type)) {
|
||||
Lock lock = getWxMpService().getWxMpConfigStorage().getTicketLock(type);
|
||||
lock.lock();
|
||||
try {
|
||||
if (this.getWxMpService().getWxMpConfigStorage().isTicketExpired(type)) {
|
||||
String responseContent = this.wxMpService.execute(SimpleGetRequestExecutor
|
||||
.create(this.getWxMpService().getRequestHttp()), WxMpApiUrl.Card.CARD_GET_TICKET, null);
|
||||
@ -67,6 +68,7 @@ public class WxMpCardServiceImpl implements WxMpCardService {
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
return this.getWxMpService().getWxMpConfigStorage().getTicket(type);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user