We have a service within our webapp that connects to another RESTful service
using the JAX-RS Client API. We deploy to Wildfly 8.2, which is of course uses
RESTEasy. This service is a singleton and will receive many concurrent requests.
I'd like to know if I can safely re-use the 'javax.ws.rs.client.Client' and
'javax.ws.rs.client.WebTarget' instances and just close each
'javax.ws.rs.core.Response' for each request that our service makes?
In other words I'd like to initialize my Client and WebTarget instances once
when my service starts up and only close the Client only our service shuts
down. For each of the concurrent requests that our service receives, it will
execute a new request and close the Response.
In researching this online and such, I come across different answers and I'd
like to find out what's the correct one.
In the book "RESTful Java with JAX-RS 2.0 (2nd edition)" by Burke, I see the
following relevant statements:
1. "The javax.ws.rs.client.Client interface is the main entry point into the
JAX-RS Client API. Client instances manage client socket connections and are
pretty heavyweight. Instances of this interface should be reused wherever
possible, as it can be quite expensive to create and destroy these objects."
2. "WARNING: Always remember to close() your Client objects. Client objects
often pool connections for performance reasons. If you do not close them, you
are leaking valuable system resources. While most JAX-RS implementations
implement a finalize() method for Client, it is not a good idea to rely on the
garbage collector to clean up poorly written code."
3. "WARNING: Always remember to close() your Response objects. Response objects
reference open socket streams. If you do not close them, you are leaking system
resources. While most JAX-RS implementations implement a finalize() method for
Response, it is not a good idea to rely on the garbage collector to clean up
poorly written code. The default behavior of the RESTEasy JAX-RS implementation
actually only lets you have one open Response per Client instance. This forces
you to write responsible client code."
What's confusing here is that in quote 1, it's recommended that we reuse the
Client instance, but quote 3 ends by stating that RESTEasy only lets you have
one open Response per Client. When we have multiple concurrent requests hitting
our service, we'll need to have multiple JAX-RS Client Request-Response cycles
happen at once. Isn't this one of the use cases where we should reuse the
I also see two conflicting response to the following StackOverflow post:
Also if I read the 2nd response to that StackOverflow post correctly, I believe
I should manually supply the ThreadSafeClientConnManager() for my use case. Is
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
Resteasy-users mailing list