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.
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_CHILD_NODE_NAME |
static int |
DEFAULT_TOUCH_SCHEDULE_FACTOR |
static boolean |
DEFAULT_USE_PARENT_CREATION |
Constructor and Description |
---|
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) |
PersistentTtlNode(CuratorFramework client,
String path,
long ttlMs,
byte[] initData) |
PersistentTtlNode(CuratorFramework client,
String path,
long ttlMs,
byte[] initData,
boolean useParentCreation) |
Modifier and Type | Method and 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,
TimeUnit unit)
Block until the either initial node creation initiated by
start() succeeds or
the timeout elapses. |
public static final String DEFAULT_CHILD_NODE_NAME
public static final int DEFAULT_TOUCH_SCHEDULE_FACTOR
public static final boolean DEFAULT_USE_PARENT_CREATION
public PersistentTtlNode(CuratorFramework client, String path, long ttlMs, byte[] initData)
client
- the clientpath
- path for the parent ZNodettlMs
- max ttl for the node in millisecondsinitData
- data for the nodepublic PersistentTtlNode(CuratorFramework client, String path, long ttlMs, byte[] initData, boolean useParentCreation)
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 ancestorspublic PersistentTtlNode(CuratorFramework client, ScheduledExecutorService executorService, String path, long ttlMs, byte[] initData, String childNodeName, int touchScheduleFactor)
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 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)
public PersistentTtlNode(CuratorFramework client, ScheduledExecutorService executorService, String path, long ttlMs, byte[] initData, String childNodeName, int touchScheduleFactor, boolean useParentCreation)
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 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 ancestorspublic void start()
public boolean waitForInitialCreate(long timeout, TimeUnit unit) throws InterruptedException
start()
succeeds or
the timeout elapses.timeout
- the maximum time to waitunit
- time unitInterruptedException
- if the thread is interruptedpublic void setData(byte[] data) throws Exception
start()
but before the initial create
has completed. Use waitForInitialCreate(long, TimeUnit)
to ensure initial
creation.data
- new data valueException
- errorspublic byte[] getData()
public void close()
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2011–2023 The Apache Software Foundation. All rights reserved.