In the java.net package description, a brief description of some of the key interfaces. Which is responsible for networking identifiers is addresses. The concrete implementation class of this class is inetaddress, the bottom encapsulates the similarities and differences between inet4address and inet6address, can be regarded as a facade tool class.
- A low Level API, which deals with the following abstractions:
- Addresses, which is networking identifiers, like IP Addresses.
- Sockets, which is basic bidirectional data communication mechanisms.
- Interfaces, which describe network Interfaces.
Copy Code
In OpenJDK's InetAddress source code section, the IP is parsed according to DNS or hostname:
- private static inetaddress[] GETALLBYNAME0 (String host, InetAddress reqaddr, Boolean check)
- Throws Unknownhostexception {
- /* If It gets here it's presumed to be a hostname */
- /* Cache.get can return:null, unknownaddress, or inetaddress[] */
- /* Make sure the connection to the host is allowed, before we
- * Give out a hostname
- */
- if (check) {
- SecurityManager security = System.getsecuritymanager ();
- if (Security! = null) {
- Security.checkconnect (host,-1);
- }
- }
- Inetaddress[] addresses = getcachedaddresses (host);
- /* If No entry in cache, then do the host lookup */
- if (addresses = = null) {
- Addresses = Getaddressesfromnameservice (host, reqaddr);
- }
- if (addresses = = Unknown_array)
- throw new Unknownhostexception (host);
- return Addresses.clone ();
- }
Copy Code
The
Key two methods are:
Getcachedaddresses (host);
Getaddressesfromnameservice (host, reqaddr); The
parses the cached IP from Addresscache, or Negativecache according to Dns/hostname. The
then iterates through the nameservices, calls each Nameservice's LOOKUPALLHOSTADDR (host) to find the IP, and then caches the HOST:IP to the previous cache.
based on the above, there are two ways to dynamically resolve DNS:
1. Reflect Addresscache, or negativecache, and put Host:ip through the cache's put () method.
2. Reflect the nameservices and put the Nameservice instance of the agent into.
Two ways to do this:
1. Addresscache, or Negativecache, are java.net.InetAddress.Cache, and their internal cacheentry are affected by the two sets of JVM options:
Networkaddress.cache.ttl
Networkaddress.cache.negative.ttl
After the TTL, CacheEntry get () returns only null.
2. Nameservices is only a OPENJDK implementation. In other words, just the sun family. The other JDK does not use this property name.
Write the snippet code to see the properties inside the JRockit and IBM JVM inetaddress:
- class<inetaddress> type = Inetaddress.class;
- field[] fields = Type.getdeclaredfields ();
- for (Field f:fields) {
- System.out.println (F.getname () + ":" + F.gettype ());
- }
Copy Code
OpenJDK:
- IPv4
- IPv6
- Preferipv6address
- Holder
- Nameservices
- Canonicalhostname
- Serialversionuid
- Addresscache
- Negativecache
- Addresscacheinit
- Unknown_array
- Impl
- LookupTable
- Cachedlocalhost
- CacheTime
- Maxcachetime
- Cachelock
- Fields_offset
- UNSAFE
- Serialpersistentfields
- $assertionsDisabled
Copy Code
JRockit:
- IPv4
- IPv6
- Preferipv6address
- HostName
- Address
- Family
- Nameservice
- Canonicalhostname
- Serialversionuid
- Addresscache
- Negativecache
- Addresscacheinit
- Unknown_array
- Impl
- LookupTable
- $assertionsDisabled
Copy Code
IBM JDK
- Ipv4:int
- Ipv6:int
- Preferipv6address
- Hostname:class java.lang.String
- Address:int
- Family:int
- Nameservice:interface Sun.net.spi.nameservice.NameService
- Canonicalhostname:class java.lang.String
- Serialversionuid:long
- Addresscache:class Java.net.inetaddress$cache
- Negativecache:class Java.net.inetaddress$cache
- Localhostname:class java.lang.String
- Localhostnamelock:class Java.lang.Object
- Cachelocalhost:boolean
- Addresscacheinit:boolean
- Unknown_array:class [Ljava.net.InetAddress;
- Impl:interface Java.net.InetAddressImpl
- Lookuptable:class Java.util.HashMap
- $assertionsDisabled: Boolean
Copy Code
See here, know the egg hurts. Three types of JDK,
OpenJDK is Nameservices is a list<nameservice>
JRockit and the IBM JVM are Nameservice, just a separate nameservice instance.
So with a 2nd approach, you should at least meet the needs of these three mainstream JDK types.
Simple implementation of two practices:
Procedure 1, dynamically replaces the Addresscache.
Procedure 2, dynamic proxy nameservice.
The source code is as follows
- Length limit, source view Huitie.
Copy Code
Temporarily test to this bar, interested students can be perfect together. Strive to meet OpenJDK, Jrockit, IBM JDK Three mainstream environment DNS dynamic resolution class.
Java dynamic replacement for DNS in inetaddress simple analysis 1