3

After my first post dealing with DNS,which is still not resolved: JVM and OS DNS Caching ,i am facing a new problem.

First here is my use case: i want to check if my private DNS is alive. If not i want to use a general DNS (e.g 8.8.8.8).

My private DNS (a bind9 on ubuntu with 192.168.1.188) as a specific record: test.testdnd.fr -> 192.168.1.100

So i thought i could do this:

if(InetAddress.getByAddress(my_dns_ip_in_byte).isReachable()){
    System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");
    System.setProperty("sun.net.spi.nameservice.nameservers", 192.168.1.188);
}else{
    System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");
    System.setProperty("sun.net.spi.nameservice.nameservers", "8.8.8.8");
}
 InetAddress.getHostByAddress(test.testdnd.fr) -> unknow host exception

But if i only Do:

System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");
System.setProperty("sun.net.spi.nameservice.nameservers", 192.168.1.188);
InetAddress.getHostByAddress(test.testdnd.fr) -> 192.168.1.100

So as soon as i called an InetAddress method before setting system.property, it seems that the system.property has no effect. Maybe because the JVM load the system DNS values when a InetAddress method is called even if it dont use DNS resolution.

Is that a standard behavior in the JVM or am i loosing something?

Any help is welcome,

4

2 回答 2

7

问题是名称服务提供程序只加载一次InetAddress并缓存以供进一步使用。加载发生在类的静态初始化程序中InetAddress。因此,一旦InetAddress初始化,设置或更改sun.net.spi.nameservice.provider属性将不再有效。

于 2014-10-15T14:33:31.820 回答
0

JVM 仅使用您在使用系统默认值失败的查找时设置的 DNS 提供程序。您可以在文档(http://docs.oracle.com/javase/7/docs/technotes/guides/net/properties.html)中看到这一点,相关摘录:

默认情况下,Java 将使用系统配置的名称查找机制

您可以指定指向您要使用的 DNS 服务器的 IP 地址的逗号分隔列表

在 JDK 7 中,提供者是链式的,这意味着如果对提供者的查找失败,则会咨询列表中的下一个提供者来解析名称。

于 2013-07-02T16:55:22.840 回答