Class PersistentTtlNode

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

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

    • Constructor Summary

      Constructors 
      Constructor Description
      PersistentTtlNode​(org.apache.curator.framework.CuratorFramework client, java.lang.String path, long ttlMs, byte[] initData)  
      PersistentTtlNode​(org.apache.curator.framework.CuratorFramework client, java.util.concurrent.ScheduledExecutorService executorService, java.lang.String path, long ttlMs, byte[] initData, java.lang.String childNodeName, int touchScheduleFactor)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Call when you are done with the PersistentTtlNode.
      byte[] getData()
      Return the current value of our data
      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 ttl 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
    • Field Detail

      • DEFAULT_CHILD_NODE_NAME

        public static final java.lang.String DEFAULT_CHILD_NODE_NAME
        See Also:
        Constant Field Values
      • DEFAULT_TOUCH_SCHEDULE_FACTOR

        public static final int DEFAULT_TOUCH_SCHEDULE_FACTOR
        See Also:
        Constant Field Values
    • Constructor Detail

      • PersistentTtlNode

        public PersistentTtlNode​(org.apache.curator.framework.CuratorFramework client,
                                 java.lang.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​(org.apache.curator.framework.CuratorFramework client,
                                 java.util.concurrent.ScheduledExecutorService executorService,
                                 java.lang.String path,
                                 long ttlMs,
                                 byte[] initData,
                                 java.lang.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)
    • Method Detail

      • start

        public void start()
        You must call start() to initiate the persistent ttl node
      • 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
      • 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
      • 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 java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable