[wildfly-dev] Invalid use of BasicClientConnManager for JAX-RS Client API

Nibin Varghese nibin.gv at gmail.com
Thu Nov 28 13:21:30 EST 2013


Hi,

Connections can be re-used if the responses of the previous request is
consumed. That's how ApacheHttpClient works for SimpleConnectionManager. By
default, rest easy is using SingleClientConnectionManager[1] as explained
in rest-easy docs[2]. Now this[1] class is depricated and they suggest
using BasicClientConnectionManager[3]. Technically, they[1][3] means the
same thing such that

Quoted
"BasicHttpClientConnectionManager is a simple connection manager that
maintains only one connection at a time. Even though this class is
thread-safe it ought to be used by one execution thread only.
BasicHttpClientConnectionManager will make an effort to reuse the
connection for subsequent requests with the same route. It will, however,
close the existing connection and re-open it for the given route, if the
route of the persistent connection does not match that of the connection
request. If the connection has been already been allocated, then
java.lang.IllegalStateException is thrown."

A connection remains allocated if the response is not consumed.

[1]
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/SingleClientConnManager.html
[2]
http://docs.jboss.org/resteasy/docs/3.0.5.Final/userguide/html_single/index.html#transport_layer
[3]
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/BasicClientConnectionManager.html
[4]
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e361

-Nibin


On Sun, Nov 10, 2013 at 11:05 AM, Arun Gupta <arun.gupta at gmail.com> wrote:

> Nibin,
>
> After changing
>
>     @BeforeClass
>     public void setUp() {
>         client = ClientBuilder.newClient();
>         target =
> client.target("http://localhost:8080/jaxrs-client/webresources/persons");
>     }
>
>
> to
>
>     @Before
>     public void setUp() {
>         client = ClientBuilder.newClient();
>         target =
> client.target("http://localhost:8080/jaxrs-client/webresources/persons");
>     }
>
> this test is working fine on beta2 snapshot.
>
> Are you suggesting that the connections cannot be reused even within
> each test ? And it should not work ?
>
> Arun
>
>
> On Sun, Nov 10, 2013 at 6:24 AM, Nibin Varghese <nibin.gv at gmail.com>
> wrote:
> > Hi,
> >
> > This is becuase AppacheHTTPClient will not allow to use the same
> connection
> > if the previous request is not yet consumed. For eg, as mentioned in the
> > Test (quoted below)
> >
> >  @Test
> >     public void test1PostAndGet() {
> >         MultivaluedHashMap<String, String> map = new
> MultivaluedHashMap<>();
> >         map.add("name", "Penny");
> >         map.add("age", "1");
> >         target.request().post(Entity.form(map));
> >
> >
> >         map.clear();
> >         map.add("name", "Leonard");
> >         map.add("age", "2");
> >         target.request().post(Entity.form(map)); // <--- Reference [1]
> >
> >
> >         map.clear();
> >         map.add("name", "Sheldon");
> >         map.add("age", "3");
> >         target.request().post(Entity.form(map));  <--- Reference [2]
> >
> >
> >         Person[] list = target.request().get(Person[].class);
> >         assertEquals(3, list.length);
> >
> >
> >         assertEquals("Penny", list[0].getName());
> >         assertEquals(1, list[0].getAge());
> >
> >
> >         assertEquals("Leonard", list[1].getName());
> >         assertEquals(2, list[1].getAge());
> >
> >
> >         assertEquals("Sheldon", list[2].getName());
> >         assertEquals(3, list[2].getAge());
> >     }
> >
> > As you can see in Reference[1], the request is made and its response is
> not
> > consumed. So the connection is still in active state. You should consume
> the
> > response or call connection.close() before making the second request as
> > mentioned in Reference[2]. This is because the "target" object uses the
> same
> > ApacheHttpClient connection manager which internally uses "
> >
> > SingleClientConnManager" as discussed in the documentation[3].
> >
> >
> >
> > [3]
> >
> >
> http://docs.jboss.org/resteasy/docs/3.0.5.Final/userguide/html_single/index.html#RESTEasy_Client_Framework
> >
> >
> >
> >
> > -Nibin
> >
> >
> >
> > On Fri, Nov 8, 2013 at 5:31 AM, Arun Gupta <arun.gupta at gmail.com> wrote:
> >>
> >> Tests for JAX-RS Client API sample at:
> >>
> >>
> >>
> https://github.com/arun-gupta/javaee7-samples/tree/master/jaxrs/jaxrs-client
> >>
> >> is failing with:
> >>
> >> Caused by: java.lang.IllegalStateException: Invalid use of
> >> BasicClientConnManager: connection still allocated.
> >> Make sure to release the connection before allocating another one.
> >>     at
> >>
> org.apache.http.impl.conn.BasicClientConnectionManager.getConnection(BasicClientConnectionManager.java:161)
> >>     at
> >>
> org.apache.http.impl.conn.BasicClientConnectionManager$1.getConnection(BasicClientConnectionManager.java:138)
> >>     at
> >>
> org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:455)
> >>     at
> >>
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
> >>     at
> >>
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
> >>     at
> >>
> org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:245)
> >>     ... 33 more
> >>
> >> The instructions to run the test are described at:
> >>
> >> https://github.com/arun-gupta/javaee7-samples
> >>
> >> Any suggestions ?
> >>
> >> Arun
> >>
> >> --
> >> http://blog.arungupta.me
> >> http://twitter.com/arungupta
> >> _______________________________________________
> >> wildfly-dev mailing list
> >> wildfly-dev at lists.jboss.org
> >> https://lists.jboss.org/mailman/listinfo/wildfly-dev
> >
> >
>
>
>
> --
> http://blog.arungupta.me
> http://twitter.com/arungupta
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/wildfly-dev/attachments/20131128/c7ebf108/attachment.html 


More information about the wildfly-dev mailing list