Thursday, October 24, 2013

Setting a timeout on an Apache CXF webservice call

In this short post, I'll provide the code for setting a timeout on a webservice call. The client has been generated by Apache CXF 2.6.1, and the webservice is accessed by an HTTP call.

Client client = ClientProxy.getClient(yourPortType);

HTTPConduit httpConduit = (HTTPConduit) client.getConduit();

HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(5000);
httpClientPolicy.setReceiveTimeout(5000);

httpConduit.setClient(httpClientPolicy);

You should add this chunk of code before you call the method on yourPortType. The connectionTimeout is how long it may take to connect to the service, the receiveTimeout is how long it may take for the webservice to respond.