标签:
IZkClient 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。
Zookeeper的watcher在使用上存在一次性、session过期等难点,因此zkclient对这些问题进行了封装和屏蔽。 zkclient一共定义了三种事件:
com.github.zkclient.IZkStateListener
public void handleStateChanged(KeeperState state) throws Exception; public void handleNewSession() throws Exception;
com.github.zkclient.IZkDataListener
public void handleDataChange(String dataPath, byte[] data) throws Exception; public void handleDataDeleted(String dataPath) throws Exception;
com.github.zkclient.IZkChildListener
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失效时能够自动重新订阅这些事件,而不需要开发者重新订阅。
标签:
原文地址:http://www.cnblogs.com/rilley/p/5451052.html