wen
wen

Reputation: 303

Is JAX-RS Client Thread Safe

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?

Upvotes: 30

Views: 16017

Answers (3)

sleske
sleske

Reputation: 83645

Update as of 2025 / JAX-RS 4.0

The official answer is that the JAX-RS specification does not guarantee the thread safety of javax.ws.rs.client.Client.

In practice, many implementations are thread-safe, but not all. Apache CXF, for example, is not thread-safe:

Proxies and web clients (clients) are not thread safe by default. In some cases this can be a limitation, especially when clients are injected; synchronizing on them can cause performance side effects.

Apache CXF - JAX-RS : Client API

There is an open spec bug to require thread safety in the spec - JavaDocs for Client, WebTarget and Invocation should clearly say whether invocations MUST be thread-safe #494.

For now, you must check the implementation you use for thread safety, or avoid reuse across threads, e.g. by keeping an instance of Client in a ThreadLocal , as explained in What is the Use and need of thread local .

Upvotes: 0

tddmonkey
tddmonkey

Reputation: 21184

PLEASE BE AWARE: Although this is the accepted answer, this is implementation specific and was correct for the Jersey 1 Client. For that you absolutely should share a single instance. Creating a client per request is a huge performance overhead

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.

Upvotes: -11

Renan
Renan

Reputation: 1895

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:

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:

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

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

Upvotes: 19

Related Questions