Set DateFormat

184 views
Skip to first unread message

Mohan Radhakrishnan

unread,
Oct 11, 2016, 2:44:26 PM10/11/16
to jackson-user
Hi,
         

        I see this comment on setDateFormat

If you need per-request configuration, use {@link #writer(DateFormat)} to * create properly configured {@link ObjectWriter} and use that; this because * {@link ObjectWriter}s are thread-safe whereas ObjectMapper itself is only * thread-safe when configuring methods (such as this one) are NOT called.
Does this mean that ObjectMapper itself cannot be thread-safe when we set a date format ?
Does this mean that DateFormat isn't thread-safe ? DateFormat's aren't thread-safe.
Thanks,
Mohan

Tatu Saloranta

unread,
Oct 11, 2016, 5:37:41 PM10/11/16
to jackso...@googlegroups.com
It means that `ObjectMapper` is only thread-safe when ALL configuration is done first, before any usage.
This includes, but is not limited to, configuring default `DateFormat` to use.
Usage of `DateFormat` itself is synchronized such that this aspect itself is not problematic, rather, settings of `ObjectMapper` are assumed to be fixed when first `readValue()` or `writeValue()` is done (*)

So if you do need to be able to change active default `DateFormat` to use, you need to either create separate `ObjectMapper`s (inefficient but ok if you have just couple), or use ObjectReader/ObjectWriter. Latter allow thread-safe creation of differently configured instances and are designed for more flexible configuration.

You can think of `ObjectMapper` as a factory for creating `ObjectReader`s and `ObjectWriter`s; with couple of convenience methods as short-cuts.

I hope this helps,

-+ Tatu +-

(*) What happens with any attempted changes after this is undefined, but is likely one of (1) ignoring new settings, (2) using new settings, (3) failure in form of exception getting thrown.




--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user+unsubscribe@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mohan Radhakrishnan

unread,
Oct 12, 2016, 3:01:07 AM10/12/16
to jackson-user
First question based on your reply.

>>Usage of `DateFormat` itself is synchronized such that this aspect itself is not problematic

The code synchronizes 'DateFormat' when mapping data across multiple threads ? Where exactly is this done ?


Thanks,
Mohan
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.

Mohan Radhakrishnan

unread,
Oct 12, 2016, 3:45:41 AM10/12/16
to jackson-user
I was asking because java.time.format.DateTimeFormatter has this property.

A formatter created from a pattern can be used as many times as necessary, it is immutable and is thread-safe.

Mohan

Tatu Saloranta

unread,
Oct 12, 2016, 4:27:15 PM10/12/16
to jackson-user
On Wed, Oct 12, 2016 at 12:01 AM, Mohan Radhakrishnan <radhakris...@gmail.com> wrote:
First question based on your reply.

>>Usage of `DateFormat` itself is synchronized such that this aspect itself is not problematic

The code synchronizes 'DateFormat' when mapping data across multiple threads ? Where exactly is this done ?

A new instance is created with a clone; no sharing is done. If you want to see where, have a look at code.

-+ Tatu +-

 
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user+unsubscribe@googlegroups.com.

Mohan Radhakrishnan

unread,
Oct 13, 2016, 3:50:59 AM10/13/16
to jackson-user
I find this in DateDeserializers.java synchronized (_customFormat) {
try {
return _customFormat.parse(str);
} catch (ParseException e) {
return (java.util.Date) ctxt.handleWeirdStringValue(handledType(), str,
"expected format \"%s\"", _formatString);
}
}

Isn't it synchronized here ?

Mohan
Reply all
Reply to author
Forward
0 new messages