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

@ -41,7 +41,7 @@ public class NacosConfigEndpoint {
private final NacosRefreshHistory refreshHistory;
private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>(){
private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

View File

@ -27,7 +27,7 @@ public class NacosRefreshHistory {
private LinkedList<Record> records = new LinkedList<>();
private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>(){
private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

View File

@ -44,7 +44,7 @@ public class AcmEndpoint {
private final AcmPropertySourceRepository propertySourceRepository;
private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>(){
private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

View File

@ -30,7 +30,7 @@ public class AcmRefreshHistory {
private LinkedList<Record> records = new LinkedList<>();
private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>(){
private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

View File

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

View File

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