@@ -14,12 +14,12 @@ public class WxMsgIdInMemoryDuplicateChecker implements WxMsgIdDuplicateChecker | |||
/** | |||
* 一个消息ID在内存的过期时间:15秒 | |||
*/ | |||
private final Long TIME_TO_LIVE; | |||
private final Long timeToLive; | |||
/** | |||
* 每隔多少周期检查消息ID是否过期:5秒 | |||
*/ | |||
private final Long CLEAR_PERIOD; | |||
private final Long clearPeriod; | |||
private final ConcurrentHashMap<Long, Long> msgId2Timestamp = new ConcurrentHashMap<Long, Long>(); | |||
@@ -31,8 +31,8 @@ public class WxMsgIdInMemoryDuplicateChecker implements WxMsgIdDuplicateChecker | |||
* </pre> | |||
*/ | |||
public WxMsgIdInMemoryDuplicateChecker() { | |||
this.TIME_TO_LIVE = 15 * 1000l; | |||
this.CLEAR_PERIOD = 5 * 1000l; | |||
this.timeToLive = 15 * 1000l; | |||
this.clearPeriod = 5 * 1000l; | |||
this.start(); | |||
} | |||
@@ -42,8 +42,8 @@ public class WxMsgIdInMemoryDuplicateChecker implements WxMsgIdDuplicateChecker | |||
* @param clearPeriod 每隔多少周期检查消息ID是否过期:毫秒 | |||
*/ | |||
public WxMsgIdInMemoryDuplicateChecker(Long timeToLive, Long clearPeriod) { | |||
this.TIME_TO_LIVE = timeToLive; | |||
this.CLEAR_PERIOD = clearPeriod; | |||
this.timeToLive = timeToLive; | |||
this.clearPeriod = clearPeriod; | |||
this.start(); | |||
} | |||
@@ -53,10 +53,10 @@ public class WxMsgIdInMemoryDuplicateChecker implements WxMsgIdDuplicateChecker | |||
public void run() { | |||
try { | |||
while (true) { | |||
Thread.sleep(CLEAR_PERIOD); | |||
Thread.sleep(clearPeriod); | |||
Long now = System.currentTimeMillis(); | |||
for (Map.Entry<Long, Long> entry : msgId2Timestamp.entrySet()) { | |||
if (now - entry.getValue() > TIME_TO_LIVE) { | |||
if (now - entry.getValue() > timeToLive) { | |||
msgId2Timestamp.entrySet().remove(entry); | |||
} | |||
} | |||
@@ -39,19 +39,18 @@ import java.io.StringReader; | |||
import java.security.NoSuchAlgorithmException; | |||
import java.util.List; | |||
import java.util.UUID; | |||
import java.util.concurrent.atomic.AtomicBoolean; | |||
public class WxMpServiceImpl implements WxMpService { | |||
/** | |||
* 全局的是否正在刷新access token的锁 | |||
*/ | |||
protected static final Object GLOBAL_ACCESS_TOKEN_REFRESH_LOCK = new Object(); | |||
protected final Object globalAccessTokenRefreshLock = new Object(); | |||
/** | |||
* 全局的是否正在刷新jsapi_ticket的锁 | |||
*/ | |||
protected static final Object GLOBAL_JSAPI_TICKET_REFRESH_LOCK = new Object(); | |||
protected final Object globalJsapiTicketRefreshLock = new Object(); | |||
protected WxMpConfigStorage wxMpConfigStorage; | |||
@@ -78,7 +77,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
wxMpConfigStorage.expireAccessToken(); | |||
} | |||
if (wxMpConfigStorage.isAccessTokenExpired()) { | |||
synchronized (GLOBAL_ACCESS_TOKEN_REFRESH_LOCK) { | |||
synchronized (globalAccessTokenRefreshLock) { | |||
if (wxMpConfigStorage.isAccessTokenExpired()) { | |||
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" | |||
+ "&appid=" + wxMpConfigStorage.getAppId() | |||
@@ -119,7 +118,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
wxMpConfigStorage.expireJsapiTicket(); | |||
} | |||
if (wxMpConfigStorage.isJsapiTicketExpired()) { | |||
synchronized (GLOBAL_JSAPI_TICKET_REFRESH_LOCK) { | |||
synchronized (globalJsapiTicketRefreshLock) { | |||
if (wxMpConfigStorage.isJsapiTicketExpired()) { | |||
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi"; | |||
String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | |||