How do I set the timeout for a JAX-WS webservice client? How do I set the timeout for a JAX-WS webservice client? java java

How do I set the timeout for a JAX-WS webservice client?


I know this is old and answered elsewhere but hopefully this closes this down. I'm not sure why you would want to download the WSDL dynamically but the system properties:

sun.net.client.defaultConnectTimeout (default: -1 (forever))sun.net.client.defaultReadTimeout (default: -1 (forever))

should apply to all reads and connects using HttpURLConnection which JAX-WS uses. This should solve your problem if you are getting the WSDL from a remote location - but a file on your local disk is probably better!

Next, if you want to set timeouts for specific services, once you've created your proxy you need to cast it to a BindingProvider (which you know already), get the request context and set your properties. The online JAX-WS documentation is wrong, these are the correct property names (well, they work for me).

MyInterface myInterface = new MyInterfaceService().getMyInterfaceSOAP();Map<String, Object> requestContext = ((BindingProvider)myInterface).getRequestContext();requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, 3000); // Timeout in millisrequestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, 1000); // Timeout in millismyInterface.callMyRemoteMethodWith(myParameter);

Of course, this is a horrible way to do things, I would create a nice factory for producing these binding providers that can be injected with the timeouts you want.


The properties in the accepted answer did not work for me, possibly because I'm using the JBoss implementation of JAX-WS?

Using a different set of properties (found in the JBoss JAX-WS User Guide) made it work:

//Set timeout until a connection is established((BindingProvider)port).getRequestContext().put("javax.xml.ws.client.connectionTimeout", "6000");//Set timeout until the response is received((BindingProvider) port).getRequestContext().put("javax.xml.ws.client.receiveTimeout", "1000");


Here is my working solution :

// --------------------------// SOAP Message creation// --------------------------SOAPMessage sm = MessageFactory.newInstance().createMessage();sm.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");sm.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");SOAPPart sp = sm.getSOAPPart();SOAPEnvelope se = sp.getEnvelope();se.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");se.setAttribute("xmlns:SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");se.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");se.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");SOAPBody sb = sm.getSOAPBody();// // Add all input fields here ...// SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();// -----------------------------------// URL creation with TimeOut connexion// -----------------------------------URL endpoint = new URL(null,                      "http://myDomain/myWebService.php",                    new URLStreamHandler() { // Anonymous (inline) class                    @Override                    protected URLConnection openConnection(URL url) throws IOException {                    URL clone_url = new URL(url.toString());                    HttpURLConnection clone_urlconnection = (HttpURLConnection) clone_url.openConnection();                    // TimeOut settings                    clone_urlconnection.setConnectTimeout(10000);                    clone_urlconnection.setReadTimeout(10000);                    return(clone_urlconnection);                    }                });try {    // -----------------    // Send SOAP message    // -----------------    SOAPMessage retour = connection.call(sm, endpoint);}catch(Exception e) {    if ((e instanceof com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl) && (e.getCause()!=null) && (e.getCause().getCause()!=null) && (e.getCause().getCause().getCause()!=null)) {        System.err.println("[" + e + "] Error sending SOAP message. Initial error cause = " + e.getCause().getCause().getCause());    }    else {        System.err.println("[" + e + "] Error sending SOAP message.");    }}