public class NodeReader extends Object
start()
method is called, this implementation waits
for the target znode to be created and then reads its content.
// Prepare a ZooKeeper instance. ZooKeeper zooKeeper = ... // The path of the target znode to read. String path = ... // Prepare a Listener implementation. NodeReader.NodeReader.Listener
listener = new NodeReader.Listener() { @Override public voidonRead
(NodeReader reader, byte[] data, Stat stat) { System.out.println("Successfully read the znode."); } @Override public voidonGaveUp
(NodeReader reader) { System.out.println("Gave up reading the znode."); } }; // Read the target znode. new NodeReader() .setZooKeeper
(zooKeeper) .setPath
(path) .setListener
(listener) .start()
;
Modifier and Type | Class and Description |
---|---|
static class |
NodeReader.Adapter
An empty implementation of
NodeReader.Listener interface. |
static interface |
NodeReader.Listener
Listener interface to receive the result of reading.
|
Constructor and Description |
---|
NodeReader() |
NodeReader(org.apache.zookeeper.ZooKeeper zooKeeper) |
Modifier and Type | Method and Description |
---|---|
NodeReader |
finish()
Mark as 'shouldFinish' not to schedule ZooKeeper callbacks
any further.
|
NodeReader.Listener |
getListener()
Get the listener.
|
String |
getPath()
Get the path of the target znode to read.
|
org.apache.zookeeper.ZooKeeper |
getZooKeeper()
Get the
ZooKeeper instance to read the target znode. |
NodeReader |
setListener(NodeReader.Listener listener)
Set a listener.
|
NodeReader |
setPath(String path)
Set the path of the target znode to read.
|
NodeReader |
setZooKeeper(org.apache.zookeeper.ZooKeeper zooKeeper)
Set the
ZooKeeper instance to read the target znode. |
NodeReader |
start()
Start reading the target znode.
|
public NodeReader()
public NodeReader(org.apache.zookeeper.ZooKeeper zooKeeper)
public org.apache.zookeeper.ZooKeeper getZooKeeper()
ZooKeeper
instance to read the target znode.ZooKeeper
instance.public NodeReader setZooKeeper(org.apache.zookeeper.ZooKeeper zooKeeper)
ZooKeeper
instance to read the target znode.
If no ZooKeeper
instance is set when start()
is called,
an IllegalStateException
is thrown.
zooKeeper
- A ZooKeeper
instance.this
object.public String getPath()
public NodeReader setPath(String path)
If no path is set when start()
is called, an IllegalStateException
is thrown.
path
- The path of the target znode.this
object.public NodeReader.Listener getListener()
public NodeReader setListener(NodeReader.Listener listener)
listener
- A listener.this
object.public NodeReader start()
this
object.IllegalStateException
- ZooKeeper
instance is set.
public NodeReader finish()
Note that calling this method does not remove an existing
Watcher
which is watching the target znode.
this
object.Copyright © 2015. All rights reserved.