Zookeeper之创建组,加入组,列出组成员和删除组

public class CreateGroup implements Watcher {
    private static final int SESSION_TIMEOUT=5000;
    //ZooKeeper类是客户端API的主要类,用于维护客户端和ZooKeeper服务之间的连接
    private ZooKeeper zk;
    //锁存器(latch)此计数器为1,表示在释放所有等待线程之前需要发生的事件数,
    private CountDownLatch connectedSignal= new CountDownLatch(1);

    public void connect(String hosts) throws InterruptedException, IOException {
        //参数this表示一个Watcher对象接收来自于Zookeeper的回调,以获得各种事件的通知,在此表示CreateGroup对象
        zk = new ZooKeeper(hosts, SESSION_TIMEOUT, this);
        connectedSignal.await();
    }

    public void process(WatchedEvent watchedEvent) { // Watcher interface
        if (watchedEvent.getState()==Event.KeeperState.SyncConnected){
            //在调用这个方法表示计数器递减1,若计数器的值变为0,则await()方法返回
            connectedSignal.countDown();
        }
    }

    //创建组
    public void create(String groupName) throws KeeperException, InterruptedException {
        String path="/"+groupName;
        String createPath = zk.create(path, null/*data*/, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);// 持久化的 znode
        System.out.println("Created "+createPath);
    }

    //加入组
    public  void join(String groupName,String memberName) throws KeeperException, InterruptedException {
        String path = "/" + groupName + "/" + memberName;
        String createPath = zk.create(path, null/*data*/, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);// 持久化的 znode
        System.out.println("Created "+createPath);
    }

    //列出组成员
    public  void ListGroup(String groupName){
        String path="/"+groupName;
        try {
            List<String> children = zk.getChildren(path, false);
            if (children.isEmpty()){
                System.out.println(String.format("No members in group %s",groupName));
                System.exit(1);
            }
            for (String child:children){
                System.out.println(child);
            }
        } catch (KeeperException.NoNodeException e) {
            System.out.println(String.format("Group %s does not exist
",groupName));
            System.exit(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
    }
    //删除组成员
    public void deleteGroup(String groupName){
        String path="/"+groupName;
        try {
            List<String> children = zk.getChildren(path, false);
            for (String child:children){
                //节点路径和版本号  将版本号设置为-1 可以绕过版本检测机制
                zk.delete(path+"/"+child,-1);
            }
            zk.delete(path,-1);
        } catch (KeeperException.NoNodeException e) {
            System.out.println(String.format("Group %s does not exist
",groupName));
            System.exit(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
    }

    public void close() throws InterruptedException {
        zk.close();
    }



    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        CreateGroup createGroup = new CreateGroup();
        createGroup.connect("192.168.1.132:2181");
        //createGroup.join("a","b");
        //createGroup.ListGroup("a");
        createGroup.deleteGroup("a");
        createGroup.close();
    }
}
原文地址:https://www.cnblogs.com/tongxupeng/p/10427796.html