1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

fix thread-unsafe about SimpleDateFormat

This commit is contained in:
pyh_uestc 2019-05-27 19:27:18 +08:00
parent 2cb7327a78
commit ccaae6aad7
6 changed files with 12 additions and 21 deletions

View File

@ -158,15 +158,11 @@ public class DefaultAlicomMessagePuller {
if (!polling) { if (!polling) {
popMsg = queue.popMessage(); popMsg = queue.popMessage();
if (debugLogOpen) { if (debugLogOpen) {
ThreadLocal<SimpleDateFormat> format = new ThreadLocal<SimpleDateFormat>(){ SimpleDateFormat format = new SimpleDateFormat(
@Override "yyyy-MM-dd HH:mm:ss");
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
log.info("PullMessageTask_popMessage:" log.info("PullMessageTask_popMessage:"
+ Thread.currentThread().getName() + "-popDone at " + Thread.currentThread().getName() + "-popDone at "
+ "," + format.get().format(new Date()) + " msgSize=" + "," + format.format(new Date()) + " msgSize="
+ (popMsg == null ? 0 : popMsg.getMessageId())); + (popMsg == null ? 0 : popMsg.getMessageId()));
} }
if (popMsg == null) { if (popMsg == null) {

View File

@ -77,14 +77,9 @@ public class TokenGetterForAlicom {
} }
private TokenForAlicom getTokenFromRemote(String messageType) private TokenForAlicom getTokenFromRemote(String messageType)
throws ClientException, ParseException { throws ServerException, ClientException, ParseException {
ThreadLocal<SimpleDateFormat> df = new ThreadLocal<SimpleDateFormat>(){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
df.get().setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
QueryTokenForMnsQueueRequest request = new QueryTokenForMnsQueueRequest(); QueryTokenForMnsQueueRequest request = new QueryTokenForMnsQueueRequest();
request.setAcceptFormat(FormatType.JSON); request.setAcceptFormat(FormatType.JSON);
request.setMessageType(messageType); request.setMessageType(messageType);
@ -99,7 +94,7 @@ public class TokenGetterForAlicom {
TokenForAlicom token = new TokenForAlicom(); TokenForAlicom token = new TokenForAlicom();
String timeStr = dto.getExpireTime(); String timeStr = dto.getExpireTime();
token.setMessageType(messageType); token.setMessageType(messageType);
token.setExpireTime(df.get().parse(timeStr).getTime()); token.setExpireTime(df.parse(timeStr).getTime());
token.setToken(dto.getSecurityToken()); token.setToken(dto.getSecurityToken());
token.setTempAccessKeyId(dto.getAccessKeyId()); token.setTempAccessKeyId(dto.getAccessKeyId());
token.setTempAccessKeySecret(dto.getAccessKeySecret()); token.setTempAccessKeySecret(dto.getAccessKeySecret());
@ -114,7 +109,7 @@ public class TokenGetterForAlicom {
public TokenForAlicom getTokenByMessageType(String messageType, String queueName, public TokenForAlicom getTokenByMessageType(String messageType, String queueName,
String mnsAccountEndpoint) String mnsAccountEndpoint)
throws ClientException, ParseException { throws ServerException, ClientException, ParseException {
TokenForAlicom token = tokenMap.get(messageType); TokenForAlicom token = tokenMap.get(messageType);
Long now = System.currentTimeMillis(); Long now = System.currentTimeMillis();
if (token == null || (token.getExpireTime() - now) < bufferTime) {// 过期时间小于2分钟则重新获取防止服务器时间误差 if (token == null || (token.getExpireTime() - now) < bufferTime) {// 过期时间小于2分钟则重新获取防止服务器时间误差