在我的日志中,我可以找到以下两个堆栈跟踪
Caused by: java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1168)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1147)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:932)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1282)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1257)
at com.sun.xml.internal.ws.transport.http.client.HttpClientTransport.getOutput(HttpClientTransport.java:104) ... 127 more
和
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:656)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:275)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1147)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1282)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1257)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at com.sun.xml.internal.ws.transport.http.client.HttpClientTransport.getOutput(HttpClientTransport.java:104) ... 50 more
这两种异常都发生在使用JAXWS-RI(参考实现)的web服务调用期间。这两个例外的确切区别是什么?
为了配置超时,我在上下文中设置了一些属性,其中一个是连接超时:
BindingProvider bindingProvider = (BindingProvider) port;
Map<String, Object> context = bindingProvider.getRequestContext();
context.put(JAXWSProperties.REQUEST_TIMEOUT, 90000);
context.put(JAXWSProperties.CONNECT_TIMEOUT, 90000);
就我从日志中看到的,只有java.net.SocketTimeoutException
似乎尊重JAXWSProperties.CONNECT_TIMEOUT
的设置。
java.net.ConnectExceptions
在90秒前出现
我想我的问题已经在这里得到了回答。
提到的两个异常来自本机代码。正如user5436已经提到的,当设置了超时并且在期望的时间内无法建立连接时,就会发生SocketTimeoutExceptions
。当尝试建立连接时发生错误时,抛出ConnectException
。令人困惑的是,这也可以是连接超时。
我怀疑没有证据表明其中一个来自connect(..., timeout)
路径,另一个来自没有指定连接超时的路径。在前一种情况下,我期望SocketTimeoutException
,在后者ConnectException
。
它们在网络传输层方面处于较低的级别,并且由TCP/IP状态机在不同状态下的错误抛出。如果有兴趣,您可以研究TCP套接字图,但大多数网络应用程序(JAX-WS就是)用户或程序员不需要关心这些底层细节,只关心网络不工作的事实。
它们来自不同的代码路径,一个是建立http连接,另一个是https。Https基于SSL,即安全套接字层,它试图用不同的参数连接套接字。
底线是这些代码路径是由其他人写的,可能是古代的程序员。当时他们决定抛出一个异常而不是另一个。我们不必太小题大做。