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.CloseableA 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(org.apache.curator.framework.CuratorFramework givenClient, org.apache.zookeeper.CreateMode mode, boolean useProtection, java.lang.String basePath, byte[] initData)PersistentNode(org.apache.curator.framework.CuratorFramework givenClient, org.apache.zookeeper.CreateMode mode, boolean useProtection, java.lang.String basePath, byte[] initData, long ttl)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()protected voiddeleteNode()java.lang.StringgetActualPath()Returns the currently set path or null if the node does not existbyte[]getData()Return the current value of our dataorg.apache.curator.framework.listen.ListenerContainer<PersistentNodeListener>getListenable()Returns the listenablevoidsetData(byte[] data)Set data that node should set in ZK also writes the data to the node.voidstart()You must call start() to initiate the persistent node.booleanwaitForInitialCreate(long timeout, java.util.concurrent.TimeUnit unit)Block until the either initial node creation initiated bystart()succeeds or the timeout elapses.
-
-
-
Constructor Detail
-
PersistentNode
public PersistentNode(org.apache.curator.framework.CuratorFramework givenClient, org.apache.zookeeper.CreateMode mode, boolean useProtection, java.lang.String basePath, byte[] initData)- Parameters:
givenClient- client instancemode- creation modeuseProtection- if true, callCreateBuilderMain.withProtection()basePath- the base path for the nodeinitData- data for the node
-
PersistentNode
public PersistentNode(org.apache.curator.framework.CuratorFramework givenClient, org.apache.zookeeper.CreateMode mode, boolean useProtection, java.lang.String basePath, byte[] initData, long ttl)- Parameters:
givenClient- client instancemode- creation modeuseProtection- if true, callCreateBuilderMain.withProtection()basePath- the base path for the nodeinitData- data for the nodettl- for ttl modes, the ttl to use
-
-
Method Detail
-
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.InterruptedExceptionBlock 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:
java.lang.InterruptedException- if the thread is interrupted
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
getListenable
public org.apache.curator.framework.listen.ListenerContainer<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.ExceptionSet 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:
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
-
-