查找主机时如何设置套接字超时? [英] How to set the timeout for socket when looking for host?

查看:161
本文介绍了查找主机时如何设置套接字超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的单元测试中,我使用commons httpclient来请求不正确的远程网站,最大超时时间是:

In my unit test, I use commons httpclient to request a incorrect remote web site, and the max timeout is:

@Test(timeout = 10000)
public void should_not_be_accessible_if_configuration_is_incorrect() throws Exception {
     // use httpclient to visit an invalid remote http web site
}

但它可能会在某些计算机中抛出此类异常:

But it may throw such an exception in some computers:

java.lang.Exception: test timed out after 10000 milliseconds
at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:894)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1286)
at java.net.InetAddress.getAllByName0(InetAddress.java:1239)
at java.net.InetAddress.getAllByName(InetAddress.java:1155)
at java.net.InetAddress.getAllByName(InetAddress.java:1091)
at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:44)
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:102)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:314)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:357)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:218)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:194)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)

似乎它阻塞了方法: java.net.Inet4AddressImpl.lookupAllHostAddr()

我想知道有没有办法为它设置最大超时?

I wonder is there any way to set a max timeout for it?

推荐答案

您可以通过描述的超时属性这里

You can control Java's DNS timeouts via the timeout properties described here:

com.example.jndi.dns.timeout.initial
com.example.jndi.dns.timeout.retries

这篇关于查找主机时如何设置套接字超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆