从SOAPException抛出超时异常

问题描述 投票:1回答:2

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


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;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.