I had a similar question in the jersey mailing list. It seems the
specification is open to interpretation. Someone even filled a Jira to this
issue.

http://jersey.576304.n2.nabble.com/Pool-for-javax-ws-rs-client-Client-objects-td7582806.html

On Thu, Sep 17, 2015 at 12:53 PM, Ron Sigal <rsi...@redhat.com> wrote:

> Hi Peter,
>
> Currently, Resteasy defaults to using
> org.apache.http.impl.conn.BasicClientConnectionManager from the
> HttpClient project, which allows only one connection at a time. You can
> instruct Resteasy to use
> org.apache.http.impl.conn.PoolingClientConnectionManager by calling
> org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder.connectionPoolSize()
> with a value greater than 0. When Bill says "The default behavior of the
> RESTEasy JAX-RS implementation actually only lets you have one open
> Response per Client instance", I'm pretty sure that he's referring to
> the default use of BasicClientConnectionManager.
>
> With PoolingClientConnectionManager, you can do this:
>
> >       ResteasyClient client = new
> > ResteasyClientBuilder().connectionPoolSize(2).build();
> >       Invocation.Builder request =
> > client.target(TestPortProvider.generateURL("/test")).request();
> >       Response response1 = request.get();
> >       Response response2 = request.get();
> >       response1.close();
> >       response2.close();
> >       client.close();
>
> Note that BasicClientConnectionManager and
> PoolingClientConnectionManager are deprecated in HttpClient 4.3. When we
> finish https://issues.jboss.org/browse/RESTEASY-1200 "Resteasy uses
> httpclient 4.2.6 which is not in sync with httpclient 4.3.6 used in
> Wildfly" and upgrade to the new connection managers, this behavior might
> change. We've been talking about defaulting to the new
> PoolingHttpClientConnectionManager.
>
> -Ron
>
> On 07/14/2015 01:25 AM, Peter Luttrell wrote:
> > 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 Client instance?
> >
> > I also see two conflicting response to the following StackOverflow post:
> > http://stackoverflow.com/questions/24700798/is-jax-rs-client-thread-safe
> >
> > 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 that correct?
> >
> > Thanks,
> > Peter Luttrell
> >
> ------------------------------------------------------------------------------
> > 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.
> > https://www.gigenetcloud.com/
> > _______________________________________________
> > Resteasy-users mailing list
> > Resteasy-users@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/resteasy-users
>
>
>
> ------------------------------------------------------------------------------
> Monitor Your Dynamic Infrastructure at Any Scale With Datadog!
> Get real-time metrics from all of your servers, apps and tools
> in one place.
> SourceForge users - Click here to start your Free Trial of Datadog now!
> http://pubads.g.doubleclick.net/gampad/clk?id=241902991&iu=/4140
> _______________________________________________
> Resteasy-users mailing list
> Resteasy-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/resteasy-users
>
------------------------------------------------------------------------------
_______________________________________________
Resteasy-users mailing list
Resteasy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/resteasy-users

Reply via email to