title: CXF JAXRS client not reusing TCP connections
body: <p>I'm using the JAX-RS support in CXF 2.2.5 to invoke REST webservices. I'm creating a single org.apache.cxf.jaxrs.client.WebClient instance for each endpoint I need to communicate with (typically one or two endpoints for any given deployment) and re-using this client for each web-service invocation.</p>
  
  <p>The problem I face is that the client is creating new TCP connections to the server for each request, despite using the keep-alive setting. At high traffic levels, this is causing problems. An excerpt from my client code is below.</p>
  
  <p>I'm trying to dig through the CXF source to identify the problem but getting hopelessly lost at present. Any thoughts greatly appreciated.</p>
  
  <p>Thanks,
  FB</p>
  
  <pre><code>ConcurrentMap&lt;String, WebClient&gt; webclients = new ConcurrentHashMap&lt;String, WebClient&gt;();
  
  public void dispatchRequest(MyRequestClass request, String hostAddress) {
  
      // Fetch or create the web client if we don't already have one for this hostAddress
      // NOTE: WebClient is only thread-safe if not changing the URI or headers between calls!
      //   http://cxf.apache.org/docs/jax-rs-client-api.html#JAX-RSClientAPI-ThreadSafety
      WebClient client = webclients.get(hostAddress);
      if (client == null) {
          String serviceUrl = APP_HTTP_PROTOCOL + "://" + hostAddress + ":" + APP_PORT + "/" + APP_REQUEST_PATH;
          WebClient newClient = WebClient.create(serviceUrl).accept(MediaType.TEXT_PLAIN);
          client = webclients.putIfAbsent(hostAddress, newClient);
          if (client == null) {
              client = newClient;
          } // Else, another thread must have added the client in the meantime - that's fine if so.
      }
  
      XStream marshaller = MyCollection.getMarshaller();
      String requestXML = marshaller.toXML(request);
  
      Response response = null;
      try {
          // Send it!
          response = client.post(requestXML);
      }
      catch (Exception e) {
      }
  
      ...
  }
  </code></pre>