Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is RestTemplate thread safe?

Is a Spring RestTemplate thread-safe? That is

  • Is a RestTemplate a Strategy object that multiple connections can safely share. or
  • Is a RestTemplate a connection object (like a data-base connection), which can not be shared while in use, and requires creation afresh, or pooling, for each connection.
like image 598
Raedwald Avatar asked Apr 10 '14 13:04

Raedwald


People also ask

Is RestTemplate exchange thread-safe?

RestTemplate is thread-safe once constructed. Objects of the RestTemplate class do not change any of their state information to process HTTP: the class is an instance of the Strategy design pattern.

Is RestTemplate outdated?

RestTemplate provides a synchronous way of consuming Rest services, which means it will block the thread until it receives a response. RestTemplate is deprecated since Spring 5 which means it's not really that future proof.

Is RedisTemplate thread-safe?

The documentation said about RedisTemplate "Once configured, this class is thread-safe."

Should I replace RestTemplate with WebClient?

If you need such a functionality, then yes you need to replace your Resttemplate with WebClient. You can in fact achieve Rest template like synchronous processing in webclient using . block() . But the other way is not possible.


1 Answers

RestTemplate is thread safe (emphasis added):

Conceptually, it is very similar to the JdbcTemplate, JmsTemplate, and the various other templates found in the Spring Framework and other portfolio projects. This means, for instance, that the RestTemplate is thread-safe once constructed


Objects of the RestTemplate class do not change any of their state information to process HTTP: the class is an instance of the Strategy design pattern, rather than being like a connection object. With no state information, there is no possibility of different threads corrupting or racing state information if they share a RestTemplate object. This is why it is possible for threads to share these objects.

If you examine the source code of RestTemplate you will see that it does not use synchronized methods or volatile fields to provide thread-safety after construction of the object. So it is not safe to modify a RestTemplate object after construction. In particular, it is unsafe to add a message converter.

To provide it with a list of message converters you must do one of the following:

  • Use the RestTemplate(List<HttpMessageConverter<?>> messageConverters) constructor. As the internal list of messageConverters is final, this safely publishes the list of message converters.
  • Use the setMessageConverters(List<HttpMessageConverter<?>> messageConverters) mutator and then safely-publish the changed RestTemplate object. Using a Spring bean definition that has a <property name="messageConverters"><list>... does this, as the bean will be safely published by the thread setting up the container in most practical use cases.
  • Use List.add on the reference returned by getMessageConverters() and then safely publish the changed RestTemplate object. However, the documentation for RestTemplate does not explicitly state that it returns a reference that can be used to alter the list of message converters. The current implementation does, but possibly the implementation might be changed to return a Collections.unmodifiableList or a copy of the list. So it might be better not to change it this way.

Note that the first case is the only means of setting up the message converters when constructing the object, so it is correct to say that it "is thread safe once constructed".

The class is part of the Spring Framework, so in almost all practical cases objects of the class will be set up as part of a Spring Application Context, using the first (dependency injection using a constructor) or second (dependency injection using a setter) methods, and so would be guaranteed to be safely published to multiple threads.

like image 134
Raedwald Avatar answered Sep 21 '22 10:09

Raedwald