Class PersistentTtlNode
- All Implemented Interfaces:
Closeable
,AutoCloseable
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 Summary
-
Constructor Summary
ConstructorDescriptionPersistentTtlNode
(CuratorFramework client, String path, long ttlMs, byte[] initData) PersistentTtlNode
(CuratorFramework client, String path, long ttlMs, byte[] initData, boolean useParentCreation) PersistentTtlNode
(CuratorFramework client, ScheduledExecutorService executorService, String path, long ttlMs, byte[] initData, String childNodeName, int touchScheduleFactor) PersistentTtlNode
(CuratorFramework client, ScheduledExecutorService executorService, String path, long ttlMs, byte[] initData, String childNodeName, int touchScheduleFactor, boolean useParentCreation) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Call when you are done with the PersistentTtlNode.byte[]
getData()
Return the current value of our datavoid
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 nodeboolean
waitForInitialCreate
(long timeout, TimeUnit unit) Block until the either initial node creation initiated bystart()
succeeds or the timeout elapses.
-
Field Details
-
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
- Parameters:
client
- the clientpath
- path for the parent ZNodettlMs
- max ttl for the node in millisecondsinitData
- data for the node
-
PersistentTtlNode
public PersistentTtlNode(CuratorFramework client, String path, long ttlMs, byte[] initData, boolean useParentCreation) - Parameters:
client
- the clientpath
- path for the parent ZNodettlMs
- max ttl for the node in millisecondsinitData
- data for the nodeuseParentCreation
- 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 clientexecutorService
- ExecutorService to use for background thread. This service should be single threaded, otherwise you may see inconsistent results.path
- path for the parent ZNodettlMs
- max ttl for the node in millisecondsinitData
- data for the nodechildNodeName
- name to use for the child node of the node created atpath
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 clientexecutorService
- ExecutorService to use for background thread. This service should be single threaded, otherwise you may see inconsistent results.path
- path for the parent ZNodettlMs
- max ttl for the node in millisecondsinitData
- data for the nodechildNodeName
- name to use for the child node of the node created atpath
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
Block until the either initial node creation initiated bystart()
succeeds or the timeout elapses.- Parameters:
timeout
- the maximum time to waitunit
- time unit- Returns:
- if the node was created before timeout
- Throws:
InterruptedException
- if the thread is interrupted
-
setData
Set data that node should set in ZK also writes the data to the node. NOTE: it is an error to call this method afterstart()
but before the initial create has completed. UsewaitForInitialCreate(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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-