Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting socket read timeout with javax.xml.soap.SOAPConnection

Tags:

java

saaj

I am using the javax.xml.soap API (javax.xml.soap.SOAPConnectionFactory, javax.xml.soap.SOAPConnection, and friends) to make a web service call to a remote server, for the most part with great success.

However, sometimes there is a problem and the program gets stuck reading forever.

To address this, I'd like to add a read timeout.

I found several ways it might be possible to achieve this, but they all seemed pretty bad.

So my question to the community is: What is the best way to implement a read timeout behaviour when using the javax.xml.soap API to make a call?

like image 253
Samuel Edwin Ward Avatar asked Mar 02 '12 16:03

Samuel Edwin Ward


People also ask

How do you set a timeout in soap request?

We have two timeout parameters: CONNECTION_TIMEOUT - sets max connection time to web service (milliseconds). SO_TIMEOUT - sets read\write time on web service. It is the time to complete a request and response cycle.

How do you set a webserver timeout in Java?

You need to cast your port object to a BindingProvider and then retrieve the request context, which is a Java Map of key/value pairs. Then, you set the connect and read timeout properties to whatever values you wish to use - this is specified in milliseconds.

What is connection request timeout?

request timeout — a time period required to process an HTTP call: from sending a request to receiving a response. connection timeout — a time period in which a client should establish a connection with a server. socket timeout — a maximum time of inactivity between two data packets when exchanging data with a server.


2 Answers

You have to create your own URLStreamHandler so that you can set URLConnection parameters like connection timeout and read timeout.

SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
URL endpoint =
  new URL(new URL("http://yourserver.yourdomain.com/"),
          "/path/to/webservice",
          new URLStreamHandler() {
            @Override
            protected URLConnection openConnection(URL url) throws IOException {
              URL target = new URL(url.toString());
              URLConnection connection = target.openConnection();
              // Connection settings
              connection.setConnectTimeout(10000); // 10 sec
              connection.setReadTimeout(60000); // 1 min
              return(connection);
            }
          });

SOAPMessage result = connection.call(soapMessage, endpoint);

I have removed some try/catch for clarity.

like image 129
Yves Martin Avatar answered Oct 21 '22 10:10

Yves Martin


import com.sun.xml.internal.ws.client.BindingProviderProperties

public someResponse callWebService() {

    MyPort port = new Service().getPort();

    Map<String, Object> requestContext = ((BindingProvider) port).getRequestContext();

    requestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, 10 * 1000); //10 secs

    requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, 1 * 60 * 1000); //1 min

    return port.someWebMethod();

}
like image 24
Zaki Avatar answered Oct 21 '22 10:10

Zaki