【问题标题】:Throw timeout exception from SOAPException从 SOAPException 引发超时异常
【发布时间】:2018-02-21 14:59:10
【问题描述】:

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


    【解决方案1】:

    以下几行来自call方法的开放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

    【讨论】:

      【解决方案2】:

      经过几个小时的测试,我找到了将 SOAPException 与 Timeout 相关的异常区分开来的正确方法。所以解决方法是取异常的父原因字段,检查是否为SocketTimeoutException的实例。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-20
        • 2014-06-05
        相关资源
        最近更新 更多