问题描述
在Java EE7中,JAX-RS Client API提供了一个用于访问任何REST资源的高级API。根据文档,客户端是管理客户端通信基础设施的重量级对象。初始化以及客户端实例的处理可能是一个相当昂贵的操作。因此建议只构建一小部分。应用程序中的客户端实例。
In Java EE7, the JAX-RS Client API provides a high-level API for accessing any REST resources. According to the documentation, "Clients are heavy-weight objects that manage the client-side communication infrastructure. Initialization as well as disposal of a Client instance may be a rather expensive operation. It is therefore advised to construct only a small number of Client instances in the application. "
为了避免经常创建客户端,我将缓存客户端实例并重用它。客户端实例线程是否安全,因为并发线程可以使用它?
如果我只创建客户端实例并将其重用于所有请求,是否存在任何性能问题?
In order to avoid create client frequently, I am going to cache the client instance and reuse it. Is the client instance thread safe since it can be used by concurrent threads?Is there any performance issue if I only create a instance of the client and reuse it for all the requests?
推荐答案
JavaDoc主要回答你的问题 - 是的,它是线程安全的,你可以而且应该重用它。 不可能会出现性能问题,即如果您为每个HTTP请求创建一个客户端,那么您的性能就会非常糟糕。
The JavaDoc is mostly answering your question already- yes it's thread-safe and you can and should reuse it. There can be a performance issue from not reusing it, i.e. if you create a Client for every HTTP request you make your performance will suck really bad.
这篇关于JAX-RS客户端线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!