Table of Contents |
---|
Asynchronous Client HTTP Transport
...
Since 3.4.6, CXF offers an HTTP client transport that is based on Apache HttpComponents HttpClient 5 library, that supports synchronous, asynchronous and reactive programming models. Its Maven artifactId is cxf-rt-transports-http-hc5 and it serves as in-place replacement for cxf-rt-transports-http-hc (but the usage of those two transports together should be avoided).
Warning |
---|
At the moment, cxf-rt-transports-http-hc5 transport does not support OSGi based deployments |
This client transport supports HTTP/2 (when enabled using org.apache.cxf.transports.http2.enabled property, see Configuration section below).
Using the HTTP Components 4.x/5.x Transport from Java Code
...
Code Block | ||
---|---|---|
| ||
Bus bus = BusFactory.getDefaultBus();
// insist on the async connector to use PATCH.
bus.setProperty(AsyncHTTPConduit.USE_ASYNC, Boolean.TRUE); |
...
// allows the async connector to use HTTP/2 protocol (if supported by the server)
bus.setProperty(AsyncHTTPConduit.ENABLE_HTTP2, enableHttp2); |
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); bp.getRequestContext().put("org.apache.cxf.transports.http2.enabled", Boolean.TRUE); // optionally, enable HTTP/2
or using AsyncHTTPConduit.USE_ASYNC constant
Code Block java java bp.getRequestContext().put(AsyncHTTPConduit.USE_ASYNC, Boolean.TRUE); bp.getRequestContext().put(AsyncHTTPConduit.ENABLE_HTTP2, Boolean.TRUE); // optionally, enable HTTP/2
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);
Instrumenting Response Processing
In certain circumstances, it is beneficial to wrap (or instrument) the async client transport response processing. Starting from Apache CXF 4.0.4 / 3.6.3 / 3.5.8 releases, it is now possible using AsyncHttpResponseWrapperFactory bus extension, for example:
Code Block | ||||
---|---|---|---|---|
| ||||
final AsyncHttpResponseWrapper wrapper = new AsyncHttpResponseWrapper() { @Override public void responseReceived(HttpResponse response, Consumer<HttpResponse> delegate) { delegate.accept(response); } }; bus.setExtension(() -> wrapper, AsyncHttpResponseWrapperFactory.class); ... |
Warning |
---|
It is very important for the AsyncHttpResponseWrapper to pass the call to |
Netty 4.x
Apache CXF also offers an HTTP client transport that is based on Netty 4.x. Its Maven artifactId is cxf-rt-transports-http-netty-client.
This client transport supports HTTP/2 (when enabled using org.apache.cxf.transports.http2.enabled property, see Configuration section below).
Configuration
The Asynchronous HTTP Transport has several options that can set using Bus properties or via the OSGi configuration services to control various aspects of the underlying Apache HTTP Components HttpAsyncClient objects.
...
org.apache.cxf.transport.http.async.CONNECTION_TTL | Maximum time a connection to live(from creation to expiry) . Default is 60000. |
org.apache.cxf.transport.http.async.MAX_CONNECTIONS | Maximum number of connections opened in total. Default is 5000. |
org.apache.cxf.transport.http.async.MAX_PER_HOST_CONNECTIONS | Maximum number of connections opened per host. Default is 1000. |
Settings related to HTTP/2 support (Apache CXF versions 4.0.2+/3.6.1+/3.5.7+/3.4.11+):
org.apache.cxf.transports.http2.enabled | true | false | Allows HTTP/2 protocol if supported by the server. Default is false. |
Settings related to Apache HttpAsyncClient threads and selectors:
...