Java JAX-RS 客户端线程安全吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24700798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:15:35  来源:igfitidea点击:

Is JAX-RS Client Thread Safe

javamultithreadingrestjax-rs

提问by wen

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. "

在 Java EE7 中,JAX-RS 客户端 API 提供了用于访问任何 REST 资源的高级 API。根据文档,“客户端是管理客户端通信基础设施的重量级对象。客户端实例的初始化和处置可能是一个相当昂贵的操作。因此建议只构建少量的客户端实例在申请中。”

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?

为了避免频繁创建客户端,我准备缓存客户端实例并重用它。客户端实例线程是否安全,因为它可以被并发线程使用?如果我只创建客户端的一个实例并将其重用于所有请求,是否有任何性能问题?

采纳答案by tddmonkey

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 notreusing it, i.e. if you create a Client for every HTTP request you make your performance will suck really bad.

JavaDoc 主要已经回答了您的问题 - 是的,它是线程安全的,您可以而且应该重用它。重用它可能会导致性能问题,即如果您为每个 HTTP 请求创建一个客户端,您的性能将非常糟糕。

回答by Renan

I am not sure but I think this is a implementation-specific decision.

我不确定,但我认为这是一个特定于实现的决定。

I couldn't find in the JAX-RS 2.0 specification nor in the Javadoc anything granting that javax.ws.rs.client.Client is thread-safe. But in the Resteasy (an implementor of JAX-RS) documentation I found:

我在 JAX-RS 2.0 规范和 Javadoc 中都找不到任何允许 javax.ws.rs.client.Client 是线程安全的。但是在 Resteasy(JAX-RS 的实现者)文档中我发现:

One default decision made by HttpClient and adopted by Resteasy is the use of org.apache.http.impl.conn.SingleClientConnManager, which manages a single socket at any given time and which supports the use case in which one or more invocations are made serially from a single thread. For multithreaded applications, SingleClientConnManager may be replaced by org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager:

HttpClient 做出并被 Resteasy 采用的一个默认决定是使用 org.apache.http.impl.conn.SingleClientConnManager,它在任何给定时间管理单个套接字,并支持一个或多个连续调用的用例从一个线程。对于多线程应用,SingleClientConnManager 可能被 org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager 替换:

ClientConnectionManager cm = new ThreadSafeClientConnManager();
HttpClient httpClient = new DefaultHttpClient(cm);
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);

Source: http://docs.jboss.org/resteasy/docs/3.0.9.Final/userguide/html/RESTEasy_Client_Framework.html#transport_layer

来源:http: //docs.jboss.org/resteasy/docs/3.0.9.Final/userguide/html/RESTEasy_Client_Framework.html#transport_layer

Based in these information I guess that the answer for your question is likely to be "no".

根据这些信息,我猜您的问题的答案很可能是“否”。