/** * * 创建节点 * <p> * * createMode可以指定该节点为临时(ephemeral)的:当创建该节点的session失效后(程序掉了),zookeeper自动删除该节点 * <p> * * createMode 可以指定该节点为顺序的,实际生成的path=参数path为前缀+顺序号。 * 顺序号由10位数字组成,顺序号对于父节点唯一,自增。 * <p> * * 如果该path已存在,则抛出KeeperException.NodeExists异常 * 对于顺序(sequential)节点,从不抛出KeeperException.NodeExists异常,而是增加顺序号 * * 如果父节点不存在,则抛出异常KeeperException.NoNode * * 一个临时(ephemeral)的节点,不能有子节点,如果父节点是临时的,将抛出异常KeeperException.NoChildrenForEphemerals * * * 如果创建操作成功,将会触发Watcher接口被调用: * 1.在该节点上通过exists 调用时关联的Watcher(默认Watcher 和 指定的Watcher) * 2.在该父节点通过getChildren关联的Watcher(默认Watcher 和 指定的Watcher) * * 节点最大的保存数据是 1M,超过抛出KeeperExecption * * @param path * the path for the node 节点路径 * @param data * the initial data for the node 节点的初始数据值 * @param acl * the acl for the node * @param createMode * specifying whether the node to be created is ephemeral * and/or sequential * @return the actual path of the created node * @throws KeeperException if the server returns a non-zero error code * @throws KeeperException.InvalidACLException if the ACL is invalid, null, or empty * @throws InterruptedException if the transaction is interrupted * @throws IllegalArgumentException if an invalid path is specified */ public String create(final String path, byte data[], List<ACL> acl,CreateMode createMode)
/** * * 创建连接是异步的,构建器初始化连接并立即返回。 * Watcher是一个默认的监视器,任何状态的改变都会通知Watcher将会被触发。 * * * Zookeeper客户在连接串中任意选取一个服务地址连接,如果连接失败再另选一个 * 服务地址连接,直到连接被建立。客户端会不断的尝试连接,直到zookeepr被显 * 示的关闭。 * @param connectString 服务地址连接串 * 由逗号分隔的host:port,对应多个zookeeper服务地址。例如 * "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"。 * 如果连接串中指定了路径,如"127.0.0.1:3002/app/a",则认为"/app/a" * 是根节点,之后操作的所有路径都是相对于此节点的,如:“/foo/bar”则 * 对应"/app/a/foo/bar" * * @param sessionTimeout * * session超时时间(毫秒)。服务端有minSessionTimeout和maxSessionTimeout这两个参数设置的,如 * 如指定的sessionTimeout不在此范围内,将自动设成服务端的最大或最小值。 * 连接成功后,在此时间内,客户端向服务端发送心跳包,如果在此时间内服务端没有收到心跳包,则认为 * 此session超时,并清理与此会话相关的信息:临时节点和watcher。 * * @param watcher * * 默认的监视器对象,当状态改变时,被调用通知。 * @throws IOException * in cases of network failure * @throws IllegalArgumentException * if an invalid chroot path is specified */ public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) throws IOException
/** * * 事件类型 **/ public enum EventType { None (-1), //非节点事件 NodeCreated (1),//节点被创建 NodeDeleted (2),//节点被删除 NodeDataChanged (3), //节点数据改变 NodeChildrenChanged (4);//子节点改变 } /** * * **/ public enum KeeperState { /** * 连接断开状态 */ Disconnected (0), /** 连接状态. */ SyncConnected (3), /** * 授权失败状态 */ AuthFailed (4), /** * 客户端连接到只读服务器 * */ ConnectedReadOnly (5), /** * SaslAuthenticated: used to notify clients that they are SASL-authenticated, * so that they can perform Zookeeper actions with their SASL-authorized permissions. */ SaslAuthenticated(6), /** * * session超时状态,连接不再有效,需要重新建立新的客户端连接 */ Expired (-112); }