Class TreeCache

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class TreeCache
    extends java.lang.Object
    implements java.io.Closeable

    A utility that attempts to keep all data from all children of a ZK path locally cached. This class will watch the ZK path, 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.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  TreeCache.Builder  
    • Constructor Summary

      Constructors 
      Constructor Description
      TreeCache​(org.apache.curator.framework.CuratorFramework client, java.lang.String path)
      Create a TreeCache for the given client and path with default options.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close/end the cache.
      java.util.Map<java.lang.String,​ChildData> getCurrentChildren​(java.lang.String fullPath)
      Return the current set of children at the given path, mapped by child name.
      ChildData getCurrentData​(java.lang.String fullPath)
      Return the current data for the given path.
      org.apache.curator.framework.listen.Listenable<TreeCacheListener> getListenable()
      Return the cache listenable
      org.apache.curator.framework.listen.Listenable<org.apache.curator.framework.api.UnhandledErrorListener> getUnhandledErrorListenable()
      Allows catching unhandled errors in asynchronous operations.
      static TreeCache.Builder newBuilder​(org.apache.curator.framework.CuratorFramework client, java.lang.String path)
      Create a TreeCache builder for the given client and path to configure advanced options.
      TreeCache start()
      Start the cache.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TreeCache

        public TreeCache​(org.apache.curator.framework.CuratorFramework client,
                         java.lang.String path)
        Create a TreeCache for the given client and path with default options.

        If the client is namespaced, all operations on the resulting TreeCache will be in terms of the namespace, including all published events. The given path is the root at which the TreeCache will watch and explore. If no node exists at the given path, the TreeCache will be initially empty.

        Parameters:
        client - the client to use; may be namespaced
        path - the path to the root node to watch/explore; this path need not actually exist on the server
        See Also:
        newBuilder(CuratorFramework, String)
    • Method Detail

      • newBuilder

        public static TreeCache.Builder newBuilder​(org.apache.curator.framework.CuratorFramework client,
                                                   java.lang.String path)
        Create a TreeCache builder for the given client and path to configure advanced options.

        If the client is namespaced, all operations on the resulting TreeCache will be in terms of the namespace, including all published events. The given path is the root at which the TreeCache will watch and explore. If no node exists at the given path, the TreeCache will be initially empty.

        Parameters:
        client - the client to use; may be namespaced
        path - the path to the root node to watch/explore; this path need not actually exist on the server
        Returns:
        a new builder
      • start

        public TreeCache start()
                        throws java.lang.Exception
        Start the cache. The cache is not started automatically. You must call this method.
        Returns:
        this
        Throws:
        java.lang.Exception - errors
      • close

        public void close()
        Close/end the cache.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • getListenable

        public org.apache.curator.framework.listen.Listenable<TreeCacheListener> getListenable()
        Return the cache listenable
        Returns:
        listenable
      • getUnhandledErrorListenable

        public org.apache.curator.framework.listen.Listenable<org.apache.curator.framework.api.UnhandledErrorListener> getUnhandledErrorListenable()
        Allows catching unhandled errors in asynchronous operations. TODO: consider making public.
      • getCurrentChildren

        public java.util.Map<java.lang.String,​ChildData> getCurrentChildren​(java.lang.String fullPath)
        Return the current set of children at the given path, mapped by child name. There are no guarantees of accuracy; this is merely the most recent view of the data. If there is no node at this path, null is returned.
        Parameters:
        fullPath - full path to the node to check
        Returns:
        a possibly-empty list of children if the node is alive, or null
      • getCurrentData

        public ChildData getCurrentData​(java.lang.String fullPath)
        Return the current data for the given path. There are no guarantees of accuracy. This is merely the most recent view of the data. If there is no node at the given path, null is returned.
        Parameters:
        fullPath - full path to the node to check
        Returns:
        data if the node is alive, or null