36

Documentation for jersey 2.0 says:

Client instances are expensive resources. It is recommended a configured instance is reused for the creation of Web resources. The creation of Web resources, the building of requests and receiving of responses are guaranteed to be thread safe. Thus a Client instance and WebResource instances may be shared between multiple threads

Is client still thread-safe in version 2.1? I cannot find information about thread safety in docs for 2.1.

jakub.piasecki
  • 536
  • 1
  • 5
  • 12

2 Answers2

25

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.

The thread safety can be broken if you register your custom non-thread safe provider into a Client or a WebTaget. For example a ClientRequestFilter that is not thread safe and cannot handle more requests simultaneously. Jersey built-in providers are thread safe. Some Jersey extension providers must not be thread safe and in this case this is specified in the javadoc of a provider.

Miroslav Fuksa
  • 451
  • 3
  • 6
  • 4
    Helpful answer. Do you happen to have any sources for this as well? – Nils Schmidt Mar 12 '14 at 10:09
  • 5
    Does this restriction come from JAX-RS or only from Jersey? Because [`javax.ws.rs.client.Client`](https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/client/Client.html) says only: _Clients are heavy-weight objects ... 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 ... Client instances must be properly closed before being disposed to avoid leaking resources._ So nothing about **thread-safety**, only **close to be reused** and but nothing about _at the same time_. – lucasvc Oct 17 '14 at 20:48
  • 1
    @datakey EXACTLY, can anybody confirm the answer? I have no idea what to think about `Client` and `WebTarget` ..! – Yanko Nov 07 '14 at 13:20
  • 2
    @lucasvc - In fact, [javax.ws.rs.client.Client](https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/client/Client.html) does **not** say **close to be reused**. It says: _Client instances must be properly closed before being **disposed** to avoid leaking resources._ And the documentation for the `close()` method says: _Once the client is closed, invoking any other method on the client instance would result in an IllegalStateException being thrown._ So there is no reuse possible after closing. – Ajoy Bhatia Feb 05 '16 at 22:10
  • 2
    Still.. problem persist. For some unknown reason documentation of thread-safety was removed. lol, I would have expect stuff like that to be **added**, not removed? Ticket: https://java.net/jira/browse/JERSEY-3094 – Martin Andersson Apr 14 '16 at 00:08
1

I think, based on the 2.1 release notes, nothing has changed in that regard, but I cannot find the motivation for this change in documentation.

Erik Pragt
  • 11,804
  • 10
  • 44
  • 55