Setting Java DNS cache TTL
These are not system properties: they are set in the java.security file. For the corresponding system properties, which are non-preferred, see 'Sun implementation-specific properties' in Networking Properties.
Does JVM cache url because of networkaddress.cache.ttl setting?
Default behaviour of JVM is to cache forever(setting -1) if security manager is installed. So you have to explicitly set the ttl timeout so that reattempt for DNS resolution happens. You may set this property via security manager in your application startup (app level) or system level.
java.security.Security.setProperty("networkaddress.cache.ttl" , "10");
For system wide edit below file to include ttl to zero.
<JAVA_HOME>/jre/lib/security/java.security
networkaddress.cache.ttl=0
Without security manager:
Usually default value depends os & jvm type. AWS uses 60sec, Refer this
PS: System level settings always override app level settings.
How to implement internal DNS caching within simple Java web proxy?
Here are two classes I wrote to solve this problem
DnsCache
import java.net.InetAddress;
/**
* Singleton class that holds the ExpiringMap used for DNS Cache
*/
public class DnsCache {
private static ExpiringMap<String, InetAddress> theDnsCache = null;
public static ExpiringMap<String, InetAddress> getInstance() {
if (theDnsCache == null) {
theDnsCache = new ExpiringMap<String, InetAddress>();
}
return theDnsCache;
}
}
ExpiringMap
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/**
* A HashMap that removes objects after the set TTL
*/
public class ExpiringMap<K, V> implements Map<K, V> {
private final int DEFAULT_TTL = 30;
private final HashMap<K, ScheduledFuture<K>> cacheKeys = new HashMap<K, ScheduledFuture<K>>();
private final HashMap<K, V> theHash = new HashMap<K, V>();
private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
@Override
public int size() {
// TODO Auto-generated method stub
return theHash.size();
}
@Override
public boolean isEmpty() {
return theHash.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return theHash.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return theHash.containsValue(value);
}
/**
* If retrieving an object that already exists in the cache
* we must resets the TTL timer on that item.
*/
@SuppressWarnings("unchecked")
@Override
public V get(final Object key) {
// If item exists in cache, then we cancel and reset the timer on that item
if(cacheKeys.containsKey(key) && cacheKeys.get(key).cancel(false)) {
ScheduledFuture<K> task = executorService.schedule(new Callable<K>() {
@Override
public K call() {
cacheKeys.remove(key);
theHash.remove(key);
return (K) key;
}
}, DEFAULT_TTL, TimeUnit.SECONDS);
cacheKeys.put((K) key, task);
}
return theHash.get(key);
}
/**
* When we add an item to cache we add the key to a map that contains FutureScheduled
* events that will remove the item after TTL
*/
@Override
public V put(final K key, V value) {
ScheduledFuture<K> task = executorService.schedule(new Callable<K>() {
@Override
public K call() {
cacheKeys.remove(key);
theHash.remove(key);
return key;
}
}, DEFAULT_TTL, TimeUnit.SECONDS);
cacheKeys.put(key, task);
return theHash.put(key, value);
}
@Override
public V remove(Object key) {
cacheKeys.remove(key);
return theHash.remove(key);
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
// TODO implement
}
@Override
public void clear() {
cacheKeys.clear();
theHash.clear();
}
@Override
public Set<K> keySet() {
return theHash.keySet();
}
@Override
public Collection<V> values() {
return theHash.values();
}
@Override
public Set<java.util.Map.Entry<K, V>> entrySet() {
return theHash.entrySet();
}
}
Related Topics
Java Streams: Replacing Groupingby and Reducing by Tomap
Swing's Keylistener and Multiple Keys Pressed at the Same Time
Resizing Icon to Fit on Jbutton in Java
Problem with "Scopes" of Variables in Try Catch Blocks in Java
Highlighting Strings in Javafx Textarea
Jersey 415 Unsupported Media Type
Providing Input/Subcommands to Command Executed Over Ssh with Jsch
Jtable Model Listener Detects Inserted Rows Too Soon (Before They Are Drawn)
Selenium Switch Focus to Tab, Which Opened After Clicking Link
Differencebetween @Inject and @Autowired in Spring Framework? Which One to Use Under What Condition
Run a Single Test Method with Maven
Building Executable Jar with Maven
Java 8 Date and Time: Parse Iso 8601 String Without Colon in Offset
How to Create Sparksession with Hive Support (Fails with "Hive Classes Are Not Found")
Converting an Array of Objects to an Array of Their Primitive Types