Class PersistentNode

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

public class PersistentNode
extends java.lang.Object
implements java.io.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 Summary

    Constructors 
    Constructor Description
    PersistentNode​(CuratorFramework givenClient, org.apache.zookeeper.CreateMode mode, boolean useProtection, java.lang.String basePath, byte[] initData)  
    PersistentNode​(CuratorFramework givenClient, org.apache.zookeeper.CreateMode mode, boolean useProtection, java.lang.String basePath, byte[] initData, long ttl)  
  • Method Summary

    Modifier and Type Method Description
    void close()  
    protected void deleteNode()  
    java.lang.String getActualPath()
    Returns the currently set path or null if the node does not exist
    byte[] getData()
    Return the current value of our data
    Listenable<PersistentNodeListener> getListenable()
    Returns the listenable
    void setData​(byte[] data)
    Set data that node should set in ZK also writes the data to the node.
    void start()
    You must call start() to initiate the persistent node.
    boolean waitForInitialCreate​(long timeout, java.util.concurrent.TimeUnit unit)
    Block until the either initial node creation initiated by start() succeeds or the timeout elapses.

    Methods inherited from class java.lang.Object

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

    • PersistentNode

      public PersistentNode​(CuratorFramework givenClient, org.apache.zookeeper.CreateMode mode, boolean useProtection, java.lang.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, java.lang.String basePath, byte[] initData, long ttl)
      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
  • 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, java.util.concurrent.TimeUnit unit) throws java.lang.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:
      java.lang.InterruptedException - if the thread is interrupted
    • close

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

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

      public java.lang.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 java.lang.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:
      java.lang.Exception - errors
    • getData

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

      protected void deleteNode() throws java.lang.Exception
      Throws:
      java.lang.Exception