Package org.jmxtrans.agent.util
Class CachingReference<E>
java.lang.Object
org.jmxtrans.agent.util.CachingReference<E>
public abstract class CachingReference<E>
extends java.lang.Object
Maintains a cached version of the
Object that it holds and handle the renewal of this object upon expiration.
Greatly inspired by the CachedData sample provided in the javadoc
of ReentrantReadWriteLock.
Object is created implementing the newObject() method.
Sample to get an InetAddress refreshed against a DNS every 10 seconds:
CachingReference myRemoteServerAddress = new CachingReference<InetAddress>(10, TimeUnit.SECONDS) {
protected InetAddress newObject() {
try {
return InetAddress.getByName(myRemoteServerHostname);
} catch () {
throw new RuntimeException("Exception resolving '" + myRemoteServerHostname + "'", e);
}
}
}
- Author:
- Cyrille Le Clerc
-
Constructor Summary
Constructors Constructor Description CachingReference(long timeToLiveInNanos)CachingReference(long timeToLive, java.util.concurrent.TimeUnit timeToLiveUnit) -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Constructor Details
-
CachingReference
public CachingReference(long timeToLiveInNanos) -
CachingReference
public CachingReference(long timeToLive, java.util.concurrent.TimeUnit timeToLiveUnit)
-
-
Method Details
-
newObject
- Returns:
- the newly created object.
-
get
- Returns:
- the up to date version of the
Objecthold by this reference.
-
purge
public void purge()Purge the cached reference so that a new object will be created for the nextget() -
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-