public class ZkClient
extends java.lang.Object
implements org.apache.zookeeper.Watcher
Modifier and Type | Field and Description |
---|---|
protected IZkConnection |
_connection |
protected long |
_operationRetryTimeoutInMillis |
protected static java.lang.String |
JAVA_LOGIN_CONFIG_PARAM |
protected static java.lang.String |
ZK_LOGIN_CONTEXT_NAME_KEY |
protected static java.lang.String |
ZK_SASL_CLIENT |
Constructor and Description |
---|
ZkClient(IZkConnection connection) |
ZkClient(IZkConnection connection,
int connectionTimeout) |
ZkClient(IZkConnection zkConnection,
int connectionTimeout,
ZkSerializer zkSerializer) |
ZkClient(IZkConnection zkConnection,
int connectionTimeout,
ZkSerializer zkSerializer,
long operationRetryTimeout) |
ZkClient(java.lang.String serverstring) |
ZkClient(java.lang.String zkServers,
int connectionTimeout) |
ZkClient(java.lang.String zkServers,
int sessionTimeout,
int connectionTimeout) |
ZkClient(java.lang.String zkServers,
int sessionTimeout,
int connectionTimeout,
ZkSerializer zkSerializer) |
ZkClient(java.lang.String zkServers,
int sessionTimeout,
int connectionTimeout,
ZkSerializer zkSerializer,
long operationRetryTimeout) |
Modifier and Type | Method and Description |
---|---|
void |
addAuthInfo(java.lang.String scheme,
byte[] auth)
Add authentication information to the connection.
|
void |
close()
Close the client.
|
void |
connect(long maxMsToWaitUntilConnected,
org.apache.zookeeper.Watcher watcher)
Connect to ZooKeeper.
|
int |
countChildren(java.lang.String path)
Counts number of children for the given path.
|
java.lang.String |
create(java.lang.String path,
java.lang.Object data,
org.apache.zookeeper.CreateMode mode)
Create a node.
|
java.lang.String |
create(java.lang.String path,
java.lang.Object data,
java.util.List<org.apache.zookeeper.data.ACL> acl,
org.apache.zookeeper.CreateMode mode)
Create a node with ACL.
|
void |
createEphemeral(java.lang.String path)
Create an ephemeral node.
|
void |
createEphemeral(java.lang.String path,
java.util.List<org.apache.zookeeper.data.ACL> acl)
Create an ephemeral node and set its ACL.
|
void |
createEphemeral(java.lang.String path,
java.lang.Object data)
Create an ephemeral node.
|
void |
createEphemeral(java.lang.String path,
java.lang.Object data,
java.util.List<org.apache.zookeeper.data.ACL> acl)
Create an ephemeral node.
|
java.lang.String |
createEphemeralSequential(java.lang.String path,
java.lang.Object data)
Create an ephemeral, sequential node.
|
java.lang.String |
createEphemeralSequential(java.lang.String path,
java.lang.Object data,
java.util.List<org.apache.zookeeper.data.ACL> acl)
Create an ephemeral, sequential node with ACL.
|
void |
createPersistent(java.lang.String path)
Create a persistent node.
|
void |
createPersistent(java.lang.String path,
boolean createParents)
Create a persistent node and set its ACLs.
|
void |
createPersistent(java.lang.String path,
boolean createParents,
java.util.List<org.apache.zookeeper.data.ACL> acl)
Create a persistent node and set its ACLs.
|
void |
createPersistent(java.lang.String path,
java.lang.Object data)
Create a persistent node.
|
void |
createPersistent(java.lang.String path,
java.lang.Object data,
java.util.List<org.apache.zookeeper.data.ACL> acl)
Create a persistent node.
|
java.lang.String |
createPersistentSequential(java.lang.String path,
java.lang.Object data)
Create a persistent, sequental node.
|
java.lang.String |
createPersistentSequential(java.lang.String path,
java.lang.Object data,
java.util.List<org.apache.zookeeper.data.ACL> acl)
Create a persistent, sequential node and set its ACL.
|
boolean |
delete(java.lang.String path) |
boolean |
delete(java.lang.String path,
int version) |
boolean |
deleteRecursive(java.lang.String path) |
boolean |
exists(java.lang.String path) |
protected boolean |
exists(java.lang.String path,
boolean watch) |
java.util.Map.Entry<java.util.List<org.apache.zookeeper.data.ACL>,org.apache.zookeeper.data.Stat> |
getAcl(java.lang.String path)
Gets the acl on path
|
java.util.List<java.lang.String> |
getChildren(java.lang.String path) |
protected java.util.List<java.lang.String> |
getChildren(java.lang.String path,
boolean watch) |
long |
getCreationTime(java.lang.String path) |
protected java.util.Set<IZkDataListener> |
getDataListener(java.lang.String path) |
ZkLock |
getEventLock()
Returns a mutex all zookeeper events are synchronized aginst.
|
boolean |
getShutdownTrigger() |
java.util.List<org.apache.zookeeper.OpResult> |
multi(java.lang.Iterable<org.apache.zookeeper.Op> ops) |
int |
numberOfListeners() |
void |
process(org.apache.zookeeper.WatchedEvent event) |
<T> T |
readData(java.lang.String path) |
<T> T |
readData(java.lang.String path,
boolean returnNullIfPathNotExists) |
<T> T |
readData(java.lang.String path,
org.apache.zookeeper.data.Stat stat) |
protected <T> T |
readData(java.lang.String path,
org.apache.zookeeper.data.Stat stat,
boolean watch) |
<T> T |
retryUntilConnected(java.util.concurrent.Callable<T> callable) |
void |
setAcl(java.lang.String path,
java.util.List<org.apache.zookeeper.data.ACL> acl)
Sets the acl on path
|
void |
setCurrentState(org.apache.zookeeper.Watcher.Event.KeeperState currentState) |
void |
setShutdownTrigger(boolean triggerState) |
void |
setZkSerializer(ZkSerializer zkSerializer) |
void |
showFolders(java.io.OutputStream output) |
java.util.List<java.lang.String> |
subscribeChildChanges(java.lang.String path,
IZkChildListener listener) |
void |
subscribeDataChanges(java.lang.String path,
IZkDataListener listener) |
void |
subscribeStateChanges(IZkStateListener listener) |
void |
unsubscribeAll() |
void |
unsubscribeChildChanges(java.lang.String path,
IZkChildListener childListener) |
void |
unsubscribeDataChanges(java.lang.String path,
IZkDataListener dataListener) |
void |
unsubscribeStateChanges(IZkStateListener stateListener) |
<T> void |
updateDataSerialized(java.lang.String path,
DataUpdater<T> updater)
Updates data of an existing znode.
|
boolean |
waitForKeeperState(org.apache.zookeeper.Watcher.Event.KeeperState keeperState,
long time,
java.util.concurrent.TimeUnit timeUnit) |
void |
waitUntilConnected() |
boolean |
waitUntilConnected(long time,
java.util.concurrent.TimeUnit timeUnit) |
boolean |
waitUntilExists(java.lang.String path,
java.util.concurrent.TimeUnit timeUnit,
long time) |
java.util.List<java.lang.String> |
watchForChilds(java.lang.String path)
Installs a child watch for the given path.
|
void |
watchForData(java.lang.String path) |
void |
writeData(java.lang.String path,
java.lang.Object object) |
void |
writeData(java.lang.String path,
java.lang.Object datat,
int expectedVersion) |
org.apache.zookeeper.data.Stat |
writeDataReturnStat(java.lang.String path,
java.lang.Object datat,
int expectedVersion) |
protected static final java.lang.String JAVA_LOGIN_CONFIG_PARAM
protected static final java.lang.String ZK_SASL_CLIENT
protected static final java.lang.String ZK_LOGIN_CONTEXT_NAME_KEY
protected final IZkConnection _connection
protected final long _operationRetryTimeoutInMillis
public ZkClient(java.lang.String serverstring)
public ZkClient(java.lang.String zkServers, int connectionTimeout)
public ZkClient(java.lang.String zkServers, int sessionTimeout, int connectionTimeout)
public ZkClient(java.lang.String zkServers, int sessionTimeout, int connectionTimeout, ZkSerializer zkSerializer)
public ZkClient(java.lang.String zkServers, int sessionTimeout, int connectionTimeout, ZkSerializer zkSerializer, long operationRetryTimeout)
zkServers
- The Zookeeper serverssessionTimeout
- The session timeout in milli secondsconnectionTimeout
- The connection timeout in milli secondszkSerializer
- The Zookeeper data serializeroperationRetryTimeout
- Most operations done through this ZkClient
are retried in cases like
connection loss with the Zookeeper servers. During such failures, this
operationRetryTimeout
decides the maximum amount of time, in milli seconds, each
operation is retried. A value lesser than 0 is considered as
"retry forever until a connection has been reestablished".public ZkClient(IZkConnection connection)
public ZkClient(IZkConnection connection, int connectionTimeout)
public ZkClient(IZkConnection zkConnection, int connectionTimeout, ZkSerializer zkSerializer)
public ZkClient(IZkConnection zkConnection, int connectionTimeout, ZkSerializer zkSerializer, long operationRetryTimeout)
zkConnection
- The Zookeeper serversconnectionTimeout
- The connection timeout in milli secondszkSerializer
- The Zookeeper data serializeroperationRetryTimeout
- Most operations done through this ZkClient
are retried in cases like
connection loss with the Zookeeper servers. During such failures, this
operationRetryTimeout
decides the maximum amount of time, in milli seconds, each
operation is retried. A value lesser than 0 is considered as
"retry forever until a connection has been reestablished".public void setZkSerializer(ZkSerializer zkSerializer)
public java.util.List<java.lang.String> subscribeChildChanges(java.lang.String path, IZkChildListener listener)
public void unsubscribeChildChanges(java.lang.String path, IZkChildListener childListener)
public void subscribeDataChanges(java.lang.String path, IZkDataListener listener)
public void unsubscribeDataChanges(java.lang.String path, IZkDataListener dataListener)
public void subscribeStateChanges(IZkStateListener listener)
public void unsubscribeStateChanges(IZkStateListener stateListener)
public void unsubscribeAll()
public void createPersistent(java.lang.String path) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
path
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic void createPersistent(java.lang.String path, boolean createParents) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
path
- createParents
- if true all parent dirs are created as well and no ZkNodeExistsException
is thrown in case the
path already existsZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic void createPersistent(java.lang.String path, boolean createParents, java.util.List<org.apache.zookeeper.data.ACL> acl) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
path
- acl
- List of ACL permissions to assign to the nodecreateParents
- if true all parent dirs are created as well and no ZkNodeExistsException
is thrown in case the
path already existsZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic void setAcl(java.lang.String path, java.util.List<org.apache.zookeeper.data.ACL> acl) throws ZkException
path
- acl
- List of ACL permissions to assign to the path.ZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic java.util.Map.Entry<java.util.List<org.apache.zookeeper.data.ACL>,org.apache.zookeeper.data.Stat> getAcl(java.lang.String path) throws ZkException
path
- ZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic void createPersistent(java.lang.String path, java.lang.Object data) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
path
- data
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic void createPersistent(java.lang.String path, java.lang.Object data, java.util.List<org.apache.zookeeper.data.ACL> acl)
path
- data
- acl
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic java.lang.String createPersistentSequential(java.lang.String path, java.lang.Object data) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
path
- data
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic java.lang.String createPersistentSequential(java.lang.String path, java.lang.Object data, java.util.List<org.apache.zookeeper.data.ACL> acl) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
path
- acl
- data
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic void createEphemeral(java.lang.String path) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
path
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic void createEphemeral(java.lang.String path, java.util.List<org.apache.zookeeper.data.ACL> acl) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
path
- acl
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic java.lang.String create(java.lang.String path, java.lang.Object data, org.apache.zookeeper.CreateMode mode) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
path
- data
- mode
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic java.lang.String create(java.lang.String path, java.lang.Object data, java.util.List<org.apache.zookeeper.data.ACL> acl, org.apache.zookeeper.CreateMode mode)
path
- data
- acl
- mode
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic void createEphemeral(java.lang.String path, java.lang.Object data) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
path
- data
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic void createEphemeral(java.lang.String path, java.lang.Object data, java.util.List<org.apache.zookeeper.data.ACL> acl) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
path
- data
- acl
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic java.lang.String createEphemeralSequential(java.lang.String path, java.lang.Object data) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
path
- data
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic java.lang.String createEphemeralSequential(java.lang.String path, java.lang.Object data, java.util.List<org.apache.zookeeper.data.ACL> acl) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
path
- data
- acl
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurspublic void process(org.apache.zookeeper.WatchedEvent event)
process
in interface org.apache.zookeeper.Watcher
public java.util.List<java.lang.String> getChildren(java.lang.String path)
protected java.util.List<java.lang.String> getChildren(java.lang.String path, boolean watch)
public int countChildren(java.lang.String path)
path
- protected boolean exists(java.lang.String path, boolean watch)
public boolean exists(java.lang.String path)
public boolean deleteRecursive(java.lang.String path)
public boolean waitUntilExists(java.lang.String path, java.util.concurrent.TimeUnit timeUnit, long time) throws ZkInterruptedException
ZkInterruptedException
protected java.util.Set<IZkDataListener> getDataListener(java.lang.String path)
public void showFolders(java.io.OutputStream output)
public void waitUntilConnected() throws ZkInterruptedException
ZkInterruptedException
public boolean waitUntilConnected(long time, java.util.concurrent.TimeUnit timeUnit) throws ZkInterruptedException
ZkInterruptedException
public boolean waitForKeeperState(org.apache.zookeeper.Watcher.Event.KeeperState keeperState, long time, java.util.concurrent.TimeUnit timeUnit) throws ZkInterruptedException
ZkInterruptedException
public <T> T retryUntilConnected(java.util.concurrent.Callable<T> callable) throws ZkInterruptedException, java.lang.IllegalArgumentException, ZkException, java.lang.RuntimeException
T
- callable
- ZkInterruptedException
- if operation was interrupted, or a required reconnection got interruptedjava.lang.IllegalArgumentException
- if called from anything except the ZooKeeper event threadZkException
- if any ZooKeeper exception occurredjava.lang.RuntimeException
- if any other exception occurs from invoking the Callablepublic void setCurrentState(org.apache.zookeeper.Watcher.Event.KeeperState currentState)
public ZkLock getEventLock()
public boolean delete(java.lang.String path)
public boolean delete(java.lang.String path, int version)
public <T> T readData(java.lang.String path)
public <T> T readData(java.lang.String path, boolean returnNullIfPathNotExists)
public <T> T readData(java.lang.String path, org.apache.zookeeper.data.Stat stat)
protected <T> T readData(java.lang.String path, org.apache.zookeeper.data.Stat stat, boolean watch)
public void writeData(java.lang.String path, java.lang.Object object)
public <T> void updateDataSerialized(java.lang.String path, DataUpdater<T> updater)
DataUpdater
that is
passed into this method, which returns the new content. The new content is only written back to ZooKeeper if
nobody has modified the given znode in between. If a concurrent change has been detected the new data of the
znode is passed to the updater once again until the new contents can be successfully written back to ZooKeeper.T
- path
- The path of the znode.updater
- Updater that creates the new contents.public void writeData(java.lang.String path, java.lang.Object datat, int expectedVersion)
public org.apache.zookeeper.data.Stat writeDataReturnStat(java.lang.String path, java.lang.Object datat, int expectedVersion)
public void watchForData(java.lang.String path)
public java.util.List<java.lang.String> watchForChilds(java.lang.String path)
path
- public void addAuthInfo(java.lang.String scheme, byte[] auth)
scheme
- auth
- public void connect(long maxMsToWaitUntilConnected, org.apache.zookeeper.Watcher watcher) throws ZkInterruptedException, ZkTimeoutException, java.lang.IllegalStateException
maxMsToWaitUntilConnected
- watcher
- ZkInterruptedException
- if the connection timed out due to thread interruptionZkTimeoutException
- if the connection timed outjava.lang.IllegalStateException
- if the connection timed out due to thread interruptionpublic long getCreationTime(java.lang.String path)
public void close() throws ZkInterruptedException
ZkInterruptedException
public void setShutdownTrigger(boolean triggerState)
public boolean getShutdownTrigger()
public int numberOfListeners()
public java.util.List<org.apache.zookeeper.OpResult> multi(java.lang.Iterable<org.apache.zookeeper.Op> ops) throws ZkException
ZkException