Skip to content

Commit 15e02d7

Browse files
🎨 #1820 优化更新getTicket方法,调整锁调用时机避免并发问题
* Fix:调整获取相关票据的锁处理时机 * Fix:更新票据,锁之后,再次检查是否有效,避免并发同时进入多次重置票据 Co-authored-by: weiwei.xing <[email protected]>
1 parent 449bda4 commit 15e02d7

File tree

3 files changed

+52
-44
lines changed

3 files changed

+52
-44
lines changed

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaJsapiServiceImpl.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@ public String getCardApiTicket() throws WxErrorException {
3232

3333
@Override
3434
public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
35-
Lock lock = this.wxMaService.getWxMaConfig().getCardApiTicketLock();
36-
lock.lock();
37-
try {
38-
if (forceRefresh) {
39-
this.wxMaService.getWxMaConfig().expireCardApiTicket();
40-
}
4135

42-
if (this.wxMaService.getWxMaConfig().isCardApiTicketExpired()) {
43-
String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=wx_card", null);
44-
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
45-
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
46-
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
47-
this.wxMaService.getWxMaConfig().updateCardApiTicket(jsapiTicket, expiresInSeconds);
36+
if (forceRefresh) {
37+
this.wxMaService.getWxMaConfig().expireCardApiTicket();
38+
}
39+
40+
if (this.wxMaService.getWxMaConfig().isCardApiTicketExpired()) {
41+
Lock lock = this.wxMaService.getWxMaConfig().getCardApiTicketLock();
42+
lock.lock();
43+
try {
44+
if (this.wxMaService.getWxMaConfig().isCardApiTicketExpired()) {
45+
String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=wx_card", null);
46+
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
47+
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
48+
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
49+
this.wxMaService.getWxMaConfig().updateCardApiTicket(jsapiTicket, expiresInSeconds);
50+
}
51+
} finally {
52+
lock.unlock();
4853
}
49-
} finally {
50-
lock.unlock();
5154
}
5255
return this.wxMaService.getWxMaConfig().getCardApiTicket();
5356
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,23 +151,26 @@ public String getTicket(TicketType type) throws WxErrorException {
151151

152152
@Override
153153
public String getTicket(TicketType type, boolean forceRefresh) throws WxErrorException {
154-
Lock lock = this.getWxMpConfigStorage().getTicketLock(type);
155-
lock.lock();
156-
try {
157-
if (forceRefresh) {
158-
this.getWxMpConfigStorage().expireTicket(type);
159-
}
160154

161-
if (this.getWxMpConfigStorage().isTicketExpired(type)) {
162-
String responseContent = execute(SimpleGetRequestExecutor.create(this),
163-
GET_TICKET_URL.getUrl(this.getWxMpConfigStorage()) + type.getCode(), null);
164-
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
165-
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
166-
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
167-
this.getWxMpConfigStorage().updateTicket(type, jsapiTicket, expiresInSeconds);
155+
if (forceRefresh) {
156+
this.getWxMpConfigStorage().expireTicket(type);
157+
}
158+
159+
if (this.getWxMpConfigStorage().isTicketExpired(type)) {
160+
Lock lock = this.getWxMpConfigStorage().getTicketLock(type);
161+
lock.lock();
162+
try {
163+
if (this.getWxMpConfigStorage().isTicketExpired(type)) {
164+
String responseContent = execute(SimpleGetRequestExecutor.create(this),
165+
GET_TICKET_URL.getUrl(this.getWxMpConfigStorage()) + type.getCode(), null);
166+
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
167+
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
168+
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
169+
this.getWxMpConfigStorage().updateTicket(type, jsapiTicket, expiresInSeconds);
170+
}
171+
} finally {
172+
lock.unlock();
168173
}
169-
} finally {
170-
lock.unlock();
171174
}
172175

173176
return this.getWxMpConfigStorage().getTicket(type);

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,26 @@ public String getCardApiTicket() throws WxErrorException {
4848
@Override
4949
public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
5050
final TicketType type = TicketType.WX_CARD;
51-
Lock lock = getWxMpService().getWxMpConfigStorage().getTicketLock(type);
52-
lock.lock();
53-
try {
5451

55-
if (forceRefresh) {
56-
this.getWxMpService().getWxMpConfigStorage().expireTicket(type);
57-
}
52+
if (forceRefresh) {
53+
this.getWxMpService().getWxMpConfigStorage().expireTicket(type);
54+
}
5855

59-
if (this.getWxMpService().getWxMpConfigStorage().isTicketExpired(type)) {
60-
String responseContent = this.wxMpService.execute(SimpleGetRequestExecutor
61-
.create(this.getWxMpService().getRequestHttp()), WxMpApiUrl.Card.CARD_GET_TICKET, null);
62-
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
63-
String cardApiTicket = tmpJsonObject.get("ticket").getAsString();
64-
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
65-
this.getWxMpService().getWxMpConfigStorage().updateTicket(type, cardApiTicket, expiresInSeconds);
56+
if (this.getWxMpService().getWxMpConfigStorage().isTicketExpired(type)) {
57+
Lock lock = getWxMpService().getWxMpConfigStorage().getTicketLock(type);
58+
lock.lock();
59+
try {
60+
if (this.getWxMpService().getWxMpConfigStorage().isTicketExpired(type)) {
61+
String responseContent = this.wxMpService.execute(SimpleGetRequestExecutor
62+
.create(this.getWxMpService().getRequestHttp()), WxMpApiUrl.Card.CARD_GET_TICKET, null);
63+
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
64+
String cardApiTicket = tmpJsonObject.get("ticket").getAsString();
65+
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
66+
this.getWxMpService().getWxMpConfigStorage().updateTicket(type, cardApiTicket, expiresInSeconds);
67+
}
68+
} finally {
69+
lock.unlock();
6670
}
67-
} finally {
68-
lock.unlock();
6971
}
7072
return this.getWxMpService().getWxMpConfigStorage().getTicket(type);
7173
}

0 commit comments

Comments
 (0)