Search code examples
web-servicesrestcxfconnection-poolingcxf-client

how to use connection pooling in cxf jaxrs webclient


I am building a REST service which internally calls other services and we use org.apache.cxf.jaxrs.client.WebClient to do this.

I want to use HTTP connection pooling to improve the performance but the documentation isn't very clear about how to do this or if this is even possible. Has anyone here done this?

The only other option I can think of it is to re-use clients but I'd rather not get into the whole set of thread-safety and synchronization issues that comes with that approach.


Solution

  • By default, CXF uses a transport based on the in-JDK HttpURLConnection object to perform HTTP requests.

    Connection pooling is performed allowing persistent connections to reuse the underlying socket connection for multiple http requests.

    Set system properties (default values)

     http.keepalive=true
     http.maxConnections=5
    

    Increment the value of http.maxConnections to set the maximum number of idle connections that will be simultaneously kept alive, per destination.

    In this post are explained some detail how it works

    Java HttpURLConnection and pooling

    When you need many requests executed simultaneosly CXF can also use the asynchronous apache HttpAsyncClient. Ser details here

    http://cxf.apache.org/docs/asynchronous-client-http-transport.html