Class NodeCache
- java.lang.Object
-
- org.apache.curator.framework.recipes.cache.NodeCache
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class NodeCache extends java.lang.Object implements java.io.CloseableA utility that attempts to keep the data from a node locally cached. This class will watch the node, respond to update/create/delete events, pull down the data, etc. You can register a listener that will get notified when changes occur.
IMPORTANT - it's not possible to stay transactionally in sync. Users of this class must be prepared for false-positives and false-negatives. Additionally, always use the version number when updating data to avoid overwriting another process' change.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()org.apache.curator.framework.CuratorFrameworkgetClient()ChildDatagetCurrentData()Return the current data.org.apache.curator.framework.listen.ListenerContainer<NodeCacheListener>getListenable()Return the cache listenablejava.lang.StringgetPath()Return the path this cache is watchingprotected voidhandleException(java.lang.Throwable e)Default behavior is just to log the exceptionvoidrebuild()NOTE: this is a BLOCKING method.voidstart()Start the cache.voidstart(boolean buildInitial)Same asstart()but gives the option of doing an initial build
-
-
-
Constructor Detail
-
NodeCache
public NodeCache(org.apache.curator.framework.CuratorFramework client, java.lang.String path)- Parameters:
client- curztor clientpath- the full path to the node to cache
-
NodeCache
public NodeCache(org.apache.curator.framework.CuratorFramework client, java.lang.String path, boolean dataIsCompressed)- Parameters:
client- curztor clientpath- the full path to the node to cachedataIsCompressed- if true, data in the path is compressed
-
-
Method Detail
-
getClient
public org.apache.curator.framework.CuratorFramework getClient()
-
start
public void start() throws java.lang.ExceptionStart the cache. The cache is not started automatically. You must call this method.- Throws:
java.lang.Exception- errors
-
start
public void start(boolean buildInitial) throws java.lang.ExceptionSame asstart()but gives the option of doing an initial build- Parameters:
buildInitial- if true,rebuild()will be called before this method returns in order to get an initial view of the node- Throws:
java.lang.Exception- errors
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
getListenable
public org.apache.curator.framework.listen.ListenerContainer<NodeCacheListener> getListenable()
Return the cache listenable- Returns:
- listenable
-
rebuild
public void rebuild() throws java.lang.ExceptionNOTE: this is a BLOCKING method. Completely rebuild the internal cache by querying for all needed data WITHOUT generating any events to send to listeners.- Throws:
java.lang.Exception- errors
-
getCurrentData
public ChildData getCurrentData()
Return the current data. There are no guarantees of accuracy. This is merely the most recent view of the data. If the node does not exist, this returns null- Returns:
- data or null
-
getPath
public java.lang.String getPath()
Return the path this cache is watching- Returns:
- path
-
handleException
protected void handleException(java.lang.Throwable e)
Default behavior is just to log the exception- Parameters:
e- the exception
-
-