RestTemplate线程安全吗? [英] Is RestTemplate thread safe?
问题描述
是一个Spring RestTemplate
线程安全吗?那是
Is a Spring RestTemplate
thread-safe? That is
- 是一个
RestTemplate
多个连接可以安全共享的策略对象。 或 - 是
RestTemplate
一个连接对象(如数据库连接),它不能在使用时共享,并需要为每个连接重新创建或汇集。
- 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.
推荐答案
RestTemplate
是线程安全的(强调添加):
从概念上讲,它与
非常相似JdbcTemplate
,JmsTemplate
,以及Spring Framework和其他项目组合项目中的各种其他模板。这意味着,例如,RestTemplate
一旦构造就是线程安全的
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 theRestTemplate
is thread-safe once constructed
RestTemplate
类的对象不会更改任何状态信息来处理HTTP: class是Strategy设计模式的一个实例,而不是像连接对象。如果没有状态信息,如果共享 RestTemplate
对象,则不可能有不同的线程破坏或竞争状态信息。这就是线程可以共享这些对象的原因。
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.
如果你检查源代码 RestTemplate
您将看到它不使用 synchronized
方法或 volatile
字段来提供线程安全性在建造物体之后。因此,在构造之后修改 RestTemplate
对象不是安全的。特别是,添加消息转换器是不安全的。
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:
- 使用
RestTemplate(List< HttpMessageConverter<?>> messageConverters)
构造函数。由于messageConverters
的内部列表是final
,因此安全地发布消息转换器列表。 - 使用
setMessageConverters(List< HttpMessageConverter<?>> messageConverters)
mutator 和然后safely-publish 已更改的RestTemplate
对象。使用具有< property name =messageConverters>< list> ...
的Spring bean定义执行此操作,因为bean 将在大多数实际使用案例中由设置容器的线程安全地发布。 - 对
getMessageConverters()
返回的引用使用List.add
然后安全地发布已更改的RestTemplate
对象。但是,RestTemplate
的文档没有明确声明它返回一个可用于更改消息转换器列表的引用。当前的实现确实如此,但可能会更改实现以返回Collections.unmodifiableList
或列表的副本。所以最好不要这样改变它。
- Use the
RestTemplate(List<HttpMessageConverter<?>> messageConverters)
constructor. As the internal list ofmessageConverters
isfinal
, this safely publishes the list of message converters. - Use the
setMessageConverters(List<HttpMessageConverter<?>> messageConverters)
mutator and then safely-publish the changedRestTemplate
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 bygetMessageConverters()
and then safely publish the changedRestTemplate
object. However, the documentation forRestTemplate
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 aCollections.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".
该类是Spring的一部分框架,所以在几乎所有实际情况下,类的对象将被设置为Spring Application Context的一部分,使用第一个(使用构造函数的依赖注入)或第二个(使用setter的依赖注入)方法,因此将得到保证安全地发布到多个线程。
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.
这篇关于RestTemplate线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!