In the JDK 8 the java.net.InetAddress resolves host names using the local machine's default host name resolver by default:
Host name-to-IP address resolution is accomplished through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and Network Information Service(NIS). The particular naming services(s) being used is by default the local machine configured one. For any host name, its corresponding IP address is returned. [source]
In the JDK 19 the java.net.InetAddress works the same way but there is also the option to use a custom configuration:
The built-in InetAddress resolver implementation does host name-to-IP address resolution and vice versa through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and the Lightweight Directory Access Protocol (LDAP). The particular naming services that the built-in resolver uses by default depends on the configuration of the local machine. InetAddress has a service provider mechanism for InetAddress resolvers that allows a custom InetAddress resolver to be used instead of the built-in implementation. [source]
So how can we configure this behavior without modifying the local machine's default host name resolver in a universal way that works for Java 8 and later?
In this situation Burningwave Tools comes to our aid by providing us with the HostResolutionRequestInterceptor
that allows us:
- to use a custom resolver instead the default one
- to prepend a custom resolver to the default one
- to postpone a custom resolver to the default one
To include Burningwave Tools in our project we need to add the following dependency to our pom.xml:
And to install the HostResolutionRequestInterceptor component we must proceed as follows:
Burningwave Tools provides also a DNS server connection based host resolver:
But you can also define a new custom resolver by implementing the HostResolver interface that create DNS packet requests, send them to the servers through the java.net.DatagramSocket and parse the responses:
If we add the Jackson framework to our project we can add the configuration via yaml file support with a few lines of code by using the PathHelper
component:
From here you can download/clone the tutorial shared on GitHub.
Top comments (0)