Throw timeout exception from SOAPException

1.8k Views Asked by At

I am trying to throw a timeout exception in the code below. I tried a simple condition but it's not the proper way. My question is how can I distinct the timeout exception from SOAPException?

URL endpoint = new URL(null,
    urlStr,
    new URLStreamHandler() {
      // The url is the parent of this stream handler, so must create clone
      protected URLConnection openConnection(URL url) throws IOException {
        URL cloneURL = new URL(url.toString());
        HttpURLConnection cloneURLConnection = (HttpURLConnection) cloneURL.openConnection();
        // TimeOut settings
        cloneURLConnection.setConnectTimeout(10000);
        cloneURLConnection.setReadTimeout(10000);
        return cloneURLConnection;
      }
    });

try {
  response = connection.call(request, endpoint);
} catch (SOAPException soapEx) {
  if(soapEx.getMessage().contains("Message send failed")) {
    throw new TimeoutExpirationException();
  } else {
    throw soapEx;
  }
}
2

There are 2 best solutions below

0
pik4 On BEST ANSWER

After some hours of testing I found the proper way to distict the SOAPException from Timeout related exceptions. So the solution is to take the parent cause field of the exception and check if it's an instance of SocketTimeoutException.

try {
  response = connection.call(request, endpoint);
} catch (SOAPException soapEx) {
  if(soapEx.getCause().getCause() instanceof SocketTimeoutException) {
    throw new TimeoutExpirationException(); //custom exception
  } else {
    throw soapEx;
  }
}
0
miskender On

The following lines are from open jdk source code of call method. In the code they are only catching with Exception (also with chaining? comment). I don't think there is other way unless Oracle jdk handles this differently.
You can still try something like if(soapEx.getCause() instanceof SomeTimeoutException) (not sure if this will work)

            try {
                SOAPMessage response = post(message, (URL)endPoint);
                return response;
            } catch (Exception ex) {
                // TBD -- chaining?
                throw new SOAPExceptionImpl(ex);
            } 

If you want to check the source code HttpSoapConnection