Java implementation of DNS Cache cleaning methods

  • 2020-04-01 03:37:07
  • OfStack

This article illustrates the Java implementation of DNS Cache cleaning methods. Share with you for your reference. Specific analysis is as follows:

I. test environment

OS: Windows 7 x64

The JDK: 1.6.0 _45

Two, I found four ways to clean up the JVM's DNS cache, you can choose according to their own situation.

1. In the first call InetAddress. GetByName (before), set up the Java security. Security. The setProperty (" networkaddress. Cache. TTL ", "0");
2. Modify the jre/lib/security/Java security under the networkaddress. Cache. TTL attributes
3. The JVM startup - Dsun.net.inetaddr.ttl=0 parameter Settings
Clean through reflection, as in this article's clearCache method

Three, code,

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. On the first call InetAddress.getByName() Before setting java.security.Security.setProperty("networkaddress.cache.ttl", "0");
     * 2. Modify the jre/lib/security/java.security Under the networkaddress.cache.ttl attribute
     * 3. jvm Set in the startup parameter -Dsun.net.inetaddr.ttl=0
     * 4. call clearCache Methods to remove
     *
     * @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(); 
        //Set a breakpoint on the next line. < br / >         //This is not valid because the value is already cached when the class is loaded (it should be set before inetaddress.getbyname is used); < br / >         //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 { 
        //Modify the cache data start & NBSP; < br / >         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"); 
    } 
}

I hope this article has been helpful to your Java programming.


Related articles: