28

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?

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
wen
  • 283
  • 1
  • 3
  • 5
  • 7
    Great question and a glaring omission from the JAX-RS specification. I'd be interested in getting the spec lead's input on this and submit a suggestion to make this much more explicit in the spec. Either it's thread-safe in which case you would need only ONE instance in your application. Or it's not and you would need at least an instance per thread. But the vague "It is therefore advised to construct only a small number of Client instances in the application" line has absolutely no informative value. – Frans May 10 '18 at 07:43
  • 2
    There is an example in the java ee tutorial that shows the jax-rs Client is thread safe. The example uses a Stateless EJB "CustomerBean" and stores the client. https://javaee.github.io/tutorial/jaxrs-advanced008.html#GKOIB – grigouille Nov 09 '22 at 10:26
  • In that link, shouldn't the `Response response` on the `CustomerBean.createCustomer` method be closed? – gian1200 Apr 21 '23 at 18:23

2 Answers2

17

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

Renan
  • 1,705
  • 2
  • 15
  • 32
-12

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.

tddmonkey
  • 20,798
  • 10
  • 58
  • 67
  • 5
    Where does the JavaDoc say it? https://docs.oracle.com/javaee/7/api/javax/ws/rs/client/Client.html I agree with the other answer (http://stackoverflow.com/a/27427911/57217) that this seems to be an implementation specific topic. Surely you shouldn't create a new Client for each request, but it is also not safe to use it from multiple threads. – Kutzi Aug 07 '15 at 08:56
  • I said 'mostly' - "It is therefore advised to construct only a small number of Client instances". Whereas you're right in that it will be implementation specific, I can guarantee that using the Jersey client IS safe to be used from multiple threads. I have a very high volume backend system that says so! – tddmonkey Aug 07 '15 at 09:28
  • 7
    It depends on the implementation. For example: http://cxf.apache.org/docs/jax-rs-client-api.html#JAX-RSClientAPI-ThreadSafety – Peter De Winter Dec 06 '15 at 21:04
  • 1
    This shouldn't be the accepted answer. It is encouraging you to infer something that isn't there. In Java land never assume anything is threadsafe - it must be explicitly stated. SimpleDateFormat is a great example of that. I don't infer from the docs that it is threadsafe, but rather that they are expensive to create so do it sparingly. – absmiths Apr 16 '21 at 17:26
  • I can't delete the answer due to it being the accepted answer – tddmonkey Apr 18 '21 at 08:44