Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is java Jersey 2.1 client thread safe?

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.

like image 295
jakub.piasecki Avatar asked Aug 06 '13 11:08

jakub.piasecki


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.

Which thread is safe in Java?

A [portion of code] is thread-safe if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronization or other coordination on the part of the calling code.

What is jersey client used for?

Jersey is an open source framework for developing RESTFul Web Services. It also has great inbuilt client capabilities. In this quick tutorial, we will explore the creation of JAX-RS client using Jersey 2.

What is Jersey Apache client?

Jersey is a REST-client, featuring full JAX-RS implementation, neat fluent API and a powerfull filter stack. Apache Http Client is a HTTP-client, perfect in managing low-level details like timeouts, complex proxy routes and connection polling. They act on a different levels of your protocol stack.


2 Answers

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.

like image 58
Miroslav Fuksa Avatar answered Oct 10 '22 04:10

Miroslav Fuksa


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.

like image 41
Erik Pragt Avatar answered Oct 10 '22 04:10

Erik Pragt