Class PersistentNode

java.lang.Object
org.apache.curator.framework.recipes.nodes.PersistentNode
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
PersistentEphemeralNode

public class PersistentNode extends Object implements Closeable

A persistent node is a node that attempts to stay present in ZooKeeper, even through connection and session interruptions.

Thanks to bbeck (https://github.com/bbeck) for the initial coding and design

  • Constructor Details

    • PersistentNode

      public PersistentNode(CuratorFramework givenClient, org.apache.zookeeper.CreateMode mode, boolean useProtection, String basePath, byte[] initData)
      Parameters:
      givenClient - client instance
      mode - creation mode
      useProtection - if true, call CreateBuilderMain.withProtection()
      basePath - the base path for the node
      initData - data for the node
    • PersistentNode

      public PersistentNode(CuratorFramework givenClient, org.apache.zookeeper.CreateMode mode, boolean useProtection, String basePath, byte[] initData, boolean useParentCreation)
      Parameters:
      givenClient - client instance
      mode - creation mode
      useProtection - if true, call CreateBuilderMain.withProtection()
      basePath - the base path for the node
      initData - data for the node
      useParentCreation - if true, call CreateBuilderMain.creatingParentContainersIfNeeded()
    • PersistentNode

      public PersistentNode(CuratorFramework givenClient, org.apache.zookeeper.CreateMode mode, boolean useProtection, String basePath, byte[] initData, long ttl, boolean useParentCreation)
      Parameters:
      givenClient - client instance
      mode - creation mode
      useProtection - if true, call CreateBuilderMain.withProtection()
      basePath - the base path for the node
      initData - data for the node
      ttl - for ttl modes, the ttl to use
      useParentCreation - if true, call CreateBuilderMain.creatingParentContainersIfNeeded()
  • Method Details

    • start

      public void start()
      You must call start() to initiate the persistent node. An attempt to create the node in the background will be started
    • waitForInitialCreate

      public boolean waitForInitialCreate(long timeout, TimeUnit unit) throws InterruptedException
      Block until the either initial node creation initiated by start() succeeds or the timeout elapses.
      Parameters:
      timeout - the maximum time to wait
      unit - time unit
      Returns:
      if the node was created before timeout
      Throws:
      InterruptedException - if the thread is interrupted
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • getListenable

      public Listenable<PersistentNodeListener> getListenable()
      Returns the listenable
      Returns:
      listenable
    • getActualPath

      public String getActualPath()
      Returns the currently set path or null if the node does not exist
      Returns:
      node path or null
    • setData

      public void setData(byte[] data) throws Exception
      Set data that node should set in ZK also writes the data to the node. NOTE: it is an error to call this method after start() but before the initial create has completed. Use waitForInitialCreate(long, TimeUnit) to ensure initial creation.
      Parameters:
      data - new data value
      Throws:
      Exception - errors
    • getData

      public byte[] getData()
      Return the current value of our data
      Returns:
      our data
    • deleteNode

      protected void deleteNode() throws Exception
      Throws:
      Exception