java实现清理DNS Cache的方法
程序员文章站
2024-03-02 17:48:28
本文实例讲述了java实现清理dns cache的方法。分享给大家供大家参考。具体分析如下:
一、测试环境
os:windows7 x64
jdk:1.6.0_45...
本文实例讲述了java实现清理dns cache的方法。分享给大家供大家参考。具体分析如下:
一、测试环境
os:windows7 x64
jdk:1.6.0_45
二、本人找到四种方式清理jvm的dns缓存,大家可以根据自己的情况选用。
1. 在首次调用inetaddress.getbyname()前,设置java.security.security.setproperty("networkaddress.cache.ttl", "0");
2. 修改jre/lib/security/java.security 下的 networkaddress.cache.ttl 属性
3. jvm启动参数中设置-dsun.net.inetaddr.ttl=0
4. 通过反射清理,如本文的clearcache方法
三、代码
复制代码 代码如下:
package xiaofei;
import java.lang.reflect.field;
import java.net.inetaddress;
import java.net.unknownhostexception;
import java.util.map;
/**
* @author xiaofei.wxf
* @date 13-12-18
*/
public class dnscachetest {
/**
* 1. 在首次调用inetaddress.getbyname()前,设置java.security.security.setproperty("networkaddress.cache.ttl", "0");
* 2. 修改jre/lib/security/java.security 下的 networkaddress.cache.ttl 属性
* 3. jvm启动参数中设置-dsun.net.inetaddr.ttl=0
* 4. 调用clearcache方法清除
*
* @param args
* @throws unknownhostexception
*/
public static void main(string[] args) throws unknownhostexception, nosuchfieldexception, illegalaccessexception {
java.security.security.setproperty("networkaddress.cache.ttl", "0");
inetaddress addr1 = inetaddress.getbyname("www.baidu.com");
system.out.println(addr1.gethostaddress());
//clearcache();
//在下一行设置断点.
//放在此处无效,因为类加载的时候就确定了这个值(应该在使用inetaddress.getbyname之前设置)已经缓存了cache
//java.security.security.setproperty("networkaddress.cache.ttl", "0");
inetaddress addr2 = inetaddress.getbyname("www.baidu.com");
system.out.println(addr2.gethostaddress());
inetaddress addr3 = inetaddress.getbyname("www.google.com");
system.out.println(addr3.gethostaddress());
inetaddress addr4 = inetaddress.getbyname("www.google.com");
system.out.println(addr4.gethostaddress());
//clearcache();
}
public static void clearcache() throws nosuchfieldexception, illegalaccessexception {
//修改缓存数据开始
class clazz = java.net.inetaddress.class;
final field cachefield = clazz.getdeclaredfield("addresscache");
cachefield.setaccessible(true);
final object obj = cachefield.get(clazz);
class cacheclazz = obj.getclass();
final field cachepolicyfield = cacheclazz.getdeclaredfield("type");
final field cachemapfield = cacheclazz.getdeclaredfield("cache");
cachepolicyfield.setaccessible(true);
cachemapfield.setaccessible(true);
final map cachemap = (map)cachemapfield.get(obj);
system.out.println(cachemap);
cachemap.remove("www.baidu.com");
}
}
import java.lang.reflect.field;
import java.net.inetaddress;
import java.net.unknownhostexception;
import java.util.map;
/**
* @author xiaofei.wxf
* @date 13-12-18
*/
public class dnscachetest {
/**
* 1. 在首次调用inetaddress.getbyname()前,设置java.security.security.setproperty("networkaddress.cache.ttl", "0");
* 2. 修改jre/lib/security/java.security 下的 networkaddress.cache.ttl 属性
* 3. jvm启动参数中设置-dsun.net.inetaddr.ttl=0
* 4. 调用clearcache方法清除
*
* @param args
* @throws unknownhostexception
*/
public static void main(string[] args) throws unknownhostexception, nosuchfieldexception, illegalaccessexception {
java.security.security.setproperty("networkaddress.cache.ttl", "0");
inetaddress addr1 = inetaddress.getbyname("www.baidu.com");
system.out.println(addr1.gethostaddress());
//clearcache();
//在下一行设置断点.
//放在此处无效,因为类加载的时候就确定了这个值(应该在使用inetaddress.getbyname之前设置)已经缓存了cache
//java.security.security.setproperty("networkaddress.cache.ttl", "0");
inetaddress addr2 = inetaddress.getbyname("www.baidu.com");
system.out.println(addr2.gethostaddress());
inetaddress addr3 = inetaddress.getbyname("www.google.com");
system.out.println(addr3.gethostaddress());
inetaddress addr4 = inetaddress.getbyname("www.google.com");
system.out.println(addr4.gethostaddress());
//clearcache();
}
public static void clearcache() throws nosuchfieldexception, illegalaccessexception {
//修改缓存数据开始
class clazz = java.net.inetaddress.class;
final field cachefield = clazz.getdeclaredfield("addresscache");
cachefield.setaccessible(true);
final object obj = cachefield.get(clazz);
class cacheclazz = obj.getclass();
final field cachepolicyfield = cacheclazz.getdeclaredfield("type");
final field cachemapfield = cacheclazz.getdeclaredfield("cache");
cachepolicyfield.setaccessible(true);
cachemapfield.setaccessible(true);
final map cachemap = (map)cachemapfield.get(obj);
system.out.println(cachemap);
cachemap.remove("www.baidu.com");
}
}
希望本文所述对大家的java程序设计有所帮助。
下一篇: css静态滤镜