Class PersistentTtlNode

java.lang.Object
org.apache.curator.framework.recipes.nodes.PersistentTtlNode
All Implemented Interfaces:
Closeable, AutoCloseable

public class PersistentTtlNode extends Object implements Closeable

Manages a PersistentNode that uses CreateMode.CONTAINER. Asynchronously it creates or updates a child on the persistent node that is marked with a provided TTL.

The effect of this is to have a node that can be watched, etc. The child node serves as a method of having the parent node deleted if the TTL expires. i.e. if the process that is running the PersistentTtlNode crashes and the TTL elapses, first the child node will be deleted due to the TTL expiration and then the parent node will be deleted as it's a container node with no children.

PersistentTtlNode is useful when you need to create a TTL node but don't want to keep it alive manually by periodically setting data - PersistentTtlNode does that for you. Further the keep-alive is done in a way that does not generate watch triggers on the parent node.

  • Field Details

    • DEFAULT_CHILD_NODE_NAME

      public static final String DEFAULT_CHILD_NODE_NAME
      See Also:
    • DEFAULT_TOUCH_SCHEDULE_FACTOR

      public static final int DEFAULT_TOUCH_SCHEDULE_FACTOR
      See Also:
    • DEFAULT_USE_PARENT_CREATION

      public static final boolean DEFAULT_USE_PARENT_CREATION
      See Also:
  • Constructor Details

    • PersistentTtlNode

      public PersistentTtlNode(CuratorFramework client, String path, long ttlMs, byte[] initData)
      Parameters:
      client - the client
      path - path for the parent ZNode
      ttlMs - max ttl for the node in milliseconds
      initData - data for the node
    • PersistentTtlNode

      public PersistentTtlNode(CuratorFramework client, String path, long ttlMs, byte[] initData, boolean useParentCreation)
      Parameters:
      client - the client
      path - path for the parent ZNode
      ttlMs - max ttl for the node in milliseconds
      initData - data for the node
      useParentCreation - if true, parent ZNode can be created without ancestors
    • PersistentTtlNode

      public PersistentTtlNode(CuratorFramework client, ScheduledExecutorService executorService, String path, long ttlMs, byte[] initData, String childNodeName, int touchScheduleFactor)
      Parameters:
      client - the client
      executorService - ExecutorService to use for background thread. This service should be single threaded, otherwise you may see inconsistent results.
      path - path for the parent ZNode
      ttlMs - max ttl for the node in milliseconds
      initData - data for the node
      childNodeName - name to use for the child node of the node created at path
      touchScheduleFactor - how ofter to set/create the child node as a factor of the ttlMs. i.e. the child is touched every (ttlMs / touchScheduleFactor)
    • PersistentTtlNode

      public PersistentTtlNode(CuratorFramework client, ScheduledExecutorService executorService, String path, long ttlMs, byte[] initData, String childNodeName, int touchScheduleFactor, boolean useParentCreation)
      Parameters:
      client - the client
      executorService - ExecutorService to use for background thread. This service should be single threaded, otherwise you may see inconsistent results.
      path - path for the parent ZNode
      ttlMs - max ttl for the node in milliseconds
      initData - data for the node
      childNodeName - name to use for the child node of the node created at path
      touchScheduleFactor - how ofter to set/create the child node as a factor of the ttlMs. i.e. the child is touched every (ttlMs / touchScheduleFactor)
      useParentCreation - if true, parent ZNode can be created without ancestors
  • Method Details

    • start

      public void start()
      You must call start() to initiate the persistent ttl node
    • 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
    • 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
    • close

      public void close()
      Call when you are done with the PersistentTtlNode. Note: the ZNode is not immediately deleted. However, if no other PersistentTtlNode with the same path is running the node will get deleted based on the ttl.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable