问题
I am trying to make use of the CXF asynchronous HTTP client transport, through setting the "use.async.http.conduit" property, as detailed in this thread, and recommended by this CXF article.
I do that using the following code:
Client client = ClientProxy.getClient(wsClient);
client.getRequestContext().put("use.async.http.conduit", Boolean.TRUE);
As it happens, my web service call is timing out (probably due to some environmental network issue), and my client exception contains (extract):
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:698)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:641)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1218)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
at org.apache.cxf.transport.http.URLConnectionHTTPConduit$URLConnectionWrappedOutputStream.getResponseCode(URLConnectionHTTPConduit.java:260)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1513)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1486)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1305)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:623)
The exception stack above suggests that the java.net.HttpURLConnection
class is still in use and that based on the CXF documentation the setting has not taken effect.
What I am trying to figure out is how to ensure that the "use.async.http.conduit" has taken effect, i.e. is there a particular behaviour that can be tested, or a particular log config that I can enable on the client that would undoubtedly tell me the Apache HttpAsyncClient is in use?
Many thanks :)
回答1:
I just found the answer to my question, inspired by more reading of the CXF documentation, and of a useful blog.
So first how to identify whether the default Java transport client or the Apache async transport client is used:
In addition to the code I was using (see question)
Client client = ClientProxy.getClient(wsClient);
client.getRequestContext().put("use.async.http.conduit", Boolean.TRUE);
I added the following:
HTTPConduit conduit = (HTTPConduit)client.getConduit();
System.out.println(conduit.getClass().getName());
This initially produced
org.apache.cxf.transport.http.URLConnectionHTTPConduit
which told me the setting was not working. That is when, after reading more of the CXF documentation, I realised I was missing a dependency (library on my classpath): cxf-rt-transports-http-hc
. So I added the library in my maven dependencies:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-hc</artifactId>
<version>${cxf.version}</version>
</dependency>
then retried my code, and the output was now instead:
org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit
Voilà!
来源:https://stackoverflow.com/questions/24277043/verifying-cxf-httpasyncclient-use-of-use-async-http-conduit-contextual-property