Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added information

...

CXF also has an HTTP client transport that is based on the Apache HTTP Components HttpAsyncClient library. The Its Maven artifactId is cxf-rt-transports-http-hc. The HttpAsyncClient library uses a non-blocking IO model. This allows many more requests to be outstanding without consuming extra background threads. It also allows greater control over things like Keep-Alive handling which is very difficult or impossible with the HttpURLConnection based transport. However, the non-blocking model does not perform quite as well as the blocking model for pure synchronous request/response transactions.

By default, if the cxf-rt-transports-http-hc module is found on the classpath, CXF will use the HttpAsyncClient based implementation for any Async calls, but will continue to use the HttpURLConnection based transport for synchronous calls. This allows a good balance of performance for the common synchronous cases with scalability for the asynchronous cases. However, using a contextual property of "use.async.http.conduit" and set to true/false, you can control whether the async or blocking version is used. If "true", the HttpAsyncClient will be used even for synchronous calls, if "false", asynchronous calls will rely on the traditional method of using HTTPURLConnection along with a work queue to mimic the asynchronocity.

Another reason to use the asynchronous transport is to use HTTP methods that HttpURLConnection does not support. For example, the github.com REST API specifies the use of PATCH for some cases, but HttpURLConnection rejects PATCH.

Using the HTTP Components Transport from Java Code

To force global use of the HTTP Components transport, you can set a bus-level property:

 

 Bus bus = BusFactory.getDefaultBus();
// insist on the async connector to use PATCH.
bus.setProperty(AsyncHTTPConduit.USE_ASYNC, Boolean.TRUE);

Setting Credentials

The "normal" CXF/JAX-WS method of setting user credentials via the BindingProvider.USERNAME_PROPERTY/PASSWORD_PROPERTY will work with the Async transport as well. However, the HttpAsyncClient library does have some additional capabilities around NTLM that can be leveraged. In order to use that, you need to:

  • Turn on the AutoRedirect and turn off the Chunking for the Conduit. This will allow CXF to cache the response in a manner that will allow the transport to keep resending the request during the authentication negotiation.
  • Force the use of the Async transport even for synchronous calls

    Code Block
    java
    java
    
    bp.getRequestContext().put("use.async.http.conduit", Boolean.TRUE);
    
  • Set the property "org.apache.http.auth.Credentials" to an instance of the Credentials. For example:

    Code Block
    java
    java
    
    Credentials creds = new NTCredentials("username", "pswd", null, "domain");
    bp.getRequestContext().put(Credentials.class.getName(), creds);
    

...