{"id":848,"date":"2017-08-06T20:43:58","date_gmt":"2017-08-07T01:43:58","guid":{"rendered":"http:\/\/tiemensfamily.com\/TimOnCS\/?p=848"},"modified":"2017-08-06T20:43:58","modified_gmt":"2017-08-07T01:43:58","slug":"is-spring-resttemplate-thread-safe","status":"publish","type":"post","link":"https:\/\/tiemensfamily.com\/timoncs\/2017\/08\/06\/is-spring-resttemplate-thread-safe\/","title":{"rendered":"Is Spring RestTemplate Thread Safe"},"content":{"rendered":"<p>Is Spring RestTemplate <a href=\"https:\/\/www.quora.com\/What-does-the-term-thread-safe-mean-in-Java\">Thread Safe<\/a>?<\/p>\n<p>The only correct answer to this question is:  No<\/p>\n<p>A quick look at the <a href=\"https:\/\/github.com\/spring-projects\/spring-framework\/blob\/cc74a2891a4d2a4c7bcec059f20c35aa80bcf668\/spring-web\/src\/main\/java\/org\/springframework\/web\/client\/RestTemplate.java\">source<\/a> (the link is tied to a specific version so the line number is always correct) on line 230 shows the method &#8220;setMessageConverters&#8221; which mutates the field messageConverters without proper thread safe access or publishing.<\/p>\n<p>There are multiple incorrect answers to this question found on the web:<\/p>\n<ul>\n<li><a href=\"https:\/\/spring.io\/blog\/2009\/03\/27\/rest-in-spring-3-resttemplate\">spring.io<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/22989500\/is-resttemplate-thread-safe\">Stackoverflow.com<\/a><\/li>\n<li><a href=\"https:\/\/codedump.io\/share\/HeuwfeqsJM7v\/1\/is-resttemplate-thread-safe\">codedump.io<\/a><\/li>\n<\/ul>\n<p>All of these incorrect answers are actually answering the question: &#8220;Is there a way to use an instance of RestTemplate that is thread safe?&#8221; or just quoting other incorrect posts.  (Especially disappointing is the blog post on spring.io.  But, it was written in 2009, so maybe it was thread safe back then.  Since at least <a href=\"https:\/\/github.com\/spring-projects\/spring-framework\/blob\/02a4473c62d8240837bec297f0a1f3cb67ef8a7b\/spring-web\/src\/main\/java\/org\/springframework\/web\/client\/RestTemplate.java\">2012<\/a> (line 171) it has not been thread safe.)  Or, they answer &#8220;Yes&#8221; then say &#8220;But don&#8217;t add a MessageConverter, because that is not thread safe&#8221;.<\/p>\n<p>&#8220;Is there a way to use an instance of RestTemplate that is thread safe?&#8221; is a different question, and like <strong>all<\/strong> Java objects, the answer of course is: Yes.<\/p>\n<p>If your code avoids the non-thread-safe methods, then you can claim thread safety.<br \/>\nIf your code simply doesn&#8217;t call any methods on the instance, you can claim thread safety.<br \/>\nIf your code only has one thread, you can claim thread safety.<\/p>\n<p>But the actual code of RestTemplate.java shows that it is not thread safe.<br \/>\nAnd the typical use is to create a singleton instance that is used by multiple threads.<br \/>\nIt is a &#8220;cross your fingers and hope nothing weird happens&#8221; kind of thread safe.<\/p>\n<p>Which, of course, is not thread safe at all.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Is Spring RestTemplate Thread Safe? The only correct answer to this question is: No A quick look at the source (the link is tied to a specific version so the line number is always correct) on line 230 shows the &hellip; <a href=\"https:\/\/tiemensfamily.com\/timoncs\/2017\/08\/06\/is-spring-resttemplate-thread-safe\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts\/848"}],"collection":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/comments?post=848"}],"version-history":[{"count":0,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts\/848\/revisions"}],"wp:attachment":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/media?parent=848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/categories?post=848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/tags?post=848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}