I was wondering if it is possible to provide a custom DNS lookup implementation on java.net.URL - my DNS hosting provider becomes unstable at certain times of the day, and then DNS queries do not work for several minutes, but if I manually configure the corresponding domains in file of my hosts, they work fine, so I want your DNS level to have some kind of DNS cache, if the DNS lookup is complete, refresh the cache if it failed, go back to the cached IP address and open URLConnection on this IP address
This is my implementation of the url connection:
URL endpoint = new URL(null, url, new URLStreamHandler() { @Override protected URLConnection openConnection(URL url) throws IOException { URL target = new URL(url.toString()); URLConnection connection = target.openConnection();
I looked at Oracle proxies, but could not see any direct way to do my own DNS lookups at the software level.
Limitations :
1: it should work in Java6 (maybe Java7, but the client will not switch to Java8 any time soon)
2: Unable to add JVM arguments
3: I do not own these endpoints, so replacing the host name with an IP address is not a solution, as load balancers will serve different contents / APIs depending on whether you come from the host name or IP address. For example: mail.google.com is allowed until 216.58.223.37, going to this IP address will serve google.com content, not mail.google.com content, since both services are sitting on the same load balancer using the same IP -address.
4: I do not know how many DNS DNS permissions I will need to cache, but I know that it will be no more than 1000. The ideal solution is to have DNS permissions in a static hashmap, if any DNS resolution is successful, update the hash map if it failed, use the DNS resolution in hashmap.
5: If there is a native Java solution, I would prefer using JNI - Understanding host name resolution and DNS behavior in Java