Is java Jersey 2.1 client thread safe?
Asked Answered
K

2

38

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.

Keister answered 6/8, 2013 at 11:13 Comment(1)
I opened a ticket for you (me lol): java.net/jira/browse/JERSEY-3094Nikethamide
A
26

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.

Avowal answered 6/9, 2013 at 8:9 Comment(6)
Helpful answer. Do you happen to have any sources for this as well?Pyroclastic
Does this restriction come from JAX-RS or only from Jersey? Because javax.ws.rs.client.Client 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.Cleo
@datakey EXACTLY, can anybody confirm the answer? I have no idea what to think about Client and WebTarget ..!Axilla
@Cleo - In fact, javax.ws.rs.client.Client 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.Fictionalize
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: java.net/jira/browse/JERSEY-3094Nikethamide
The ticket of @MartinAndersson moved here and is still open! github.com/eclipse-ee4j/jersey/issues/3366Dukes
L
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.

Loxodromic answered 6/8, 2013 at 11:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.