Class ZooKeeperGroup<T extends NodeState>

  • All Implemented Interfaces:
    Closeable, AutoCloseable, Group<T>
    Direct Known Subclasses:
    ZooKeeperMultiGroup

    public class ZooKeeperGroup<T extends NodeState>
    extends Object
    implements Group<T>

    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.

    • Constructor Detail

      • ZooKeeperGroup

        public ZooKeeperGroup​(org.apache.curator.framework.CuratorFramework client,
                              String path,
                              Class<T> clazz)
        Parameters:
        client - the client
        path - path to watch
      • ZooKeeperGroup

        public ZooKeeperGroup​(org.apache.curator.framework.CuratorFramework client,
                              String path,
                              Class<T> clazz,
                              ThreadFactory threadFactory)
        Parameters:
        client - the client
        path - path to watch
        threadFactory - factory to use when creating internal threads
      • ZooKeeperGroup

        public ZooKeeperGroup​(org.apache.curator.framework.CuratorFramework client,
                              String path,
                              Class<T> clazz,
                              ExecutorService executorService)
        Parameters:
        client - the client
        path - path to watch
        executorService - ExecutorService to use for the ZooKeeperGroup's background thread
    • Method Detail

      • start

        public void start()
        Start the cache. The cache is not started automatically. You must call this method.
        Specified by:
        start in interface Group<T extends NodeState>
      • isConnected

        public boolean isConnected()
        Description copied from interface: Group
        Are we connected with the cluster?
        Specified by:
        isConnected in interface Group<T extends NodeState>
      • add

        public void add​(GroupListener<T> listener)
        Description copied from interface: Group
        Registers a listener which will be called when the cluster membership changes or the group is connected or disconnected.
        Specified by:
        add in interface Group<T extends NodeState>
      • remove

        public void remove​(GroupListener<T> listener)
        Description copied from interface: Group
        Removes a previously added listener.
        Specified by:
        remove in interface Group<T extends NodeState>
      • update

        public void update​(T state)
        Description copied from interface: Group
        Update the state of this group member. If the state is null, the member will leave the group. This method can be called even if the group is not started, in which case the state will be stored and updated when the group becomes started.
        Specified by:
        update in interface Group<T extends NodeState>
        Parameters:
        state - the new state of this group member
      • members

        public Map<String,​T> members()
        Description copied from interface: Group
        Get the list of members connected to this group.
        Specified by:
        members in interface Group<T extends NodeState>
      • isMaster

        public boolean isMaster()
        Description copied from interface: Group
        Check if we are the master.
        Specified by:
        isMaster in interface Group<T extends NodeState>
      • master

        public T master()
        Description copied from interface: Group
        Retrieve the master node.
        Specified by:
        master in interface Group<T extends NodeState>
      • slaves

        public List<T> slaves()
        Description copied from interface: Group
        Retrieve the list of slaves.
        Specified by:
        slaves in interface Group<T extends NodeState>
      • getActiveChildren

        protected List<ChildData<T>> getActiveChildren()
        Filter stale nodes and return only active children from the current data.
        Returns:
        list of active children and data
      • getLastState

        public T getLastState()
        Description copied from interface: Group
        Gets the last state.

        This can be used by clients to access that last state, such as when the clients is being added as a listener but wants to retrieve the last state to be up to date when the client is added.

        Specified by:
        getLastState in interface Group<T extends NodeState>
        Returns:
        the state, or null if no last state yet.
      • getListenable

        public org.apache.curator.framework.listen.ListenerContainer<GroupListener<T>> getListenable()
        Return the cache listenable
        Returns:
        listenable
      • getCurrentData

        public List<ChildData> getCurrentData()
        Return the current data. There are no guarantees of accuracy. This is merely the most recent view of the data. The data is returned in sorted order.
        Returns:
        list of children and data
      • getCurrentData

        public ChildData getCurrentData​(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 child with that path, null is returned.
        Parameters:
        fullPath - full path to the node to check
        Returns:
        data or null
      • clearAndRefresh

        public void clearAndRefresh()
                             throws Exception
        Clear out current data and begin a new query on the path
        Throws:
        Exception - errors
      • clearAndRefresh

        public void clearAndRefresh​(boolean force,
                                    boolean sync)
                             throws Exception
        Clear out current data and begin a new query on the path
        Parameters:
        force - - whether to force clear and refresh to trigger updates
        sync - - whether to run this synchronously (block current thread) or asynchronously
        Throws:
        Exception - errors
      • clear

        public void clear()
        Clears the current data without beginning a new query and without generating any events for listeners.
      • handleException

        protected void handleException​(Throwable e)
        Default behavior is just to log the exception
        Parameters:
        e - the exception
      • remove

        protected void remove​(String fullPath)
      • members

        public static <T> Map<String,​T> members​(com.fasterxml.jackson.databind.ObjectMapper mapper,
                                                      org.apache.curator.framework.CuratorFramework curator,
                                                      String path,
                                                      Class<T> clazz)
                                               throws Exception
        Throws:
        Exception
      • getId

        public String getId()
      • isUnstable

        public boolean isUnstable()
        Returns an indication that the sequential, ephemeral node may be registered more than once for this group