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

查看:1979
本文介绍了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:

  • 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线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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