Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 942
wen Avatar asked Jul 11 '14 15:07

wen


People also ask

Is javax WS RS client client thread safe?

JAX-RS 2.0 provides a Client API which is prepared for intense reuse of object instances of Client, WebTarget and Invocation. Unfortunately the JavaDocs of these classes do not say whether a caller can rely upon these instances are thread-safe.

Is Resteasy client thread safe?

No, by default the ResteasyClient is not thread safe because of it's underlying HTTP dependencies.

Is Jersey client thread safe?

Yes, the Jersey 2.1 client is thread safe and it should be thread safe even in the future Jersey version. You can create many WebTarget from one Client instance and invoke many requests on these WebTargets and even more requests on one WebTarget instance in the same time.

What is JAX-RS Client API?

The JAX-RS client API is a Java based API used to access Web resources.


1 Answers

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

like image 197
Renan Avatar answered Sep 29 '22 06:09

Renan