public class ZkClient extends Object implements org.apache.zookeeper.Watcher, IZkClient
The client is thread-safety
IZkClient.DataUpdater| 限定符和类型 | 字段和说明 |
|---|---|
protected ZkConnection |
_connection |
DEFAULT_CONNECTION_TIMEOUT, DEFAULT_SESSION_TIMEOUT| 构造器和说明 |
|---|
ZkClient(String connectString)
Create a client with default connection timeout and default session timeout
|
ZkClient(String connectString,
int connectionTimeout)
Create a client
|
ZkClient(String connectString,
int sessionTimeout,
int connectionTimeout)
Create a client
|
ZkClient(ZkConnection zkConnection,
int connectionTimeout)
Create a client with special implementation
|
| 限定符和类型 | 方法和说明 |
|---|---|
void |
cas(String path,
IZkClient.DataUpdater updater)
Updates data of an existing znode.
|
void |
close()
Close the client.
|
void |
connect(long maxMsToWaitUntilConnected,
org.apache.zookeeper.Watcher watcher)
Connect to ZooKeeper.
|
int |
countChildren(String path)
Counts number of children for the given path.
|
String |
create(String path,
byte[] data,
org.apache.zookeeper.CreateMode mode)
Create a node.
|
void |
createEphemeral(String path)
Create an ephemeral node with empty data
|
void |
createEphemeral(String path,
byte[] data)
Create an ephemeral node.
|
String |
createEphemeralSequential(String path,
byte[] data)
Create an ephemeral, sequential node.
|
void |
createPersistent(String path)
Create a persistent node with empty data (null)
|
void |
createPersistent(String path,
boolean createParents)
Create a persistent node with empty data (null)
If the createParents is true, neither
ZkNodeExistsException nor ZkNoNodeException were throwed. |
void |
createPersistent(String path,
byte[] data)
Create a persistent node.
|
String |
createPersistentSequential(String path,
byte[] data)
Create a persistent, sequental node.
|
boolean |
delete(String path)
delete a node
|
boolean |
deleteRecursive(String path)
delete a node with all children
|
boolean |
exists(String path)
check the node exists
|
protected boolean |
exists(String path,
boolean watch) |
List<String> |
getChildren(String path)
get the children for the node
|
protected List<String> |
getChildren(String path,
boolean watch) |
long |
getCreationTime(String path)
get the node creation time (unix milliseconds)
|
ZkLock |
getEventLock()
Returns a mutex all zookeeper events are synchronized aginst.
|
org.apache.zookeeper.ZooKeeper |
getZooKeeper()
get the inner zookeeper client
|
boolean |
isConnected()
check the connecting state of zookeeper client
|
List<?> |
multi(Iterable<?> ops)
multi operation for zookeeper 3.4.x
|
int |
numberOfListeners()
all watcher number in this connection
|
void |
process(org.apache.zookeeper.WatchedEvent event) |
byte[] |
readData(String path)
read the data for the node
|
byte[] |
readData(String path,
boolean returnNullIfPathNotExists)
read the data for the node
|
byte[] |
readData(String path,
org.apache.zookeeper.data.Stat stat)
read the data and stat for the node
|
protected byte[] |
readData(String path,
org.apache.zookeeper.data.Stat stat,
boolean watch) |
<E> E |
retryUntilConnected(Callable<E> callable) |
void |
setCurrentState(org.apache.zookeeper.Watcher.Event.KeeperState currentState) |
List<String> |
subscribeChildChanges(String path,
IZkChildListener listener)
subscribe the changing for children
|
void |
subscribeDataChanges(String path,
IZkDataListener listener)
subscribe the data changing for the node
|
void |
subscribeStateChanges(IZkStateListener listener)
subscribe the connection state
|
void |
unsubscribeAll()
unsubscribe all listeners for all path and connection state
|
void |
unsubscribeChildChanges(String path,
IZkChildListener childListener)
unsubscribe the child listener
|
void |
unsubscribeDataChanges(String path,
IZkDataListener dataListener)
unsubscribe the data changing for the node
|
void |
unsubscribeStateChanges(IZkStateListener stateListener)
unsubscribe the connection state
|
boolean |
waitForKeeperState(org.apache.zookeeper.Watcher.Event.KeeperState keeperState,
long time,
TimeUnit timeUnit)
wait some time for the state
|
boolean |
waitUntilConnected()
wait for the connected state.
|
boolean |
waitUntilConnected(long time,
TimeUnit timeUnit)
wait for the connected state
|
boolean |
waitUntilExists(String path,
TimeUnit timeUnit,
long time)
wait some unit until the node exists
|
List<String> |
watchForChilds(String path) |
void |
watchForData(String path) |
org.apache.zookeeper.data.Stat |
writeData(String path,
byte[] object)
write the data for the node
|
org.apache.zookeeper.data.Stat |
writeData(String path,
byte[] data,
int expectedVersion)
write the data for the node
|
protected ZkConnection _connection
public ZkClient(String connectString)
connectString - zookeeper connection string
comma separated host:port pairs, each corresponding to a zk
server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If
the optional chroot suffix is used the example would look
like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a"
where the client would be rooted at "/app/a" and all paths
would be relative to this root - ie getting/setting/etc...
"/foo/bar" would result in operations being run on
"/app/a/foo/bar" (from the server perspective).IZkClient.DEFAULT_CONNECTION_TIMEOUT,
IZkClient.DEFAULT_SESSION_TIMEOUTpublic ZkClient(String connectString, int connectionTimeout)
connectString - zookeeper connection string
comma separated host:port pairs, each corresponding to a zk
server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If
the optional chroot suffix is used the example would look
like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a"
where the client would be rooted at "/app/a" and all paths
would be relative to this root - ie getting/setting/etc...
"/foo/bar" would result in operations being run on
"/app/a/foo/bar" (from the server perspective).connectionTimeout - connection timeout in millisecondspublic ZkClient(String connectString, int sessionTimeout, int connectionTimeout)
connectString - zookeeper connection string
comma separated host:port pairs, each corresponding to a zk
server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If
the optional chroot suffix is used the example would look
like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a"
where the client would be rooted at "/app/a" and all paths
would be relative to this root - ie getting/setting/etc...
"/foo/bar" would result in operations being run on
"/app/a/foo/bar" (from the server perspective).sessionTimeout - session timeout in millisecondsconnectionTimeout - connection timeout in millisecondspublic ZkClient(ZkConnection zkConnection, int connectionTimeout)
zkConnection - special clientconnectionTimeout - connection timeout in millisecondspublic List<String> subscribeChildChanges(String path, IZkChildListener listener)
IZkClientsubscribeChildChanges 在接口中 IZkClientpath - the path for the nodelistener - the listenerIZkChildListenerpublic void unsubscribeChildChanges(String path, IZkChildListener childListener)
IZkClientunsubscribeChildChanges 在接口中 IZkClientpath - the path for the nodechildListener - the listenerpublic void subscribeDataChanges(String path, IZkDataListener listener)
IZkClientsubscribeDataChanges 在接口中 IZkClientpath - the path for the nodelistener - the data changing listenerIZkDataListenerpublic void unsubscribeDataChanges(String path, IZkDataListener dataListener)
IZkClientunsubscribeDataChanges 在接口中 IZkClientpath - the path for the nodedataListener - the data changing listenerpublic void subscribeStateChanges(IZkStateListener listener)
IZkClientsubscribeStateChanges 在接口中 IZkClientlistener - the connection listenerIZkStateListenerpublic void unsubscribeStateChanges(IZkStateListener stateListener)
IZkClientunsubscribeStateChanges 在接口中 IZkClientstateListener - the connection listenerpublic void unsubscribeAll()
IZkClientunsubscribeAll 在接口中 IZkClientpublic void createPersistent(String path)
IZkClientcreatePersistent 在接口中 IZkClientpath - the path for the nodeIZkClient.createPersistent(String, boolean)public void createPersistent(String path, boolean createParents)
IZkClient
If the createParents is true, neither ZkNodeExistsException nor ZkNoNodeException were throwed.
createPersistent 在接口中 IZkClientpath - the path for the nodecreateParents - if true all parent dirs are created as well and no
ZkNodeExistsException is thrown in case the path already existspublic void createPersistent(String path, byte[] data)
IZkClientcreatePersistent 在接口中 IZkClientpath - the path for the nodedata - node datapublic String createPersistentSequential(String path, byte[] data)
IZkClientcreatePersistentSequential 在接口中 IZkClientpath - the path for the nodedata - node datapublic void createEphemeral(String path)
IZkClientcreateEphemeral 在接口中 IZkClientpath - zk pathpublic String create(String path, byte[] data, org.apache.zookeeper.CreateMode mode)
IZkClientpublic void createEphemeral(String path, byte[] data)
IZkClientcreateEphemeral 在接口中 IZkClientpath - the path for the nodedata - node datapublic String createEphemeralSequential(String path, byte[] data)
IZkClientcreateEphemeralSequential 在接口中 IZkClientpath - the path for the nodedata - the data for the nodepublic void process(org.apache.zookeeper.WatchedEvent event)
process 在接口中 org.apache.zookeeper.Watcherpublic List<String> getChildren(String path)
IZkClientgetChildren 在接口中 IZkClientpath - the path for the nodepublic int countChildren(String path)
IZkClientcountChildren 在接口中 IZkClientpath - zk pathprotected boolean exists(String path, boolean watch)
public boolean deleteRecursive(String path)
IZkClientdeleteRecursive 在接口中 IZkClientpath - the path for the nodepublic boolean waitUntilExists(String path, TimeUnit timeUnit, long time) throws ZkInterruptedException
IZkClientwaitUntilExists 在接口中 IZkClientpath - the path for the nodetimeUnit - the time unittime - some timeZkInterruptedExceptionpublic boolean waitUntilConnected()
throws ZkInterruptedException
IZkClient
waitForKeeperState(KeeperState.SyncConnected, Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
waitUntilConnected 在接口中 IZkClientZkInterruptedException - the thread was interruptedIZkClient.waitForKeeperState(org.apache.zookeeper.Watcher.Event.KeeperState, long, java.util.concurrent.TimeUnit)public boolean waitUntilConnected(long time,
TimeUnit timeUnit)
throws ZkInterruptedException
IZkClientwaitUntilConnected 在接口中 IZkClienttime - soem timetimeUnit - the time unitZkInterruptedExceptionpublic boolean waitForKeeperState(org.apache.zookeeper.Watcher.Event.KeeperState keeperState,
long time,
TimeUnit timeUnit)
throws ZkInterruptedException
IZkClientwaitForKeeperState 在接口中 IZkClientkeeperState - the statetime - some timetimeUnit - the time unitkeeperState before the end timeZkInterruptedExceptionpublic <E> E retryUntilConnected(Callable<E> callable)
E - the runtime type of resultcallable - the callable objectZkInterruptedException - if operation was interrupted, or a required reconnection
got interruptedIllegalArgumentException - if called from anything except the ZooKeeper event
threadZkException - if any ZooKeeper exception occurredRuntimeException - if any other exception occurs from invoking the Callablepublic void setCurrentState(org.apache.zookeeper.Watcher.Event.KeeperState currentState)
public ZkLock getEventLock()
public byte[] readData(String path)
IZkClientreadData 在接口中 IZkClientpath - the path for the nodeIZkClient.readData(String, boolean)public byte[] readData(String path, boolean returnNullIfPathNotExists)
IZkClientreadData 在接口中 IZkClientpath - the path for the nodereturnNullIfPathNotExists - if true no ZkNoNodeException thrownpublic byte[] readData(String path, org.apache.zookeeper.data.Stat stat)
IZkClientreadData 在接口中 IZkClientpath - the path for the nodestat - the stat for the nodeIZkClient.readData(String, boolean)protected byte[] readData(String path, org.apache.zookeeper.data.Stat stat, boolean watch)
public org.apache.zookeeper.data.Stat writeData(String path, byte[] object)
IZkClientpublic void cas(String path, IZkClient.DataUpdater updater)
IZkClientIZkClient.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.public org.apache.zookeeper.data.Stat writeData(String path, byte[] data, int expectedVersion)
IZkClientwriteData 在接口中 IZkClientpath - the path for the nodedata - the data for the nodeexpectedVersion - the version for the nodeIZkClient.cas(String, com.github.zkclient.IZkClient.DataUpdater)public void watchForData(String path)
public void connect(long maxMsToWaitUntilConnected,
org.apache.zookeeper.Watcher watcher)
IZkClientpublic long getCreationTime(String path)
IZkClientgetCreationTime 在接口中 IZkClientpath - the path for the nodepublic void close()
throws ZkInterruptedException
IZkClientclose 在接口中 IZkClientclose 在接口中 Closeableclose 在接口中 AutoCloseableZkInterruptedException - if interrupted while closingpublic int numberOfListeners()
IZkClientnumberOfListeners 在接口中 IZkClientpublic org.apache.zookeeper.ZooKeeper getZooKeeper()
IZkClientgetZooKeeper 在接口中 IZkClientpublic boolean isConnected()
IZkClientisConnected 在接口中 IZkClientCopyright © 2019. All rights reserved.