ZkClient zkClient = new ZkClient("127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002");
byte[] readData(String path); byte[] readData(String path, boolean returnNullIfPathNotExists); byte[] readData(String path, Stat stat);
Stat writeData(String path, byte[] data); Stat writeData(String path, byte[] data, int expectedVersion);
void createPersistent(String path); void createPersistent(String path, boolean createParents); void createPersistent(String path, byte[] data); String createPersistentSequential(String path, byte[] data); void createEphemeral(final String path); void createEphemeral(final String path, final byte[] data); String createEphemeralSequential(final String path, final byte[] data); String create(final String path, byte[] data, final CreateMode mode);
boolean delete(final String path); boolean deleteRecursive(String path);
boolean exists(final String path); List<String> getChildren(String path); long getCreationTime(String path); int countChildren(String path);
zkclient的強大之處不在於基本zookeeper api操做,而在於事件監聽機制,也就是zookeeper的watches。api
Zookeeper的watcher在使用上存在一次性、session過時等難點,所以zkclient對這些問題進行了封裝和屏蔽。 zkclient一共定義了三種事件:session
org.I0Itec.zkclient..IZkStateListenerspa
public void handleStateChanged(KeeperState state) throws Exception; public void handleNewSession() throws Exception;
org.I0Itec.zkclient.IZkDataListenercode
public void handleDataChange(String dataPath, byte[] data) throws Exception; public void handleDataDeleted(String dataPath) throws Exception;
org.I0Itec.zkclient.IZkChildListenerblog
public void handleChildChange(String parentPath, List currentChildren) throws Exception;
IZkClient可以很是方便的訂閱這三種事件:事件
void subscribeStateChanges(IZkStateListener listener); void subscribeDataChanges(String path, IZkDataListener listener); List subscribeChildChanges(String path, IZkChildListener listener);
而zkclient最強大之處在於,當發送session失效時可以自動從新訂閱這些事件,而不須要開發者從新訂閱。開發