RestTemplate线程安全吗?

编程入门 行业动态 更新时间:2024-10-24 12:25:03
本文介绍了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 是线程安全的(强调添加):

从概念上讲,它与非常相似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 the RestTemplate 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 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 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线程安全吗?

本文发布于:2023-11-25 18:58:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630950.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   RestTemplate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!