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-24 17:11:55 +08:00
parent c3e5e4cafa
commit 6fbb0fb1f4
5 changed files with 31 additions and 11 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.cloud.alicloud.sms.base;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -158,11 +159,15 @@ public class DefaultAlicomMessagePuller {
if (!polling) {
popMsg = queue.popMessage();
if (debugLogOpen) {
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
ThreadLocal<DateFormat> format = new ThreadLocal<DateFormat>(){
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
log.info("PullMessageTask_popMessage:"
+ Thread.currentThread().getName() + "-popDone at "
+ "," + format.format(new Date()) + " msgSize="
+ "," + format.get().format(new Date()) + " msgSize="
+ (popMsg == null ? 0 : popMsg.getMessageId()));
}
if (popMsg == null) {

View File

@@ -77,7 +77,7 @@ public class TokenGetterForAlicom {
}
private TokenForAlicom getTokenFromRemote(String messageType)
throws ServerException, ClientException, ParseException {
throws ClientException, ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
QueryTokenForMnsQueueRequest request = new QueryTokenForMnsQueueRequest();
@@ -109,7 +109,7 @@ public class TokenGetterForAlicom {
public TokenForAlicom getTokenByMessageType(String messageType, String queueName,
String mnsAccountEndpoint)
throws ServerException, ClientException, ParseException {
throws ClientException, ParseException {
TokenForAlicom token = tokenMap.get(messageType);
Long now = System.currentTimeMillis();
if (token == null || (token.getExpireTime() - now) < bufferTime) {// 过期时间小于2分钟则重新获取防止服务器时间误差