Setting timeout on generated JAXWS CXF Clients
When generating client with the CXF (2.2.2 in this case, should apply to all…) Java API without any configuration file, here is the way to set the client timeout :
package org.ow2.petals.kernel.ws.client; import org.apache.cxf.endpoint.Client; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.transport.http.HTTPConduit; import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; import org.ow2.petals.kernel.ws.api.RuntimeService; public class Main { private void createService() { long timeout = 10000L; JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(RuntimeService.class); factory.setAddress("http://localhost:9999/service/Runtime"); RuntimeService runtimeService = (RuntimeService) factory.create(); Client client = ClientProxy.getClient(runtimeService); if (client != null) { HTTPConduit conduit = (HTTPConduit) client.getConduit(); HTTPClientPolicy policy = new HTTPClientPolicy(); policy.setConnectionTimeout(timeout); policy.setReceiveTimeout(timeout); conduit.setClient(policy); } } }
Note that here the RuntimeService class is my JAXWS annotated class.
This doesn’t work if your web services is on a remote machine … but on localhost it’s okay