" java.net.ConnectException:连接超时"之间的确切区别是什么?和" java.net.SocketTimeoutException:连接超时"?

时间:2015-08-07 17:00:39

标签: java timeout jax-ws httpurlconnection

在我的日志中,我可以找到以下两个堆栈跟踪

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秒结束前发生。

3 个答案:

答案 0 :(得分:1)

我认为我的问题已经回答here

提到的两个例外来自本机代码。正如user5436已经提到的那样,SocketTimeoutExceptions在设置超时并且在所需时间内无法建立连接时发生。在尝试建立连接时发生错误时,抛出ConnectException。令人困惑的是,这也可以是连接超时

答案 1 :(得分:0)

我怀疑没有证据证明其中一个来自connect(..., timeout)路径而另一个来自没有指定连接超时的路径。在前一种情况下,我期望SocketTimeoutException,后者ConnectException

答案 2 :(得分:-1)

它们在网络传输层方面处于较低级别,并且在TCP / IP状态机的不同状态下由错误引发。如果感兴趣,你可以研究TCP套接字图,但是大多数网络应用程序(JAX-WS是)用户或程序员不需要关心网络不工作这些低级细节。

它们来自不同的代码路径,一个是建立http连接,另一个是https。 Https基于SSL,即安全套接字层,它试图用不同的参数连接套接字。

最重要的是,这些代码路径是由其他人编写的,可能是古代程序员。当时他们决定抛出一个Exception而不是另一个。我们不必为此过分夸大其词。