Setting Java DNS cache TTL

本秂侑毒 提交于 2019-12-23 11:55:10

问题


I'm attempting to change the DNS cache timeout in Java 1.6. I see discussion here of using something like the following:

java.security.Security.setProperty ("networkaddress.cache.ttl" , TTL_SECS);

But I've tried this simple test in Win 7....

System.out.println("DEFAULT DNS TTL: "+sun.net.InetAddressCachePolicy.get());
java.security.Security.setProperty ("networkaddress.cache.ttl" , "123");    
System.out.println("DEFAULT DNS TTL: "+sun.net.InetAddressCachePolicy.get());

... and the output doesn't change. It seems this can be changed in the Java installation's security properties but I preffer to keep this in the code for neatness. Any ideas how to achieve that?

Thanks.


回答1:


These are not system properties: they are set in the java.security file. For the corresponding system properties, which are non-preferred, see 'Sun implementation-specific properties' in Networking Properties.




回答2:


Try this and see the output you get. The property needs to be set when the class is loaded.

static {
    java.security.Security.setProperty ("networkaddress.cache.ttl" , "12");    
}
public static void main(String[] args) {
    System.out.println("DEFAULT DNS TTL: "+sun.net.InetAddressCachePolicy.get());
    java.security.Security.setProperty ("networkaddress.cache.ttl" , "123");    
    System.out.println("DEFAULT DNS TTL: "+sun.net.InetAddressCachePolicy.get());
}



回答3:


In Android 4.0 (Ice Cream Sandwich) and earlier, DNS caching was performed both by InetAddress and by the C library, which meant that DNS TTLs could not be honored correctly. In later releases, caching is done solely by the C library and DNS TTLs are honored.

Google desc



来源:https://stackoverflow.com/questions/12099526/setting-java-dns-cache-ttl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!