RestTemplate 线程安全吗? [英] Is RestTemplate thread safe?

查看:189
本文介绍了RestTemplate 线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 线程安全(强调):

从概念上讲,它与 JdbcTemplateJmsTemplate 以及 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 the RestTemplate is thread-safe once constructed

<小时>

RestTemplate 类的对象不会改变它们的任何状态信息来处理 HTTP:该类是 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> 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 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".

该类是 Spring 框架的一部分,因此在几乎所有实际情况下,该类的对象都将被设置为 Spring 应用程序上下文的一部分,使用第一个(使用构造函数的依赖注入)或第二个(使用构造函数的依赖注入)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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆