从SOAPException抛出超时异常

时间:2018-02-21 14:59:10

标签: java exception soap error-handling soapexception

我试图在下面的代码中抛出超时异常。我尝试了一个简单的条件,但这不是正确的方法。 我的问题是如何区分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 个答案:

答案 0 :(得分:0)

以下几行来自call方法的open jdk源代码。在代码中,他们只使用Exception(也包含链接?注释)。除非Oracle jdk以不同的方式处理这个问题,否则我认为还有其他方法 您仍然可以尝试if(soapEx.getCause() instanceof SomeTimeoutException)之类的内容(不确定这是否有效)

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

如果您想查看源代码HttpSoapConnection

答案 1 :(得分:0)

经过几个小时的测试后,我找到了从Timeout相关异常中分离SOAPException的正确方法。因此,解决方案是获取异常的父原因字段,并检查它是否是SocketTimeoutException的实例。

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