make json date formatter thread safe

patch by dbrosius reviewed by thobbs for CASSANDRA-10814
This commit is contained in:
Dave Brosius 2015-12-21 19:20:49 -05:00
parent 8565ca89a9
commit ebbd516985
3 changed files with 15 additions and 4 deletions

View File

@ -82,7 +82,7 @@ public class DateType extends AbstractType<Date>
@Override
public String toJSONString(ByteBuffer buffer, int protocolVersion)
{
return '"' + TimestampSerializer.TO_JSON_FORMAT.format(TimestampSerializer.instance.deserialize(buffer)) + '"';
return '"' + TimestampSerializer.getJsonDateFormatter().format(TimestampSerializer.instance.deserialize(buffer)) + '"';
}
@Override

View File

@ -90,7 +90,7 @@ public class TimestampType extends AbstractType<Date>
@Override
public String toJSONString(ByteBuffer buffer, int protocolVersion)
{
return '"' + TimestampSerializer.TO_JSON_FORMAT.format(TimestampSerializer.instance.deserialize(buffer)) + '"';
return '"' + TimestampSerializer.getJsonDateFormatter().format(TimestampSerializer.instance.deserialize(buffer)) + '"';
}
@Override

View File

@ -96,8 +96,14 @@ public class TimestampSerializer implements TypeSerializer<Date>
}
};
public static final SimpleDateFormat TO_JSON_FORMAT = new SimpleDateFormat(dateStringPatterns[15]);
private static final ThreadLocal<SimpleDateFormat> FORMATTER_TO_JSON = new ThreadLocal<SimpleDateFormat>()
{
protected SimpleDateFormat initialValue()
{
return new SimpleDateFormat(dateStringPatterns[15]);
}
};
public static final TimestampSerializer instance = new TimestampSerializer();
public Date deserialize(ByteBuffer bytes)
@ -138,6 +144,11 @@ public class TimestampSerializer implements TypeSerializer<Date>
throw new MarshalException(String.format("Unable to coerce '%s' to a formatted date (long)", source), e1);
}
}
public static SimpleDateFormat getJsonDateFormatter()
{
return FORMATTER_TO_JSON.get();
}
public void validate(ByteBuffer bytes) throws MarshalException
{