(Sorry for the earlier accidental post)
Hello,
I'm having some issues with the WebClient provided with CXF 2.7.8. We're
using the below code to connect to a web service through SSL and non-SSL
but my question is mostly concerning SSL.
This client is part of a Tomcat web service which uses CXF. What that web
service does is, once a request comes in, it processes it and converts it
into another structure to be used by the WebClient below. Then, the
WebClient sends this new request (with a protobuf object) to another web
service. This connection from our web service to the other web service
needs to be SSL. This is where we had the issue.
At first we were using non-SSL urls and it was working fine but once we
started using SSL, the connections started closing with each request. We
made sure that the web service we're connecting to is not closing our
connections. I also observed the traffic through Wireshark and saw that
after each request, connection is closed.
To verify my observation, I wrote a similar client code using
HttpURLConnection. I re-implemented everything same with HttpURLConnection
(same keep-alive settings etc, I can provide the code for that too if need
be) and captured the traffic through Wireshark again. However, this time I
only saw one connection opening for multiple requests (only one
'Client/Server Hello').
When I read through CXF documentation, I remember that it mentioned reusing
connections, keep-alive setting already there etc, but my code does not
reflect that.
Any thoughts about this will be really valuable. Is there something I'm
missing about keeping the connection alive for WebClient with SSL?
Thanks,
Aysun
The code is cleaned up and only related parts are left for reference:
import org.apache.cxf.jaxrs.client.WebClient;
public class ProtoClient {
public ProtoClient(String baseAddress) {
client = WebClient.create(baseAddress);
public WebClient getClient() {
return client;
...
public GeneratedMessage submitToWS(Class<?> responseClass, GeneratedMessage
request) throws MyException {
...
response = this.client.post(request.toByteArray());
...
in = response.readEntity(InputStream.class);
...
try {
parseFrom = responseClass.getDeclaredMethod("parseFrom", new Class[] {
InputStream.class });
resp = (GeneratedMessage) parseFrom.invoke(null, in);
} catch (Exception e) {
logger.error("Exception",e);
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
logger.error("IOException : Cannot close the inputstream",e);
public static ProtoClient getUserClient() {
...
String API = GetConfigurations.getEddieConfig("API_NAME");
if(SSL) {
URL_BASE = GetConfigurations.getEnvConfig("URL_BASE_SSL");
} else {
URL_BASE = GetConfigurations.getEnvConfig("URL_BASE");
ProtoClient userClient = new ProtoClient(URL_BASE);
WebClient client = userClient.getClient();
client.path(API);
//with URL_BASE and API; it ends up being sth like this:
http://www.webservice.com/api/v1/api-name<http://api-sandbox.myws.com/api/v2/api-name>
client.accept(MediaType.APPLICATION_OCTET_STREAM_TYPE);
// accepts protobuf objects
client.type(MediaType.APPLICATION_OCTET_STREAM_TYPE);
WebClient.getConfig(client).getHttpConduit().getClient().setConnectionTimeout(
CONNECTION_TIMEOUT_IN_MS);
WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(
TIMEOUT_IN_MS);
WebClient.getConfig(client).getHttpConduit().getClient().setConnection(ConnectionType.
KEEP_ALIVE);
...
return userClient;
Usage:
GeneratedMessage resp =
ProtoClientFactory.getUserClient().submitToWS(UserResponse.class,
newRequest.writeProto());
Hello,
I'm having some issues with the WebClient provided with CXF 2.7.8. We're
using the below code to connect to a web service through SSL and non-SSL
but my question is mostly concerning SSL.
This client is part of a Tomcat web service which uses CXF. What that web
service does is, once a request comes in, it processes it and converts it
into another structure to be used by the WebClient below. Then, the
WebClient sends this new request (with a protobuf object) to another web
service. This connection from our web service to the other web service
needs to be SSL. This is where we had the issue.
At first we were using non-SSL urls and it was working fine but once we
started using SSL, the connections started closing with each request. We
made sure that the web service we're connecting to is not closing our
connections. I also observed the traffic through Wireshark and saw that
after each request, connection is closed.
To verify my observation, I wrote a similar client code using
HttpURLConnection. I re-implemented everything same with HttpURLConnection
(same keep-alive settings etc, I can provide the code for that too if need
be) and captured the traffic through Wireshark again. However, this time I
only saw one connection opening for multiple requests (only one
'Client/Server Hello').
When I read through CXF documentation, I remember that it mentioned reusing
connections, keep-alive setting already there etc, but my code does not
reflect that.
Any thoughts about this will be really valuable. Is there something I'm
missing about keeping the connection alive for WebClient with SSL?
Thanks,
Aysun
The code is cleaned up and only related parts are left for reference:
import org.apache.cxf.jaxrs.client.WebClient;
public class ProtoClient {
public ProtoClient(String baseAddress) {
client = WebClient.create(baseAddress);
public WebClient getClient() {
return client;
...
public GeneratedMessage submitToWS(Class<?> responseClass, GeneratedMessage
request) throws MyException {
...
response = this.client.post(request.toByteArray());
...
in = response.readEntity(InputStream.class);
...
try {
parseFrom = responseClass.getDeclaredMethod("parseFrom", new Class[] {
InputStream.class });
resp = (GeneratedMessage) parseFrom.invoke(null, in);
} catch (Exception e) {
logger.error("Exception",e);
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
logger.error("IOException : Cannot close the inputstream",e);
public static ProtoClient getUserClient() {
...
String API = GetConfigurations.getEddieConfig("API_NAME");
if(SSL) {
URL_BASE = GetConfigurations.getEnvConfig("URL_BASE_SSL");
} else {
URL_BASE = GetConfigurations.getEnvConfig("URL_BASE");
ProtoClient userClient = new ProtoClient(URL_BASE);
WebClient client = userClient.getClient();
client.path(API);
//with URL_BASE and API; it ends up being sth like this:
http://www.webservice.com/api/v1/api-name<http://api-sandbox.myws.com/api/v2/api-name>
client.accept(MediaType.APPLICATION_OCTET_STREAM_TYPE);
// accepts protobuf objects
client.type(MediaType.APPLICATION_OCTET_STREAM_TYPE);
WebClient.getConfig(client).getHttpConduit().getClient().setConnectionTimeout(
CONNECTION_TIMEOUT_IN_MS);
WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(
TIMEOUT_IN_MS);
WebClient.getConfig(client).getHttpConduit().getClient().setConnection(ConnectionType.
KEEP_ALIVE);
...
return userClient;
Usage:
GeneratedMessage resp =
ProtoClientFactory.getUserClient().submitToWS(UserResponse.class,
newRequest.writeProto());